RichEdit读写文件

一个小demo:

HWND	RichEdit;
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
   RECT	rcClient;
   hInst = LoadLibrary(TEXT("RICHED20.dll"));
   hInst = hInstance; // 将实例句柄存储在全局变量中

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   GetClientRect(hWnd,&rcClient);
   RichEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
				TEXT("RichEdit20A"),NULL,
				ES_MULTILINE | WS_CHILD |WS_VISIBLE |WS_VSCROLL |WS_HSCROLL | ES_WANTRETURN,
				10,	10,	rcClient.right-50,	rcClient.bottom-50,
				hWnd,	0,	hInstance,	NULL);

   FileInitialize (hWnd) ;

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}
在WndProc中需要加入以下代码:

case IDM_Open:
			{
				ZeroMemory(&ofn,sizeof (ofn));
				ZeroMemory(szFileName,MAX_PATH);

				ofn.lStructSize = sizeof(OPENFILENAME); 
				ofn.hwndOwner = hWnd; 
				ofn.lpstrFile = szFileName; 
				ofn.nMaxFile = sizeof(szFileName); 
				ofn.lpstrFilter = TEXT("Text File(*.txt)/0*.txt/0All File(*.*)/0*.*"); 
				ofn.nFilterIndex = 1; 
				ofn.lpstrFileTitle = NULL; 
				ofn.nMaxFileTitle = 0; 
				ofn.lpstrInitialDir = NULL;

				ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

				if (GetOpenFileName(&ofn) == FALSE)
					break;

				LoadFromFile(RichEdit,szFileName);

				break;
			}
			break;		
		case IDM_Save:
			{
				SaveFile(szFileName,256);
				SaveToFile(RichEdit,szFileName);
			}
			break;


由于文件处理比较繁琐,所以单提出来一个file.cpp头文件:

#include <windows.h>
#include <commdlg.h>

static OPENFILENAME ofn ;

void FileInitialize (HWND hwnd)
{
     static TCHAR szFilter[] = TEXT ("Text Files (*.TXT)\0*.txt\0")  \
                               TEXT ("ASCII Files (*.ASC)\0*.asc\0") \
                               TEXT ("All Files (*.*)\0*.*\0\0") ;
     
     ofn.lStructSize       = sizeof (OPENFILENAME) ;
     ofn.hwndOwner         = hwnd ;
     ofn.hInstance         = NULL ;
     ofn.lpstrFilter       = szFilter ;
     ofn.lpstrCustomFilter = NULL ;
     ofn.nMaxCustFilter    = 0 ;
     ofn.nFilterIndex      = 0 ;
     ofn.lpstrFile         = NULL ;          
     ofn.nMaxFile          = MAX_PATH ;
     ofn.lpstrFileTitle    = NULL ;          
     ofn.nMaxFileTitle     = MAX_PATH ;
     ofn.lpstrInitialDir   = NULL ;
     ofn.lpstrTitle        = NULL ;
     ofn.Flags             = 0 ;            
     ofn.nFileOffset       = 0 ;
     ofn.nFileExtension    = 0 ;
     ofn.lpstrDefExt       = TEXT ("txt") ;
     ofn.lCustData         = 0L ;
     ofn.lpfnHook          = NULL ;
     ofn.lpTemplateName    = NULL ;
}

BOOL FileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
     ofn.hwndOwner         = hwnd ;
     ofn.lpstrFile         = pstrFileName ;
     ofn.lpstrFileTitle    = pstrTitleName ;
     ofn.Flags             = OFN_HIDEREADONLY | OFN_CREATEPROMPT ;
     
     return GetOpenFileName (&ofn) ;
}

BOOL FileSaveDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
     ofn.hwndOwner         = hwnd ;
     ofn.lpstrFile         = pstrFileName ;
     ofn.lpstrFileTitle    = pstrTitleName ;
     ofn.Flags             = OFN_OVERWRITEPROMPT ;
     
     return GetSaveFileName (&ofn) ;
}

BOOL FileRead (HWND hwndEdit, PTSTR pstrFileName)
{
     BYTE   bySwap ;
     DWORD  dwBytesRead ;
     HANDLE hFile ;
     int    i, iFileLength, iUniTest ;
     PBYTE  pBuffer, pText, pConv ;

     if (INVALID_HANDLE_VALUE == 
               (hFile = CreateFile (pstrFileName, GENERIC_READ, FILE_SHARE_READ,
                                    NULL, OPEN_EXISTING, 0, NULL)))
          return FALSE ;

     iFileLength = GetFileSize (hFile, NULL) ; 
     pBuffer = (PBYTE)malloc(iFileLength + 2) ;

     
     ReadFile (hFile, pBuffer, iFileLength, &dwBytesRead, NULL) ;
     CloseHandle (hFile) ;
     pBuffer[iFileLength] = '\0' ;
     pBuffer[iFileLength + 1] = '\0' ;


     iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE ;
     
     if (IsTextUnicode (pBuffer, iFileLength, &iUniTest))
     {
          pText = pBuffer + 2 ;
          iFileLength -= 2 ;

          if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
          {
               for (i = 0 ; i < iFileLength / 2 ; i++)
               {
                    bySwap = ((BYTE *) pText) [2 * i] ;
                    ((BYTE *) pText) [2 * i] = ((BYTE *) pText) [2 * i + 1] ;
                    ((BYTE *) pText) [2 * i + 1] = bySwap ;
               }
          }


          pConv = (PBYTE)malloc(iFileLength + 2) ;

#ifndef UNICODE
		  WideCharToMultiByte (CP_ACP, 0, (PWSTR) pText, -1, (LPSTR)pConv, 
			  iFileLength + 2, NULL, NULL) ;

#else
		  lstrcpy ((PTSTR) pConv, (PTSTR) pText) ;
#endif

	 }
	 else      
	 {
		 pText = pBuffer ;

		 pConv = (PBYTE)malloc (2 * iFileLength + 2) ;


#ifdef UNICODE
		// MultiByteToWideChar (CP_ACP, 0, pText, -1, (PTSTR) pConv, 
		//	 iFileLength + 1) ;
;
		
#else
		 lstrcpy ((PTSTR) pConv, (PTSTR) pText) ;
#endif

     }
     
     SetWindowText (hwndEdit, (PTSTR) pConv) ;
     free (pBuffer) ;
     free (pConv) ;
   
     return TRUE ;
}

BOOL FileWrite (HWND hwndEdit, PTSTR pstrFileName)
{
     DWORD  dwBytesWritten ;
     HANDLE hFile ;
     int    iLength ;
     PTSTR  pstrBuffer ;
     WORD   wByteOrderMark = 0xFEFF ;

         
     
     if (INVALID_HANDLE_VALUE == 
               (hFile = CreateFile (pstrFileName, GENERIC_WRITE, 0, 
                                    NULL, CREATE_ALWAYS, 0, NULL)))
          return FALSE ;

      
     
     iLength = GetWindowTextLength (hwndEdit) ;
     pstrBuffer = (PTSTR) malloc ((iLength + 1) * sizeof (TCHAR)) ;
     
     if (!pstrBuffer)
     {
          CloseHandle (hFile) ;
          return FALSE ;
     }


#ifdef UNICODE
     WriteFile (hFile, &wByteOrderMark, 2, &dwBytesWritten, NULL) ;
#endif

     
     GetWindowText (hwndEdit, pstrBuffer, iLength + 1) ;
     WriteFile (hFile, pstrBuffer, iLength * sizeof (TCHAR), 
                &dwBytesWritten, NULL) ;
     
     if ((iLength * sizeof (TCHAR)) != (int) dwBytesWritten)
     {
          CloseHandle (hFile) ;
          free (pstrBuffer) ;
          return FALSE ;
     }
     
     CloseHandle (hFile) ;
     free (pstrBuffer) ;
     
     return TRUE ;
}
void OkMessage (HWND hwnd, TCHAR * szMessage, TCHAR * szTitleName)
{
	TCHAR szBuffer[64 + MAX_PATH] ;

	wsprintf (szBuffer, szMessage, szTitleName[0] ? szTitleName : _T("UNTITLED")) ;

	MessageBox (hwnd, szBuffer, NULL, MB_OK | MB_ICONEXCLAMATION) ;
}
BOOL LoadFromFile(HWND hEdit,LPCTSTR lpFileName)
{
	HANDLE hFile = NULL;
	HANDLE hFileMapping = NULL;
	LPCTSTR lpData = NULL;


	hFile = CreateFile(lpFileName,GENERIC_READ,FILE_SHARE_READ |         FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if (INVALID_HANDLE_VALUE == hFile)
	{
		return FALSE;
	}

	hFileMapping = CreateFileMapping(hFile,NULL,PAGE_READONLY | SEC_COMMIT,0,0,NULL);

	if (NULL == hFileMapping)
	{
		CloseHandle(hFile);
		return FALSE;
	}

	lpData = (LPCTSTR)MapViewOfFile(hFileMapping,FILE_MAP_READ,0,0,0);

	if (lpData == NULL)
	{
		CloseHandle(hFileMapping);
		CloseHandle(hFile);
	}
	SetWindowText(hEdit,lpData);


	UnmapViewOfFile(lpData);
	CloseHandle(hFileMapping);
	CloseHandle(hFile);


	return TRUE;
}
BOOL SaveToFile(HWND hEdit,LPCTSTR lpFileName)
{
	HANDLE hFile = NULL;
	HANDLE hFileMapping = NULL;
	LPTSTR lpData = NULL;
	int nTextLength = 0;

	hFile = CreateFile(lpFileName,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ |         FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
	if (INVALID_HANDLE_VALUE == hFile)
	{
		return FALSE;
	}
	nTextLength = GetWindowTextLength(hEdit) + 1;

	hFileMapping = CreateFileMapping(hFile,NULL,PAGE_READWRITE,0,nTextLength,NULL);


	if (NULL == hFileMapping)
	{
		CloseHandle(hFile);
		return FALSE;
	}

	lpData = (LPTSTR)MapViewOfFile(hFileMapping,FILE_MAP_WRITE,0,0,0);
	if (lpData == NULL)
	{
		CloseHandle(hFileMapping);
		CloseHandle(hFile);
	}
	GetWindowText(hEdit,lpData,nTextLength);

	FlushViewOfFile(lpData,nTextLength);


	UnmapViewOfFile(lpData);
	CloseHandle(hFileMapping);
	CloseHandle(hFile);



	return TRUE;
}
bool SaveFile(LPTSTR lpFileName,int nLength)
{
	OPENFILENAME ofn;

	ZeroMemory(&ofn,sizeof (ofn));
	ofn.lStructSize = sizeof(OPENFILENAME); 
	ofn.hwndOwner = NULL; 
	ofn.lpstrFile = lpFileName; 
	ofn.nMaxFile = nLength; 
	ofn.lpstrFilter = TEXT("Text File(*.txt)/0*.txt/0All File(*.*)/0*.*"); 
	ofn.nFilterIndex = 1; 
	ofn.lpstrFileTitle = NULL; 
	ofn.nMaxFileTitle = 0; 
	ofn.lpstrInitialDir = NULL; 
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; 
	if (GetSaveFileName(&ofn) == FALSE)
		return false;
	lstrcat(lpFileName,TEXT(".txt"));
	return true;
}


你可能感兴趣的:(File,null,存储,Path,byte)