深入浅出MFC第2版(PDF格式)-第12部分
按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
#0004 BOOL InitApplication(HANDLE);
#0005 BOOL InitInstance(HANDLE; int);
#0006 LRESULT CALLBACK WndProc(HWND; UINT; WPARAM; LPARAM);
#0007 LRESULT CALLBACK About(HWND; UINT; WPARAM; LPARAM);
Generic。c (粗体代表Windows API 函数或宏)
#0001 //……………………………………………………………………………………………………………………………………………………………………
#0002 // Generic Win32 程序的基础写法
#0003 // Top Studio * J。J。Hou
#0004 // 档名 : generic。c
#0005 // 作者 : 侯俊杰
#0006 // 编译联结 : 请参考generic。mak
#0007 //……………………………………………………………………………………………………………………………………………………………………
#0008
#0009 #include // 每一个 Windows 程序都需要包含此档
#0010 #include 〃resource。h〃 // 內含各个 resource IDs
#0011 #include 〃generic。h〃 // 本程序之含入档
#0012
#0013 HINSTANCE _hInst; // Instance handle
#0014 HWND _hWnd;
10
…………………………………………………………Page 73……………………………………………………………
#0015
#0016 char _szAppName'' = 〃Generic〃; //
程序名称
#0017 char _szTitle'' = 〃Generic Sample Application〃; //
窗口标题
#0018
#0019 //……………………………………………………………………………………………………………………………………………………………………
#0020 // WinMain 程序进入点
#0021 //……………………………………………………………………………………………………………………………………………………………………
#0022 int CALLBACK WinMain (HINSTANCE hInstance; HINSTANCE hPrevInstance;
#0023 LPSTR lpCmdLine; int nCmdShow)
#0024 {
#0025 MSG msg;
#0026
#0027 UNREFERENCED_PARAMETER (lpCmdLine); //
避免编译时的警告
#0028
#0029 if (!hPrevInstance)
#0030 if (!InitApplication(hInstance))
#0031 return (FALSE);
#0032
#0033 if (!InitInstance(hInstance; nCmdShow))
#0034 return (FALSE);
#0035
#0036 while (GetMessage (&msg; NULL; 0; 0)) {
#0037 TranslateMessage (&msg);
#0038 DispatchMessage (&msg);
#0039 }
#0040
#0041 return (msg。wParam); //
传回 PostQuitMessage
的参数
#0042 }
#0043 //……………………………………………………………………………………………………………………………………………………………………
#0044 // InitApplication
注册窗口类别
#0045 //……………………………………………………………………………………………………………………………………………………………………
#0046 BOOL InitApplication(HINSTANCE hInstance)
#0047 {
#0048 WNDCLASS wc;
#0049
#0050 wc。style = CS_HREDRAW | CS_VREDRAW;
#0051 wc。lpfnWndProc = (WNDPROC)WndProc; //窗口函数
#0052 wc。cbClsExtra = 0;
#0053 wc。cbWndExtra = 0;
#0054 wc。hInstance = hInstance;
#0055 wc。hIcon = LoadIcon(hInstance; 〃jjhouricon〃);
#0056 wc。hCursor = LoadCursor(NULL; IDC_ARROW);
#0057 wc。hbrBackground = GetStockObject(WHITE_BRUSH); // 窗口背景颜色
#0058 wc。lpszMenuName = 〃GenericMenu〃; // 。RC
所定义的菜单
#0059 wc。lpszClassName = _szAppName;
#0060
11
…………………………………………………………Page 74……………………………………………………………
#0061 return (RegisterClass (&wc));
#0062 }
#0063 //……………………………………………………………………………………………………………………………………………………………………
#0064 // InitInstance
产生窗口
#0065 //……………………………………………………………………………………………………………………………………………………………………
#0066 BOOL InitInstance(HINSTANCE hInstance; int nCmdShow)
#0067 {
#0068 _hInst = hInstance; //
存储为全域变量,方便使用
#0069
#0070 _hWnd = CreateWindow (
#0071 _szAppName;
#0072 _szTitle;
#0073 WS_OVERLAPPEDWINDOW;
#0074 CW_USEDEFAULT;
#0075 CW_USEDEFAULT;
#0076 CW_USEDEFAULT;
#0077 CW_USEDEFAULT;
#0078 NULL;
#0079 NULL;
#0080 hInstance;
#0081 NULL
#0082 );
#0083
#0084 if (!_hWnd)
#0085 return (FALSE);
#0086
#0087 ShowWindow (_hWnd; nCmdShow); // 显示窗口
#0088 UpdateWindow (_hWnd); // 送出 WM_PAINT
#0089 return (TRUE);
#0090 }
#0091 //……………………………………………………………………………………………………………………………………………………………………
#0092 // WndProc
窗口函数
#0093 //……………………………………………………………………………………………………………………………………………………………………
#0094 LRESULT CALLBACK WndProc(HWND hWnd; UINT message;
#0095 WPARAM wParam; LPARAM lParam)
#0096 {
#0097 int wmId; wmEvent;
#0098
#0099 switch (message) {
#0100 case WM_MAND:
#0101
#0102 wmId = LOWORD (wParam);
#0103 wmEvent = HIWORD (wParam);
#0104
#0105 switch (wmId) {
#0106 case IDM_ABOUT:
12
…………………………………………………………Page 75……………………………………………………………
#0107 DialogBox (_hInst; // Winmain第一个参数,应用程序的实例句柄
#0108 〃AboutBox〃; // 对话框资源名称
#0109 hWnd; // 父窗口
#0110 (DLGPROC)About //对话框函数名称
#0111 );
#0112 break;
#0113
#0114 case IDM_EXIT:
#0115 // 使用者想结束程序。处理方式与 WM_CLOSE 相同
#0116 DestroyWindow (hWnd);
#0117 break;
#0118
#0119 default:
#0120 return (DefWindowProc (hWnd; message; wParam; lParam));
#0121 }
#0122 break;
#0123
#0124 case WM_DESTROY: // 窗口已经被摧毀 (程序即将结束) 。
#0125 PostQuitMessage (0);
#0126 break;
#0127
#0128 default:
#0129 return (DefWindowProc (hWnd; message; wParam; lParam));
#0130 }
#0131 return (0);
#0132 }
#0133 //……………………………………………………………………………………………………………………………………………………………………
#0134 // About …对话框函数
#0135 //……………………………………………………………………………………………………………………………………………………………………
#0136 LRESULT CALLBACK About(HWND hDlg; UINT message;
#0137 WPARAM wParam; LPARAM lParam)
#0138 {
#0139 UNREFERENCED_PARAMETER (lParam); // 避免編译时的警告
#0140
#0141 switch (message) {
#0142 case WM_INITDIALOG:
#0143 return (TRUE); // TRUE 表示我已处理过这个消息
#0144
#0145 case WM_MAND:
#0146 if (LOWORD (wParam) == IDOK
#0147 || LOWORD (wParam) == IDCANCEL) {
#0148 EndDialog (hDlg; TRUE);
#0149 return (TRUE); // TRUE 表示我已处理过这个消息
#0150 }
#0151 break;
#0152 }
13
…………………………………………………………Page 76……………………………………………………………
#0153 return (FALSE); // FALSE 表示我没有处理这个消息
#0154 }
Generic。rc
#0001 //……………………………………………………………………………………………………………………………………………………………………
#0002 // 档名 : generic。rc
#0003 //……………………………………………………………………………………………………………………………………………………………………
#0004 #include 〃windows。h〃
#0005 #include 〃resource。h〃
#0006
#0007 jjhouricon ICON DISCARDABLE 〃jjhour。ico〃
#0008
#0009 GenericMenu MENU DISCARDABLE
#0010 BEGIN
#0011 POPUP 〃&File〃
#0012 BEGIN
#0013 MENUITEM 〃&New〃; IDM_NEW; GRAYED
#0014 MENUITEM 〃&Open。。。〃; IDM_OPEN; GRAYED
#0015 MENUITEM 〃&Save〃; IDM_SAVE; GRAYED
#0016 MENUITEM 〃Save &As。。。〃; IDM_SAVEAS; GRAYED
#0017 MENUITEM SEPARATOR
#0018 MENUITEM 〃&Print。。。〃; IDM_PRINT; GRAYED
#0019 MENUITEM 〃P&rint Setup。。。〃; IDM_PRINTSETUP; GRAYED
#0020 MENUITEM SEPARATOR