11.3通用对话框
POPPAD.cpp
#include <windows.h> #include <commdlg.h> #include "resource.h" #define EDITID 1 #define UNTITLED TEXT("(untitled)") LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); BOOL CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM); // Functions in POPFILE.C void PopFileInitialize(HWND); BOOL PopFileOpenDlg(HWND, PTSTR, PTSTR); BOOL PopFileSaveDlg (HWND, PTSTR, PTSTR); BOOL PopFileRead(HWND, PTSTR); BOOL PopFileWrite(HWND, PTSTR); //Functions in POPFIND.C HWND PopFindFindDlg(HWND); HWND PopFindReplaceDlg(HWND); BOOL PopFindFindText(HWND, int *, LPFINDREPLACE); BOOL PopFindReplaceText(HWND, int *, LPFINDREPLACE); BOOL PopFindNextText(HWND, int *); BOOL PopFindValidFind(void); // Functions in POPFONT.C void PopFontInitialize(HWND); BOOL PopFontChooseFont(HWND); void PopFontSetFont(HWND); void PopFontDeinitialize(void); // Functions in POPPRNT.C BOOL PopPrntPrintFile(HINSTANCE, HWND, HWND, PTSTR); // Global variables static HWND hDlgModeless; static TCHAR szAppName[] = TEXT("PopPad"); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MSG msg; HWND hwnd; HACCEL hAccel; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(hInstance, szAppName); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = szAppName; wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } HMENU hMenu = LoadMenu(hInstance,MAKEINTRESOURCE(POPPAD)); hwnd = CreateWindow(szAppName, NULL, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, hMenu, hInstance, szCmdLine); ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); hAccel = LoadAccelerators(hInstance, szAppName); while (GetMessage(&msg, NULL, 0, 0)) { if (hDlgModeless == NULL || !IsDialogMessage(hDlgModeless, &msg)) { if (!TranslateAccelerator(hwnd, hAccel, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } } return msg.wParam; } //将文件名称显示在窗口的标题列上 void DoCaption(HWND hwnd, TCHAR * szTitleName) { TCHAR szCaption[64 + MAX_PATH]; wsprintf (szCaption, TEXT ("%s - %s"), szAppName, szTitleName[0] ? szTitleName:UNTITLED); SetWindowText(hwnd, szCaption); } //向使用者显示消息框 void OkMessage(HWND hwnd, TCHAR * szMessage, TCHAR * szTitleName) { TCHAR szBuffer[64 + MAX_PATH]; wsprintf(szBuffer, szMessage, szTitleName[0] ? szTitleName : UNTITLED); MessageBox(hwnd, szBuffer, szAppName, MB_OK | MB_ICONEXCLAMATION); } //向使用者显示消息框 short AskAboutSave(HWND hwnd, TCHAR * szTitleName) { TCHAR szBuffer[64 + MAX_PATH]; int iReturn; wsprintf(szBuffer, TEXT ("Save current changes in %s?"), szTitleName[0] ? szTitleName : UNTITLED); iReturn = MessageBox (hwnd, szBuffer, szAppName, MB_YESNOCANCEL | MB_ICONQUESTION) ; if (iReturn == IDYES) if (!SendMessage(hwnd, WM_COMMAND, IDM_FILE_SAVE, 0)) iReturn = IDCANCEL; return iReturn; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam) { static BOOL bNeedSave = FALSE; static HINSTANCE hInst; static HWND hwndEdit; static int iOffset; //含有详细的驱动器名称、路径名称和文件名称 static TCHAR szFileName[MAX_PATH]; //是程序本身的文件名称 static TCHAR szTitleName[MAX_PATH]; static UINT messageFindReplace; int iSelBeg, iSelEnd, iEnable; LPFINDREPLACE pfr; switch (message) { case WM_CREATE: hInst = ((LPCREATESTRUCT)lParam)->hInstance; // Create the edit control child window hwndEdit = CreateWindow (TEXT ("edit"), NULL, WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_NOHIDESEL | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 0, 0, 0, 0, hwnd, (HMENU) EDITID, hInst, NULL); SendMessage(hwndEdit, EM_LIMITTEXT, 32000, 0L); // Initialize common dialog box stuff PopFileInitialize(hwnd); PopFontInitialize(hwndEdit); //在显示FindText和ReplaceText对话框时,它们通过一条特殊消息 //与拥有者窗口联络,消息编号可以通过以FINDMSGSTRING //为参数呼叫RegisterWindowMessage函数来获得。 messageFindReplace = RegisterWindowMessage(FINDMSGSTRING); DoCaption(hwnd, szTitleName); return 0; case WM_SETFOCUS: SetFocus(hwndEdit); return 0; case WM_SIZE: MoveWindow(hwndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE) ; return 0; case WM_INITMENUPOPUP: switch (lParam) { case 1: // Edit menu // Enable Undo if edit control can do it EnableMenuItem ((HMENU)wParam, IDM_EDIT_UNDO, SendMessage(hwndEdit, EM_CANUNDO, 0, 0L) ? MF_ENABLED : MF_GRAYED); // Enable Paste if text is in the clipboard EnableMenuItem((HMENU)wParam, IDM_EDIT_PASTE, IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED); // Enable Cut, Copy, and Del if text is selected SendMessage(hwndEdit, EM_GETSEL,(WPARAM)&iSelBeg, (LPARAM)&iSelEnd) ; iEnable = iSelBeg != iSelEnd ? MF_ENABLED : MF_GRAYED; EnableMenuItem((HMENU) wParam, IDM_EDIT_CUT,iEnable); EnableMenuItem((HMENU) wParam, IDM_EDIT_COPY, iEnable); EnableMenuItem((HMENU) wParam, IDM_EDIT_CLEAR, iEnable); break; case 2:// Search menu // Enable Find, Next, and Replace if modeless // dialogs are not already active iEnable = hDlgModeless == NULL ? MF_ENABLED : MF_GRAYED; EnableMenuItem((HMENU)wParam, IDM_SEARCH_FIND, iEnable); EnableMenuItem((HMENU)wParam, IDM_SEARCH_NEXT, iEnable); EnableMenuItem((HMENU)wParam, IDM_SEARCH_REPLACE,iEnable); break; } return 0; case WM_COMMAND: // Messages from edit control if(lParam && LOWORD(wParam) == EDITID) { switch (HIWORD(wParam)) { case EN_UPDATE: bNeedSave = TRUE; return 0; case EN_ERRSPACE: case EN_MAXTEXT: MessageBox(hwnd, TEXT("Edit control out of space."), szAppName, MB_OK | MB_ICONSTOP) ; return 0; } break; } switch (LOWORD(wParam)) { // Messages from File menu case IDM_FILE_NEW: if(bNeedSave && IDCANCEL == AskAboutSave(hwnd, szTitleName)) return 0; SetWindowText(hwndEdit, TEXT("/0")); szFileName[0] = '/0'; szTitleName[0] = '/0'; DoCaption (hwnd, szTitleName); bNeedSave = FALSE; return 0 ; case IDM_FILE_OPEN: if (bNeedSave && IDCANCEL == AskAboutSave(hwnd, szTitleName)) return 0; if (PopFileOpenDlg(hwnd, szFileName, szTitleName)) { if (!PopFileRead(hwndEdit, szFileName)) { OkMessage(hwnd, TEXT("Could not read file %s!"),szTitleName); szFileName[0] = '/0'; szTitleName[0] = '/0'; } } DoCaption(hwnd, szTitleName); bNeedSave = FALSE; return 0; case IDM_FILE_SAVE: if(szFileName[0]) { if(PopFileWrite(hwndEdit, szFileName)) { bNeedSave = FALSE; return 1; } else { OkMessage(hwnd, TEXT("Could not file %s"), szTitleName); return 0; } } //fall through case IDM_FILE_SAVE_AS: if (PopFileSaveDlg(hwnd, szFileName, szTitleName)) { DoCaption(hwnd, szTitleName) ; if (PopFileWrite(hwndEdit, szFileName)) { bNeedSave = FALSE; return 1; } else { OkMessage(hwnd, TEXT ("Could not write file %s"), szTitleName); return 0; } } return 0; case IDM_FILE_PRINT: if(!PopPrntPrintFile(hInst, hwnd, hwndEdit, szTitleName)) OkMessage(hwnd, TEXT("Could not print file %s"), szTitleName); return 0; case IDM_APP_EXIT: SendMessage(hwnd, WM_CLOSE, 0, 0); return 0; // Messages from Edit menu case IDM_EDIT_UNDO: SendMessage(hwndEdit, WM_UNDO, 0, 0); return 0; case IDM_EDIT_CUT: SendMessage(hwndEdit, WM_CUT, 0, 0); return 0; case IDM_EDIT_COPY: SendMessage(hwndEdit, WM_COPY, 0, 0); return 0; case IDM_EDIT_PASTE: SendMessage(hwndEdit, WM_PASTE, 0, 0); return 0; case IDM_EDIT_CLEAR: SendMessage(hwndEdit, WM_CLEAR, 0, 0); return 0; case IDM_EDIT_SELECT_ALL: SendMessage (hwndEdit, EM_SETSEL, 0, -1); return 0; // Messages from Search menu case IDM_SEARCH_FIND: SendMessage(hwndEdit, EM_GETSEL, 0,(LPARAM)&iOffset); hDlgModeless = PopFindFindDlg(hwnd); return 0; case IDM_SEARCH_NEXT: SendMessage(hwndEdit, EM_GETSEL, 0,(LPARAM)&iOffset); if (PopFindValidFind ()) PopFindNextText(hwndEdit, &iOffset); else hDlgModeless = PopFindFindDlg(hwnd); return 0; case IDM_SEARCH_REPLACE: SendMessage(hwndEdit, EM_GETSEL, 0, (LPARAM)&iOffset); hDlgModeless = PopFindReplaceDlg (hwnd); return 0; case IDM_FORMAT_FONT: if (PopFontChooseFont(hwnd)) PopFontSetFont(hwndEdit); return 0; //Messages from Help menu case IDM_HELP: OkMessage(hwnd, TEXT("Help not yet implemented!"),TEXT("/0")); return 0; case IDM_APP_ABOUT: DialogBox(hInst, TEXT("AboutBox"), hwnd, AboutDlgProc); return 0; } break; case WM_CLOSE: if (!bNeedSave || IDCANCEL != AskAboutSave(hwnd, szTitleName)) DestroyWindow(hwnd); return 0; case WM_QUERYENDSESSION: if(!bNeedSave || IDCANCEL != AskAboutSave(hwnd, szTitleName)) return 1; return 0; case WM_DESTROY: PopFontDeinitialize(); PostQuitMessage(0); return 0; default: //在处理内定消息时,WndProc将消息变量与 //RegisterWindowMessage传回的值相比较。 if (message == messageFindReplace) { //lParam消息参数是一个指向FINDREPLACE结构的指针 pfr = (LPFINDREPLACE)lParam; if(pfr->Flags & FR_DIALOGTERM) hDlgModeless = NULL; //Flags字段指示使用者使用对话 //框是为了搜寻文字还是替换文字, //以及是否要终止对话框。 if(pfr->Flags & FR_FINDNEXT) if(!PopFindFindText(hwndEdit, &iOffset, pfr)) OkMessage(hwnd,TEXT("Text not found!"),TEXT("/0")); if (pfr->Flags & FR_REPLACE || pfr->Flags & FR_REPLACEALL) if (!PopFindReplaceText(hwndEdit, &iOffset, pfr)) OkMessage(hwnd, TEXT("Text not found!"),TEXT("/0")); //PopFindFindText和PopFindReplaceText函数来执行搜寻和替换功能 if (pfr->Flags & FR_REPLACEALL) while(PopFindReplaceText(hwndEdit, &iOffset, pfr)); return 0; } break; } return DefWindowProc(hwnd, message, wParam, lParam); } BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: switch(LOWORD(wParam)) { case IDOK: EndDialog(hDlg, 0); return TRUE; } break; } return FALSE; }
POPFILE.cpp
#include <windows.h> #include <commdlg.h> //POPFILE.C具有启动File Open和File Save //对话框的程序代码,它还包含文件I/O例程。 //由于该结构是静态变量,下次启动对话框时,筛选 //条件将被设定为选中的文件型态。 static OPENFILENAME ofn; //ofn的大多数字段在 //PopFileInitialize函数中被初始化 //在WndProc中处理WM_CREATE消息时呼叫该函数。 void PopFileInitialize(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"); static TCHAR szFilter[] = TEXT("Text Files (*.TXT)/0*.txt/0 ASCII Files(*.ASC)/0*.asc/0 All Files (*.*)/0*.*/0/0"); ofn.lStructSize = sizeof(OPENFILENAME); //结构的长度 ofn.hwndOwner = hwnd; //对话框拥有者 ofn.hInstance = NULL; //变量szFilter(一个字符串数组)中为三种 //型态的文件定义了一个筛检清单:带有.TXT扩展名的文本文件、 //带有.ASC扩展名的ASCII文件和所有文件 //OPENFILENAME结构的lpstrFilter字段储存指向此数组第一个字符串的指针 ofn.lpstrFilter = szFilter; ofn.lpstrCustomFilter = NULL; ofn.nMaxCustFilter = 0; //如果使用者在对话框处于活动状态时改变了筛选条件, //那么OPENFILENAME的nFilterIndex字段反映出使用者的选择。 ofn.nFilterIndex = 0; ofn.lpstrFile = NULL; //指向接收完整文件名称的缓冲区 ofn.nMaxFile = MAX_PATH; //缓冲区及其大小 ofn.lpstrFileTitle = NULL; // Set in Open and Close functions ofn.nMaxFileTitle = MAX_PATH; ofn.lpstrInitialDir = NULL; ofn.lpstrTitle = NULL; //文件名称缓冲区 ofn.Flags = 0; // Set in Open and Close functions ofn.nFileOffset = 0; //设定对话框的选项 ofn.nFileExtension = 0; //如果使用者在对话框中输入文件名时不指定文件扩展名, //那么它就是内定的文件扩展名 ofn.lpstrDefExt = TEXT("txt"); ofn.lCustData = 0L; ofn.lpfnHook = NULL; ofn.lpTemplateName = NULL; } //对话框是使用函数GetOpenFileName和GetSaveFileName来显示的。这两个函 //数都使用一个型态为OPENFILENAME的结构,这个结构在COMMDLG.H中定义。 BOOL PopFileOpenDlg(HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName) { //OFN_CREATEPROMPT旗标指示GetOpenFileName显示一个消息框, //询问使用者如果所选文件不存在,是否要建立该文件。 ofn.hwndOwner = hwnd; ofn.lpstrFile = pstrFileName; ofn.lpstrFileTitle = pstrTitleName; ofn.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT; return GetOpenFileName(&ofn); } BOOL PopFileSaveDlg(HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName) { ofn.hwndOwner = hwnd; ofn.lpstrFile = pstrFileName; ofn.lpstrFileTitle = pstrTitleName; ofn.Flags = OFN_OVERWRITEPROMPT; //OFN_OVERWRITEPROMPT旗标导致显示一个消息框,如果被选文件已经存在, //那么将询问使用者是否覆盖该文件。 return GetSaveFileName(&ofn); } //文件是非Unicode文本文件,而执行的是Unicode版的程序, //那么文字必须用MultiCharToWideChar转换。 BOOL PopFileRead(HWND hwndEdit, PTSTR pstrFileName) { BYTE bySwap; DWORD dwBytesRead; HANDLE hFile; int i, iFileLength, iUniTest; PBYTE pBuffer, pText, pConv; // Open the file. if (INVALID_HANDLE_VALUE == (hFile = CreateFile(pstrFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL))) return FALSE; // Get file size in bytes and allocate memory for read. // Add an extra two bytes for zero termination. iFileLength = GetFileSize(hFile, NULL); pBuffer = (PBYTE)malloc(iFileLength + 2); // Read file and put terminating zeros at end. ReadFile(hFile, pBuffer, iFileLength, &dwBytesRead, NULL); CloseHandle(hFile); pBuffer[iFileLength] = '/0'; pBuffer[iFileLength + 1] = '/0'; // Test to see if the text is Unicode iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE; //程序用IsTextUnicode函数来决定文件是否含有字节顺序标记。 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; } } // Allocate memory for possibly converted string pConv = (PBYTE)malloc(iFileLength + 2); // If the edit control is not Unicode, convert Unicode text to // non-Unicode (i.e., in general, wide character). //如果文件是Unicode版,但是被非Unicode版的POPPAD3读取, //这时,文字将被WideCharToMultiChar转换。 #ifndef UNICODE //WideCharToMultiChar实际上是一个宽字符ANSI函数 WideCharToMultiByte(CP_ACP, 0, (PWSTR) pText, -1, pConv, iFileLength + 2, NULL, NULL); // If the edit control is Unicode, just copy the string #else lstrcpy((PTSTR)pConv, (PTSTR)pText); #endif } else// the file is not Unicode { pText = pBuffer; // Allocate memory for possibly converted string. pConv = (PBYTE)malloc(2 * iFileLength + 2); // If the edit control is Unicode, convert ASCII text. #ifdef UNICODE MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pText, -1, (PTSTR)pConv, iFileLength + 1); // If not, just copy buffer #else lstrcpy ((PTSTR)pConv, (PTSTR)pText); #endif } SetWindowText(hwndEdit, (PTSTR)pConv); free(pBuffer); free(pConv); return TRUE; } BOOL PopFileWrite(HWND hwndEdit, PTSTR pstrFileName) { DWORD dwBytesWritten; HANDLE hFile; int iLength; PTSTR pstrBuffer; WORD wByteOrderMark = 0xFEFF; // Open the file, creating it if necessary if (INVALID_HANDLE_VALUE == (hFile = CreateFile(pstrFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL))) return FALSE; // Get the number of characters in the edit control and allocate // memory for them. iLength = GetWindowTextLength(hwndEdit) ; pstrBuffer = (PTSTR)malloc((iLength + 1) * sizeof (TCHAR)) ; if (!pstrBuffer) { CloseHandle(hFile); return FALSE; } //Unicode版的程序将在文件的开始位置写入0xFEFF //这定义为字节顺序标记,以表示文本文件含有Unicode文字。 //也就是第2个参数 #ifdef UNICODE WriteFile(hFile, &wByteOrderMark, 2, &dwBytesWritten, NULL); #endif // Get the edit buffer and write that out to the file. 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; }
POPFIND.cpp
#include <windows.h> #include <commdlg.h> #include <tchar.h> // for _tcsstr (strstr for Unicode & non-Unicode) //POPFIND.C中包含了搜寻和替换文字功能。 #define MAX_STRING_LEN 256 static TCHAR szFindText[MAX_STRING_LEN]; static TCHAR szReplText[MAX_STRING_LEN]; //FindText和ReplaceText使用一个型态为FINDREPLACE的结构 //搜寻和替换对话框是非模态对话框,这意味着必须 //改写消息循环,以便在对话框活动时呼叫IsDialogMessage HWND PopFindFindDlg(HWND hwnd) { static FINDREPLACE fr ; // must be static for modeless dialog!!! fr.lStructSize = sizeof (FINDREPLACE); fr.hwndOwner = hwnd; fr.hInstance = NULL; fr.Flags = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD; fr.lpstrFindWhat = szFindText; fr.lpstrReplaceWith = NULL; fr.wFindWhatLen = MAX_STRING_LEN; fr.wReplaceWithLen = 0; fr.lCustData = 0; fr.lpfnHook = NULL; fr.lpTemplateName = NULL; return FindText(&fr); } HWND PopFindReplaceDlg(HWND hwnd) { static FINDREPLACE fr ; // must be static for modeless dialog!!! fr.lStructSize = sizeof(FINDREPLACE); fr.hwndOwner = hwnd; fr.hInstance = NULL; fr.Flags = FR_HIDEUPDOWN |FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD; fr.lpstrFindWhat = szFindText; fr.lpstrReplaceWith = szReplText; fr.wFindWhatLen = MAX_STRING_LEN; fr.wReplaceWithLen = MAX_STRING_LEN; fr.lCustData = 0; fr.lpfnHook = NULL; fr.lpTemplateName = NULL; return ReplaceText(&fr); } BOOL PopFindFindText(HWND hwndEdit, int * piSearchOffset, LPFINDREPLACE pfr) { int iLength, iPos; PTSTR pstrDoc, pstrPos; // Read in the edit document iLength = GetWindowTextLength(hwndEdit); if (NULL == (pstrDoc = (PTSTR)malloc((iLength + 1) * sizeof(TCHAR)))) return FALSE; GetWindowText(hwndEdit, pstrDoc, iLength + 1); // Search the document for the find string pstrPos = _tcsstr(pstrDoc + * piSearchOffset, pfr->lpstrFindWhat); free(pstrDoc); // Return an error code if the string cannot be found if (pstrPos == NULL) return FALSE; // Find the position in the document and the new start offset iPos = pstrPos - pstrDoc; *piSearchOffset = iPos + lstrlen(pfr->lpstrFindWhat); // Select the found text SendMessage(hwndEdit, EM_SETSEL, iPos, * piSearchOffset); SendMessage(hwndEdit, EM_SCROLLCARET, 0, 0); return TRUE; } BOOL PopFindNextText(HWND hwndEdit, int * piSearchOffset) { FINDREPLACE fr; fr.lpstrFindWhat = szFindText; return PopFindFindText(hwndEdit, piSearchOffset, &fr); } BOOL PopFindReplaceText(HWND hwndEdit, int* piSearchOffset, LPFINDREPLACE pfr) { // Find the text if (!PopFindFindText(hwndEdit, piSearchOffset, pfr)) return FALSE; // Replace it SendMessage(hwndEdit, EM_REPLACESEL, 0, (LPARAM)pfr->lpstrReplaceWith); return TRUE; } BOOL PopFindValidFind(void) { return * szFindText != '/0'; }
POPFONT.cpp
#include <windows.h> #include <commdlg.h> //POPFONT.C包含了字体选择功能 static LOGFONT logfont; static HFONT hFont; BOOL PopFontChooseFont(HWND hwnd) { CHOOSEFONT cf; cf.lStructSize = sizeof(CHOOSEFONT); cf.hwndOwner = hwnd; cf.hDC = NULL; cf.lpLogFont = &logfont; cf.iPointSize = 0; cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS | CF_EFFECTS; cf.rgbColors = 0; cf.lCustData = 0; cf.lpfnHook = NULL; cf.lpTemplateName = NULL; cf.hInstance = NULL; cf.lpszStyle = NULL; cf.nFontType = 0; // Returned from ChooseFont cf.nSizeMin = 0; cf.nSizeMax = 0; return ChooseFont(&cf); } //取得一个依据系统字体建立的LOGFONT结构,由此建立一种字体, //并向编辑控件发送一个WM_SETFONT消息来设定一种新的字体 void PopFontInitialize(HWND hwndEdit) { GetObject(GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), (PTSTR)&logfont); hFont = CreateFontIndirect(&logfont); SendMessage(hwndEdit, WM_SETFONT, (WPARAM) hFont, 0); } //PopFontSetFont来设定编辑控件中的新字体 void PopFontSetFont(HWND hwndEdit) { HFONT hFontNew; RECT rect; hFontNew = CreateFontIndirect(&logfont); SendMessage(hwndEdit, WM_SETFONT, (WPARAM)hFontNew, 0); DeleteObject(hFont); hFont = hFontNew; GetClientRect(hwndEdit, &rect); InvalidateRect(hwndEdit, &rect, TRUE); } //删除最近一次由PopFontSetFont建立的字体。 void PopFontDeinitialize(void) { DeleteObject(hFont); }
POPPAD.rc
///////////////////////////////////////////////////////////////////////////// // // Dialog // ABOUTBOX DIALOG DISCARDABLE 32, 32, 180, 100 STYLE DS_MODALFRAME | WS_POPUP FONT 8, "MS Sans Serif" BEGIN DEFPUSHBUTTON "OK",IDOK,66,80,50,14 ICON "POPPAD",IDC_STATIC,7,7,20,20 CTEXT "PopPad",IDC_STATIC,40,12,100,8 CTEXT "Popup Editor for Windows",IDC_STATIC,7,40,166,8 CTEXT "(c) Charles Petzold, 1998",IDC_STATIC,7,52,166,8 END PRINTDLGBOX DIALOG DISCARDABLE 32, 32, 186, 95 STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU CAPTION "PopPad" FONT 8, "MS Sans Serif" BEGIN PUSHBUTTON "Cancel",IDCANCEL,67,74,50,14 CTEXT "Sending",IDC_STATIC,8,8,172,8 CTEXT "",IDC_FILENAME,8,28,172,8 CTEXT "to print spooler.",IDC_STATIC,8,48,172,8 END ///////////////////////////////////////////////////////////////////////////// // // Accelerator // POPPAD ACCELERATORS DISCARDABLE BEGIN VK_BACK, IDM_EDIT_UNDO, VIRTKEY, ALT, NOINVERT VK_DELETE, IDM_EDIT_CLEAR, VIRTKEY, NOINVERT VK_DELETE, IDM_EDIT_CUT, VIRTKEY, SHIFT, NOINVERT VK_F1, IDM_HELP, VIRTKEY, NOINVERT VK_F3, IDM_SEARCH_NEXT, VIRTKEY, NOINVERT VK_INSERT, IDM_EDIT_COPY, VIRTKEY, CONTROL, NOINVERT VK_INSERT, IDM_EDIT_PASTE, VIRTKEY, SHIFT, NOINVERT "^C", IDM_EDIT_COPY, ASCII, NOINVERT "^F", IDM_SEARCH_FIND, ASCII, NOINVERT "^N", IDM_FILE_NEW, ASCII, NOINVERT "^O", IDM_FILE_OPEN, ASCII, NOINVERT "^P", IDM_FILE_PRINT, ASCII, NOINVERT "^R", IDM_SEARCH_REPLACE, ASCII, NOINVERT "^S", IDM_FILE_SAVE, ASCII, NOINVERT "^V", IDM_EDIT_PASTE, ASCII, NOINVERT "^X", IDM_EDIT_CUT, ASCII, NOINVERT "^Z", IDM_EDIT_UNDO, ASCII, NOINVERT END ///////////////////////////////////////////////////////////////////////////// // // Menu // POPPAD MENU DISCARDABLE BEGIN POPUP "&File" BEGIN MENUITEM "&New/tCtrl+N", IDM_FILE_NEW MENUITEM "&Open.../tCtrl+O", IDM_FILE_OPEN MENUITEM "&Save/tCtrl+S", IDM_FILE_SAVE MENUITEM "Save &As...", IDM_FILE_SAVE_AS MENUITEM SEPARATOR MENUITEM "&Print/tCtrl+P", IDM_FILE_PRINT MENUITEM SEPARATOR MENUITEM "E&xit", IDM_APP_EXIT END POPUP "&Edit" BEGIN MENUITEM "&Undo/tCtrl+Z", IDM_EDIT_UNDO MENUITEM SEPARATOR MENUITEM "Cu&t/tCtrl+X", IDM_EDIT_CUT MENUITEM "&Copy/tCtrl+C", IDM_EDIT_COPY MENUITEM "&Paste/tCtrl+V", IDM_EDIT_PASTE MENUITEM "De&lete/tDel", IDM_EDIT_CLEAR MENUITEM SEPARATOR MENUITEM "&Select All", IDM_EDIT_SELECT_ALL END POPUP "&Search" BEGIN MENUITEM "&Find.../tCtrl+F",IDM_SEARCH_FIND MENUITEM "Find &Next/tF3", IDM_SEARCH_NEXT MENUITEM "&Replace.../tCtrl+R", IDM_SEARCH_REPLACE END POPUP "F&ormat" BEGIN MENUITEM "&Font...", IDM_FORMAT_FONT END POPUP "&Help" BEGIN MENUITEM "&Help", IDM_HELP MENUITEM "&About PopPad...", IDM_APP_ABOUT END END
只呼叫一个函数的Windows程序
COLORS3
#include <windows.h> #include <commdlg.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static CHOOSECOLOR cc; static COLORREF crCustColors[16]; //ChooseColor函数使用一个CHOOSECOLOR型态的结构 //和含有16个DWORD的数组来存放常用颜色 cc.lStructSize = sizeof(CHOOSECOLOR); cc.hwndOwner = NULL; cc.hInstance = NULL; //rgbResult字段初始化为一个颜色值 cc.rgbResult = RGB(0x80, 0x80, 0x80); cc.lpCustColors = crCustColors; //CC_RGBINIT旗标被设立,则显示该颜色 cc.Flags = CC_RGBINIT | CC_FULLOPEN; cc.lCustData = 0; cc.lpfnHook = NULL; cc.lpTemplateName = NULL; //在ChooseColor函数呼叫DialogBox以显示对话框时 //DialogBox的第三个参数也被设定为NULL return ChooseColor(&cc); //该函数创建一个能使用户从中选择颜色的通用颜色对话框 //如果用户点击对话框中的OK按钮,返回值为非零值 }