笔记——《C语言也能干大事》之对话框程序代码
下面是对话框程序框架代码:
// // resource.h // // 控件 ID. // #define IDD_MAIN 100 #define IDC_OK 101 #define IDC_CANCEL 102
// // main.h // #ifndef _MAIN_H #define _MAIN_H #include <windows.h> // // 自定义函数前置声明. // BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam); void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify); void Main_OnClose(HWND hwnd);
#endif
// // dialogs.cpp // #include <commctrl.h>
#include "main.h" #include "resource.h" int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { InitCommonControls();
// 创建对话框. DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, Main_Proc); return 0; }
// // main.cpp // #include <windows.h> #include <windowsx.h> #include "main.h" #include "resource.h" // // 主对话框过程. // BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog); HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand); HANDLE_MSG(hWnd, WM_CLOSE, Main_OnClose); } return FALSE; } BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) { // // Add initializing code here // return TRUE; } void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) { switch(id) { case IDC_OK: MessageBox(hwnd,TEXT("You clicked OK!"),TEXT("test2"),MB_OK); EndDialog(hwnd, id); break; case IDC_CANCEL: MessageBox(hwnd,TEXT("You clicked Cancel!"),TEXT("test2"),MB_OK); EndDialog(hwnd, id); break; default:break; } } void Main_OnClose(HWND hwnd) { EndDialog(hwnd, 0); }