文本编辑器
学习了处理以下问题,和对文件处理的时候要考虑编码,不同编码读取文件需要判断并且进行转换后输出或者输入,例如保存的文件为ANSI,然后读取的时候软件定义为UNICODE(define unicode)那么所有函数将会调用UNICODE版本去处理字符串,会导致乱码,或者其他问题,等等
编码介绍:http://www.cnblogs.com/imissherso/articles/640727.html
学习中遇到的问题:
1.UNICODE在数组中字符一个字节扩展为两个字节,数组索引array[0] = 1则array为000000000000001十六个字节array[1]为下十六个字节 所以索引第二个字符数组为array[1]指针为array+16原本误以为索引第二个字符为array[3]
2.对WM_CLOSE的监控即可达到关闭时候询问的功能
3.TCHAR自动UNICODE可以在视图属性页中更改,默认为UNICODE
4.EM_GETLINE 的LPARAM用法 是*((LPWORD)gBuf) = MAX_GBUF;
5.SendMessage (hwndEdit,WM_KILLFOCUS,0,0) 不是使得窗口失去焦点是窗口失去焦点后得到的消息
6.读取不标准文件时的问题:原来我的程序是直接TCHAR自动UNICODE写入,导致无UNICODE标示符FFFE或者FEFF, 判断直接使用ANSI的SETWINDOWSA导致读取的数据例如:1-----(31 00)数字1的00直接被判断为字符终止符导致后面数据无法显示
10 #include <windows.h> 11 #include "StdAfx.h" 12 #include <commdlg.h> 13 #define ID_LIST 1 14 #define ID_TEXT 2 15 #define ID_EDIT 3 16 #define ID_SAVE 4 17 #define UNTITLED TEXT ("(untitled)") 18 19 #define MAXREAD 8192 20 #define DIRATTR (DDL_READWRITE | DDL_READONLY | DDL_HIDDEN | DDL_SYSTEM | DDL_DIRECTORY | DDL_ARCHIVE | DDL_DRIVES ) 21 #define DTFLAGS (DT_WORDBREAK | DT_EXPANDTABS | DT_NOCLIP |DT_NOPREFIX ) 22 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM , LPARAM) ; 23 LRESULT CALLBACK ListProc (HWND, UINT, WPARAM , LPARAM ) ; 24 BOOL CALLBACK AboutDlgProc (HWND, UINT, WPARAM , LPARAM ) ; 25 /*LRESULT CALLBACK EditProc (HWND, UINT, WPARAM , LPARAM ) ; */ 26 27 // Function in this document 28 BOOL AskConfirmation (HWND hwnd) ; 29 BOOL AskAboutSave (HWND hwnd); 30 VOID DoCaption (HWND hwnd, TCHAR * szTitleName ) ; 31 // Functions in POPFILE.C 32 33 void FileInitialize (HWND) ; 34 BOOL FileOpenDlg (HWND, PTSTR , PTSTR ) ; 35 BOOL FileSaveDlg (HWND, PTSTR , PTSTR ) ; 36 BOOL FileRead (PTCHAR ) ; 37 BOOL FileWrite (PTCHAR ) ; 38 39 // Functions in POPFIND.C 40 HWND FindFindDlg (HWND) ; 41 HWND FindReplaceDlg (HWND) ; 42 BOOL FindFindText (HWND, int *, LPFINDREPLACE ) ; 43 BOOL FindReplaceText (HWND, int *, LPFINDREPLACE ) ; 44 BOOL FindNextText (HWND, int *) ; 45 BOOL FindValidFind (void) ; 46 47 // Functions in POPFONT.C 48 void FontInitialize (HWND) ; 49 BOOL FontChooseFont (HWND) ; 50 void FontSetFont (HWND) ; 51 void FontDeinitialize (void) ; 52 53 WNDPROC OldList ,OldEdit; 54 HINSTANCE hInst ; 55 HWND hwndEdit ; 56 BOOL bValidFile , textUpdate ; 57 TCHAR szDocName [MAX_PATH + 1] ; 58 static HWND hDlgModeless ; 59 60 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance , 61 PSTR szCmdLine, int iCmdShow ) 62 { 63 static TCHAR szAppName[] = TEXT ("TextEdit" ) ; 64 HWND hwnd ; 65 MSG msg ; 66 WNDCLASS wndclass ; 67 HACCEL hAccel ; 68 wndclass .style = CS_HREDRAW | CS_VREDRAW ; 69 wndclass .lpfnWndProc = WndProc ; 70 wndclass .cbClsExtra = 0 ; 71 wndclass .cbWndExtra = 0 ; 72 wndclass .hInstance = hInstance ; 73 wndclass .hIcon = LoadIcon (NULL, IDI_APPLICATION ) ; 74 wndclass .hCursor = LoadCursor (NULL, IDC_ARROW) ; 75 wndclass .hbrBackground = (HBRUSH ) ( COLOR_BTNFACE + 1) ; 76 wndclass .lpszMenuName = MAKEINTRESOURCE (IDC_MENU) ; 77 wndclass .lpszClassName = szAppName ; 78 79 if (! RegisterClass (& wndclass )) 80 { 81 MessageBox ( NULL, TEXT ("This program requires Windows NT!" ), 82 szAppName, MB_ICONERROR) ; 83 return 0 ; 84 } 85 86 hwnd = CreateWindow (szAppName, TEXT ("Textedit" ), 87 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN , 88 CW_USEDEFAULT , CW_USEDEFAULT , 89 CW_USEDEFAULT , CW_USEDEFAULT , 90 NULL, NULL, hInstance, NULL) ; 91 92 ShowWindow (hwnd, iCmdShow ) ; 93 UpdateWindow (hwnd) ; 94 95 hAccel = LoadAccelerators (hInstance, MAKEINTRESOURCE (IDC_TEXTEDIT)) ; 96 while (GetMessage (& msg , NULL, 0, 0)) 97 { 98 if (hDlgModeless == NULL || ! IsDialogMessage (hDlgModeless, & msg )) 99 { 100 if (! TranslateAccelerator (hwnd, hAccel , & msg )) 101 { 102 TranslateMessage (& msg ) ; 103 DispatchMessage (& msg ) ; 104 } 105 } 106 } 107 return msg.wParam ; 108 } 109 110 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam) 111 { 112 static TCHAR buffer[MAXREAD],szFile [MAX_PATH + 1],szBuffer [MAX_PATH + 1],szTitle [MAX_PATH + 1]; 113 static HWND hwndList , hwndText , hwndButton,hCtrl; 114 static RECT rect ; 115 static int iOffset ; 116 static HINSTANCE hInstance ; 117 static UINT messageFindReplace ; 118 LPFINDREPLACE pfr ; 119 HANDLE hFile ; 120 int i,iCount,iLength,cxChar,cyChar,exitProduceJudge ,iEnable,iSelect,iStart,iEnd; 121 iCount = 0 ; 122 iLength = 0 ; 123 124 switch (message) 125 { 126 case WM_CREATE : 127 hInstance = ((LPCREATESTRUCT) lParam)->hInstance ; 128 cxChar = LOWORD (GetDialogBaseUnits ()) ; 129 cyChar = HIWORD (GetDialogBaseUnits ()) ; 130 rect.left = 20 * cxChar ; 131 rect.top = 3 * cyChar ; 132 133 hwndList = CreateWindow (TEXT ("listbox"), NULL, 134 WS_CHILDWINDOW | WS_VISIBLE | LBS_STANDARD , 135 cxChar, cyChar * 3, 136 cxChar * 13 + GetSystemMetrics (SM_CXVSCROLL), 137 cyChar * 29, 138 hwnd, ( HMENU) ID_LIST, 139 ( HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE ), NULL) ; 140 141 GetCurrentDirectory (MAX_PATH + 1, szBuffer ) ; 142 143 hwndText = CreateWindow (TEXT ("static" ), szBuffer , 144 WS_CHILDWINDOW | WS_VISIBLE | SS_LEFT, 145 cxChar, cyChar, cxChar * MAX_PATH , cyChar , 146 hwnd, ( HMENU) ID_TEXT, 147 ( HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE ), 148 NULL) ; 149 150 hwndEdit = CreateWindow (TEXT ("edit" ), NULL, 151 WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | 152 WS_BORDER | ES_LEFT | ES_MULTILINE | ES_WANTRETURN | 153 ES_AUTOHSCROLL | ES_AUTOVSCROLL, 154 0,0,0,0, hwnd, ( HMENU ) ID_EDIT, 155 ((LPCREATESTRUCT) lParam ) -> hInstance, NULL) ; 156 157 158 FileInitialize (hwnd) ; 159 FontInitialize (hwndEdit) ; 160 messageFindReplace = RegisterWindowMessage (FINDMSGSTRING) ; 161 OldList = (WNDPROC) SetWindowLong (hwndList , GWL_WNDPROC , 162 ( LPARAM) ListProc ) ; 163 // OldEdit = (WNDPROC) SetWindowLong (hwndEdit , GWL_WNDPROC , 164 // ( LPARAM) EditProc ) ; 165 166 SendMessage (hwndList , LB_DIR , DIRATTR, ( LPARAM ) TEXT ("*.*" )) ; 167 return 0 ; 168 169 case WM_SIZE : 170 rect.right = LOWORD (lParam ) ; 171 rect.bottom = HIWORD (lParam ) ; 172 MoveWindow(hwndEdit,rect.left,rect.top,rect.right - 165,rect.bottom - 50,1) ; 173 return 0 ; 174 case WM_SETFOCUS : 175 SetFocus (hwndEdit ) ; 176 return 0 ; 177 case WM_INITMENUPOPUP : 178 if (lParam == 1) 179 { 180 181 EnableMenuItem (( HMENU ) wParam , IDM_EDIT_PASTE, 182 IsClipboardFormatAvailable (CF_TEXT) ? MF_ENABLED : MF_GRAYED) ; 183 184 iSelect = SendMessage (hwndEdit , EM_GETSEL, 0, 0) ; 185 186 if (HIWORD (iSelect) == LOWORD (iSelect)) 187 iEnable = MF_GRAYED ; 188 else 189 iEnable = MF_ENABLED ; 190 191 EnableMenuItem (( HMENU ) wParam , IDM_EDIT_CUT, iEnable) ; 192 EnableMenuItem (( HMENU ) wParam , IDM_EDIT_COPY , iEnable) ; 193 SendMessage (hwndEdit, EM_GETSEL, (WPARAM) &iStart,(LPARAM) &iEnd) ; 194 if (iStart == iEnd) 195 EnableMenuItem((HMENU)wParam,IDM_EDIT_DELETE,MF_GRAYED) ; 196 else 197 EnableMenuItem((HMENU)wParam,IDM_EDIT_DELETE,MF_ENABLED) ; 198 return 0 ; 199 } 200 return 0 ; 201 case WM_COMMAND : 202 switch (LOWORD (wParam)) 203 { 204 case ID_LIST : 205 if (HIWORD (wParam ) == LBN_DBLCLK ) 206 { 207 208 if (LB_ERR == ( i = SendMessage (hwndList , LB_GETCURSEL, 0, 0))) 209 break ; 210 SendMessage (hwndList , LB_GETTEXT , i, ( LPARAM ) szBuffer ) ; 211 212 if (INVALID_HANDLE_VALUE != ( hFile = CreateFile (szBuffer , 213 GENERIC_READ, 0 , NULL, 214 OPEN_EXISTING , 0, NULL))) 215 { 216 CloseHandle (hFile ) ; 217 if (FALSE == AskAboutSave(hwnd)) 218 return 0 ; 219 bValidFile = TRUE ; 220 lstrcpy (szFile , szBuffer ) ; 221 GetCurrentDirectory (MAX_PATH + 1, szBuffer ) ; 222 if (szBuffer [lstrlen (szBuffer ) - 1] != '\\') 223 lstrcat (szBuffer , TEXT ("\\")) ; 224 lstrcat (szBuffer , szFile ) ; 225 lstrcpy (szDocName,szBuffer) ; 226 SetWindowText (hwndText , szBuffer) ; 227 FileRead(szFile) ; 228 } 229 else 230 { 231 bValidFile = FALSE ; 232 szBuffer [lstrlen (szBuffer ) - 1] = '\0' ; 233 234 // If setting the directory doesn't work, maybe it's 235 // a drive textUpdate, so try that. 236 if (! SetCurrentDirectory (szBuffer + 1)) 237 { 238 szBuffer [3] = ':' ; 239 szBuffer [4] = '\0' ; 240 SetCurrentDirectory (szBuffer + 2) ; 241 } 242 // Get the new directory name and fill the list box. 243 244 GetCurrentDirectory (MAX_PATH + 1, szBuffer ) ; 245 SetWindowText (hwndText , szBuffer ) ; 246 SendMessage (hwndList , LB_RESETCONTENT , 0, 0) ; 247 SendMessage (hwndList , LB_DIR , DIRATTR, ( LPARAM ) TEXT ("*.*" )) ; 248 SendMessage (hwndEdit,WM_CLEAR,0,0) ; 249 } 250 DoCaption(hwnd,szFile) ; 251 SetFocus (hwndEdit) ; 252 } 253 return 0 ; 254 //case ID_EDIT : 255 // switch (HIWORD (wParam)) 256 // { 257 // case EN_MAXTEXT: 258 // MessageBox (hwnd,TEXT ("Your words are too much"),TEXT ("Error"),MB_OK) ; 259 // return 0 ; 260 // case EN_UPDATE: 261 // textUpdate = TRUE ; 262 // } 263 // return 0 ; 264 case IDM_EDIT_CUT: 265 SendMessage (hwndEdit , WM_CUT , 0, 0) ; 266 return 0 ; 267 case IDM_EDIT_COPY : 268 SendMessage (hwndEdit , WM_COPY, 0, 0) ; 269 return 0 ; 270 case IDM_EDIT_PASTE: 271 SendMessage (hwndEdit , WM_PASTE , 0, 0) ; 272 return 0 ; 273 case IDM_EDIT_DELETE: 274 SendMessage (hwndEdit, EM_REPLACESEL, 0, NULL) ; 275 return 0 ; 276 case IDM_EDIT_UNDO: 277 SendMessage (hwndEdit,WM_UNDO,0,0) ; 278 return 0 ; 279 case IDM_EDIT_SELECTALL: 280 SendMessage (hwndEdit, EM_SETSEL, 0, MAXREAD) ; 281 return 0 ; 282 case IDM_FILE_NEW: 283 if (FALSE == AskAboutSave (hwnd)) 284 return 0 ; 285 SetWindowText (hwndEdit, TEXT ("\0")) ; 286 szFile[0] = '\0' ; 287 szTitle[0] = '\0' ; 288 DoCaption (hwnd, szTitle) ; 289 textUpdate = FALSE ; 290 return 0 ; 291 case IDM_FILE_OPEN : 292 if (IDCANCEL == AskAboutSave (hwnd)) 293 return 0 ; 294 if (FileOpenDlg (hwnd, szFile , szTitle)) 295 { 296 if (! FileRead (szFile)) 297 { 298 MessageBox (hwnd,TEXT ("Can't open this file"),TEXT ("Sorry"),IDOK) ; 299 szFile[0] = '\0' ; 300 szTitle[0] = '\0' ; 301 } 302 } 303 DoCaption ( hwnd, szTitle) ; 304 textUpdate = FALSE ; 305 return 0 ; 306 case IDM_FILE_SAVE : 307 if (szFile[0]) 308 { 309 if (FileWrite (szFile)) 310 { 311 textUpdate = FALSE ; 312 return 1 ; 313 } 314 else 315 { 316 MessageBox (hwnd,TEXT ("Could not write this file"),TEXT ("Sorry"),IDOK) ; 317 return 0 ; 318 } 319 } 320 case IDM_FILE_SAVE_AS : 321 if (FileSaveDlg (hwnd, szFile , szTitle)) 322 { 323 DoCaption ( hwnd, szTitle) ; 324 325 if (FileWrite (szFile)) 326 { 327 textUpdate = FALSE ; 328 return 1 ; 329 } 330 else 331 { 332 MessageBox (hwnd,TEXT ("Could not write this file"),TEXT ("Sorry"),IDOK) ; 333 return 0 ; 334 } 335 } 336 return 0 ; 337 case IDM_FILE_EXIT : 338 SendMessage (hwnd,WM_CLOSE,0,0) ; 339 return 0 ; 340 case IDM_SEARCH_FIND: 341 SendMessage (hwndEdit , EM_GETSEL, 0, (LPARAM) & iOffset) ; 342 hDlgModeless = FindFindDlg (hwnd) ; 343 return 0 ; 344 case IDM_SEARCH_NEXT: 345 SendMessage (hwndEdit , EM_GETSEL, 0, (LPARAM) & iOffset) ; 346 347 if (FindValidFind ()) 348 FindNextText (hwndEdit , & iOffset) ; 349 else 350 hDlgModeless = FindFindDlg (hwnd) ; 351 352 return 0 ; 353 case IDM_SEARCH_REPLACE : 354 SendMessage (hwndEdit , EM_GETSEL, 0, (LPARAM) & iOffset) ; 355 hDlgModeless = FindReplaceDlg (hwnd) ; 356 return 0 ; 357 case IDM_FORMAT_FONT: 358 if (FontChooseFont (hwnd)) 359 FontSetFont (hwndEdit ) ; 360 return 0 ; 361 case IDM_ABOUT : 362 DialogBox (hInstance,TEXT("AboutBox"),hwnd,AboutDlgProc) ; 363 return 0 ; 364 case WM_CLOSE : 365 if (TRUE == AskAboutSave(hwnd)) 366 DestroyWindow (hwnd) ; 367 return 0 ; 368 case WM_DESTROY : 369 FontDeinitialize () ; 370 PostQuitMessage (0) ; 371 return 0 ; 372 default: 373 // Process "Find-Replace" messages 374 if (message == messageFindReplace) 375 { 376 pfr = (LPFINDREPLACE) lParam ; 377 378 if (pfr->Flags & FR_DIALOGTERM) 379 hDlgModeless = NULL ; 380 381 if (pfr->Flags & FR_FINDNEXT) 382 if (!FindFindText (hwndEdit, &iOffset, pfr)) 383 MessageBox (hwnd,TEXT ("Text not found"),TEXT ("Sorry"),NULL) ; 384 385 if (pfr->Flags & FR_REPLACE || pfr->Flags & FR_REPLACEALL) 386 if (!FindReplaceText (hwndEdit, &iOffset, pfr)) 387 MessageBox (hwnd,TEXT ("Text not found"),TEXT ("Sorry"),NULL) ; 388 389 if (pfr->Flags & FR_REPLACEALL) 390 while (FindReplaceText (hwndEdit, &iOffset, pfr)) ; 391 392 return 0 ; 393 } 394 break ; 395 } 396 397 } 398 return DefWindowProc (hwnd, message, wParam , lParam ) ; 399 } 400 401 LRESULT CALLBACK ListProc (HWND hwnd, UINT message, 402 WPARAM wParam, LPARAM lParam) 403 { 404 if (message == WM_KEYDOWN && wParam == VK_RETURN) 405 SendMessage (GetParent (hwnd), WM_COMMAND , 406 MAKELONG (1, LBN_DBLCLK ), (LPARAM ) hwnd) ; 407 return CallWindowProc (OldList, hwnd, message, wParam , lParam ) ; 408 409 } 410 BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message, 411 WPARAM wParam, LPARAM lParam) 412 { 413 switch (message) 414 { 415 case WM_INITDIALOG: 416 return true ; 417 case WM_COMMAND: 418 switch(wParam) 419 { 420 case IDOK: 421 case IDCANCEL: 422 EndDialog(hDlg,0) ; 423 return true ; 424 } 425 break ; 426 } 427 return FALSE ; 428 } 429 430 int AskConfirmation (HWND hwnd) 431 { 432 TCHAR wndName[200] = {TEXT ("Do you want to save to ")} ; 433 wcscat (wndName,szDocName) ; 434 wcscat (wndName,TEXT (" ?")) ; 435 436 return MessageBox (hwnd, wndName, 437 TEXT ("point out"), MB_YESNOCANCEL ) ; 438 } 439 int AskAboutSave (HWND hwnd) 440 { 441 int relay ; 442 if (bValidFile && textUpdate) 443 { 444 if (IDYES == (relay = AskConfirmation(hwnd))) 445 { 446 SendMessage (hwnd,WM_COMMAND,IDM_FILE_SAVE,0) ; 447 return TRUE ; 448 } 449 else if (IDNO == relay) 450 { 451 textUpdate = FALSE ; 452 return TRUE ; 453 } 454 else 455 return FALSE ; 456 } 457 return TRUE ; 458 459 } 460 void DoCaption (HWND hwnd, TCHAR * szTitleName ) 461 { 462 TCHAR szCaption[64 + MAX_PATH ] ; 463 wsprintf (szCaption, TEXT ("%s - %s"), TEXT ("TextEdit"), 464 szTitleName [0] ? szTitleName : UNTITLED ) ; 465 SetWindowText (hwnd, szCaption) ; 466 }
1 #include "StdAfx.h" 2 #include <windows.h> 3 #include <commdlg.h> 4 5 extern HWND hwndEdit ; 6 static OPENFILENAME ofn ; 7 void FileInitialize (HWND hwnd) 8 { 9 static TCHAR szFilter [] = { 10 TEXT ("Text Files (*.TXT)\0*.txt\0") 11 TEXT ("ASCII Files (*.ASC)\0*.asc\0") 12 TEXT ("All Files (*.*)\0*.*\0\0" ) 13 }; 14 15 ofn .lStructSize = sizeof (OPENFILENAME) ; 16 ofn .hwndOwner = hwnd ; 17 ofn .hInstance = NULL ; 18 ofn .lpstrFilter = szFilter ; 19 ofn .lpstrCustomFilter = NULL ; 20 ofn .nMaxCustFilter = 0 ; 21 ofn .nFilterIndex = 0 ; 22 ofn .lpstrFile = NULL ; // Set in Open and Close functions 23 ofn .nMaxFile = MAX_PATH ; 24 ofn .lpstrFileTitle = NULL ; // Set in Open and Close functions 25 ofn .nMaxFileTitle = MAX_PATH ; 26 ofn .lpstrInitialDir = NULL ; 27 ofn .lpstrTitle = NULL ; 28 ofn .Flags = 0 ; // Set in Open and Close functions 29 ofn .nFileOffset = 0 ; 30 ofn .nFileExtension = 0 ; 31 ofn .lpstrDefExt = TEXT ("txt" ) ; 32 ofn .lCustData = 0L ; 33 ofn .lpfnHook = NULL ; 34 ofn .lpTemplateName = NULL ; 35 } 36 37 BOOL FileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName ) 38 { 39 ofn .hwndOwner = hwnd ; 40 ofn .lpstrFile = pstrFileName ; 41 ofn .lpstrFileTitle = pstrTitleName ; 42 ofn .Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT ; 43 44 return GetOpenFileName (& ofn ) ; 45 } 46 47 48 BOOL FileSaveDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName ) 49 { 50 ofn .hwndOwner = hwnd ; 51 ofn .lpstrFile = pstrFileName ; 52 ofn .lpstrFileTitle = pstrTitleName ; 53 ofn .Flags = OFN_OVERWRITEPROMPT ; 54 55 return GetSaveFileName (& ofn ) ; 56 } 57 BOOL FileRead (PTCHAR pFileName) 58 { 59 int iLength ,iUniTest,i; 60 BYTE bySwap ; 61 PBYTE pBuffer, pText , pConv ; 62 HANDLE hFile ; 63 64 65 if (INVALID_HANDLE_VALUE == 66 ( hFile = CreateFile (pFileName, GENERIC_READ, FILE_SHARE_READ , 67 NULL, OPEN_EXISTING , 0, NULL))) 68 return FALSE ; 69 70 iLength = GetFileSize (hFile , NULL) ; 71 pBuffer = (PBYTE)malloc (iLength + 2) ; 72 73 // Read file and put terminating zeros at end. 74 ReadFile (hFile, pBuffer, iLength , (LPDWORD)& i , NULL) ; 75 pBuffer[iLength ] = '\0' ; 76 pBuffer[iLength + 1] = '\0' ; 77 78 // Test to see if the text is Unicode 79 iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE ; 80 if (IsTextUnicode (pBuffer, iLength , & iUniTest )) 81 { 82 pText = pBuffer + 2 ; 83 iLength -= 2 ; 84 85 if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE ) 86 { 87 for ( i = 0 ; i < iLength / 2 ; i++) 88 { 89 bySwap = (( BYTE *) pText ) [2 * i] ; 90 ((BYTE *) pText ) [2 * i] = ((BYTE *) pText ) [2 * i + 1] ; 91 ((BYTE *) pText ) [2 * i + 1] = bySwap ; 92 } 93 } 94 95 // Allocate memory for possibly converted string 96 pConv = (PBYTE)malloc (iLength + 2) ; 97 // If the edit control is not Unicode, convert Unicode text to 98 // non-Unicode (i.e., in general, wide character). 99 #ifndef UNICODE 100 WideCharToMultiByte (CP_ACP , 0, (PWSTR ) pText , -1, pConv , 101 iFileLength + 2, NULL, NULL) ; 102 // If the edit control is Unicode, just copy the string 103 #else 104 lstrcpy (( PTSTR ) pConv , ( PTSTR ) pText ) ; 105 #endif 106 107 } 108 else // the file is not Unicode 109 { 110 pText = pBuffer ; 111 // Allocate memory for possibly converted string. 112 pConv = (PBYTE)malloc (2 * iLength + 2) ; 113 // If the edit control is Unicode, convert ASCII text. 114 #ifdef UNICODE 115 MultiByteToWideChar (CP_ACP , 0, (LPCSTR)pText , -1, (PTSTR ) pConv , 116 iLength + 1) ; 117 // If not, just copy buffer 118 #else 119 lstrcpy (( PTSTR ) pConv , ( PTSTR ) pText ) ; 120 #endif 121 } 122 123 SetWindowText (hwndEdit , ( PTSTR ) pConv ) ; 124 free (pBuffer) ; 125 free (pConv ) ; 126 CloseHandle (hFile) ; 127 return TRUE ; 128 } 129 BOOL FileWrite (PTSTR pFileName) 130 { 131 DWORD dwBytesWritten ; 132 HANDLE hFile ; 133 int iLength ; 134 PTSTR pstrBuffer ; 135 WORD wByteOrderMark = 0xFEFF ; 136 // Open the file, creating it if necessary 137 138 if (INVALID_HANDLE_VALUE == 139 ( hFile = CreateFile (pFileName, GENERIC_WRITE , 0, 140 NULL, CREATE_ALWAYS , 0, NULL))) 141 return FALSE ; 142 // Get the number of characters in the edit control and allocate 143 // memory for them. 144 145 iLength = GetWindowTextLength (hwndEdit ) ; 146 pstrBuffer = (PTSTR ) malloc (( iLength + 1) * sizeof (TCHAR )) ; 147 148 if (! pstrBuffer) 149 { 150 CloseHandle (hFile ) ; 151 return FALSE ; 152 } 153 154 // If the edit control will return Unicode text, write the 155 // byte order mark to the file. 156 157 #ifdef UNICODE 158 WriteFile (hFile , & wByteOrderMark, 2, &dwBytesWritten, NULL) ; 159 #endif 160 // Get the edit buffer and write that out to the file. 161 GetWindowText (hwndEdit , pstrBuffer , iLength + 1) ; 162 WriteFile (hFile , pstrBuffer , iLength * sizeof (TCHAR ), 163 & dwBytesWritten, NULL) ; 164 if (( iLength * sizeof (TCHAR )) != (int) dwBytesWritten) 165 { 166 CloseHandle (hFile ) ; 167 free (pstrBuffer) ; 168 return FALSE ; 169 } 170 171 CloseHandle (hFile ) ; 172 free (pstrBuffer) ; 173 174 return TRUE ; 175 }
1 #include "StdAfx.h" 2 #include <windows.h> 3 #include <commdlg.h> 4 #include <tchar.h> // for _tcsstr (strstr for Unicode & non-Unicode) 5 6 #define MAX_STRING_LEN 256 7 8 static TCHAR szFindText [MAX_STRING_LEN] ; 9 static TCHAR szReplText [MAX_STRING_LEN] ; 10 11 HWND FindFindDlg (HWND hwnd) 12 { 13 static FINDREPLACE fr ; // must be static for modeless dialog!!! 14 15 fr.lStructSize = sizeof (FINDREPLACE) ; 16 fr.hwndOwner = hwnd ; 17 fr.hInstance = NULL ; 18 fr.Flags = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD ; 19 fr.lpstrFindWhat = szFindText ; 20 fr.lpstrReplaceWith = NULL ; 21 fr.wFindWhatLen = MAX_STRING_LEN ; 22 fr.wReplaceWithLen = 0 ; 23 fr.lCustData = 0 ; 24 fr.lpfnHook = NULL ; 25 fr.lpTemplateName = NULL ; 26 27 return FindText (& fr) ; 28 } 29 30 HWND FindReplaceDlg ( HWND hwnd) 31 { 32 static FINDREPLACE fr ; // must be static for modeless dialog!!! 33 34 fr.lStructSize = sizeof (FINDREPLACE) ; 35 fr.hwndOwner = hwnd ; 36 fr.hInstance = NULL ; 37 fr.Flags = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD ; 38 fr.lpstrFindWhat = szFindText ; 39 fr.lpstrReplaceWith = szReplText ; 40 fr.wFindWhatLen = MAX_STRING_LEN ; 41 fr.wReplaceWithLen = MAX_STRING_LEN ; 42 fr.lCustData = 0 ; 43 fr.lpfnHook = NULL ; 44 fr.lpTemplateName = NULL ; 45 46 return ReplaceText (& fr) ; 47 } 48 49 BOOL FindFindText ( HWND hwndEdit , int * piSearchOffset, LPFINDREPLACE pfr ) 50 { 51 int iLength, iPos ; 52 PTSTR pstrDoc, pstrPos ; 53 54 // Read in the edit document 55 iLength = GetWindowTextLength (hwndEdit ) ; 56 57 if (NULL == (pstrDoc = (PTSTR ) malloc (( iLength + 1) * sizeof (TCHAR )))) 58 return FALSE ; 59 60 GetWindowText (hwndEdit , pstrDoc, iLength + 1) ; 61 62 // Search the document for the find string 63 pstrPos = _tcsstr (pstrDoc + * piSearchOffset, pfr ->lpstrFindWhat ) ; 64 65 66 // Return an error code if the string cannot be found 67 if (pstrPos == NULL) 68 return FALSE ; 69 70 // Find the position in the document and the new start offset 71 iPos = pstrPos - pstrDoc ; 72 * piSearchOffset = iPos + lstrlen (pfr ->lpstrFindWhat ) ; 73 74 // Select the found text 75 SendMessage (hwndEdit , EM_SETSEL, iPos, * piSearchOffset) ; 76 SendMessage (hwndEdit , EM_SCROLLCARET, 0, 0) ; 77 SetFocus (hwndEdit) ; 78 free (pstrDoc) ; 79 return TRUE ; 80 } 81 BOOL FindNextText ( HWND hwndEdit , int * piSearchOffset) 82 { 83 FINDREPLACE fr ; 84 fr.lpstrFindWhat = szFindText ; 85 return FindFindText (hwndEdit , piSearchOffset, & fr) ; 86 } 87 88 BOOL FindReplaceText ( HWND hwndEdit , int * piSearchOffset, LPFINDREPLACE pfr ) 89 { 90 // Find the text 91 if (!FindFindText (hwndEdit , piSearchOffset, pfr )) 92 return FALSE ; 93 94 // Replace it 95 SendMessage (hwndEdit , EM_REPLACESEL , 0, (LPARAM) pfr -> 96 lpstrReplaceWith ) ; 97 return TRUE ; 98 } 99 100 BOOL FindValidFind (void) 101 { 102 return * szFindText != '\0' ; 103 }
1 #include "StdAfx.h" 2 #include <windows.h> 3 #include <commdlg.h> 4 5 static LOGFONT logfont ; 6 static HFONT hFont ; 7 8 BOOL FontChooseFont ( HWND hwnd) 9 { 10 CHOOSEFONT cf ; 11 cf.lStructSize = sizeof (CHOOSEFONT ) ; 12 cf.hwndOwner = hwnd ; 13 cf.hDC = NULL ; 14 cf.lpLogFont = &logfont ; 15 cf.iPointSize = 0 ; 16 cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS | CF_EFFECTS ; 17 cf.rgbColors = 0 ; 18 cf.lCustData = 0 ; 19 cf.lpfnHook = NULL ; 20 cf.lpTemplateName = NULL ; 21 cf.hInstance = NULL ; 22 cf.lpszStyle = NULL ; 23 cf.nFontType = 0 ; // Returned from ChooseFont 24 cf.nSizeMin = 0 ; 25 cf.nSizeMax = 0 ; 26 27 return ChooseFont (& cf) ; 28 } 29 30 void FontInitialize (HWND hwndEdit ) 31 { 32 GetObject (GetStockObject (SYSTEM_FONT ), sizeof (LOGFONT), 33 ( PTSTR ) & logfont) ; 34 hFont = CreateFontIndirect (& logfont) ; 35 SendMessage (hwndEdit , WM_SETFONT , ( WPARAM ) hFont , 0) ; 36 } 37 38 void FontSetFont (HWND hwndEdit ) 39 { 40 HFONT hFontNew ; 41 RECT rect ; 42 43 hFontNew = CreateFontIndirect (& logfont) ; 44 SendMessage (hwndEdit , WM_SETFONT , ( WPARAM ) hFontNew , 0) ; 45 DeleteObject (hFont ) ; 46 hFont = hFontNew ; 47 GetClientRect (hwndEdit , & rect) ; 48 InvalidateRect (hwndEdit , & rect, TRUE) ; 49 } 50 51 void FontDeinitialize (void) 52 { 53 DeleteObject (hFont ) ; 54 }