#眉標=.NET #副標=Windows Forms中的訊息迴圈 #大標=揭開訊息迴圈的神秘面紗 #作者=文/作者:蔡學鏞 -----box----- #程式1 #include "stdafx.h" #include "Test2.h" #define MAX_LOADSTRING 100 // 全域變數: HINSTANCE hInst; // 目前執行個體 TCHAR szTitle[MAX_LOADSTRING]; // 標題列文字 TCHAR szWindowClass[MAX_LOADSTRING]; // 主視窗類別名稱 // 這個程式碼模組中所包含之函式的向前宣告: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); // 程式進入點 int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // TODO: 在此置入程式碼。 MSG msg; HACCEL hAccelTable; // 初始化全域字串 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_TEST2, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 執行應用程式初始設定: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TEST2); // 主訊息迴圈: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // 函式: MyRegisterClass() // // 用途: 登錄視窗類別。 // // 註解: // // 只有當您希望此程式碼能相容比 Windows 95 的 'RegisterClassEx' 函式更早的 Win32 系統時,才會需要 // 加入及使用這個函式。您必須呼叫這個函式,讓應用程式取得與它相關的 '正確格式 (Well Formed)' 圖示。 // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TEST2); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCTSTR)IDC_TEST2; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex); } // // 函式: InitInstance(HANDLE, int) // // 用途: 儲存執行個體控制代碼並且建立主視窗 // // 註解: // // 在這個函式中,我們會將執行個體控制代碼儲存在全域變數中, // 並且建立和顯示主程式視窗。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // 將執行個體控制代碼儲存在全域變數中 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // 函式: WndProc(HWND, unsigned, WORD, LONG) // // 用途: 處理主視窗的訊息。 // // WM_COMMAND - 處理應用程式功能表 // WM_PAINT - 繪製主視窗 // WM_DESTROY - 傳送結束訊息然後返回 // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // 剖析功能表選取項目: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: 在此加入任何繪圖程式碼... EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // [關於] 方塊的訊息處理常式。 LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; } -----end----- -----box----- #程式2 typedef struct tagMSG { HWND hwnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time; POINT pt; } MSG; -----end----- -----box----- #程式3 protected override void OnPaint(PaintEventArgs e) { // TODO: 加入 Form1.OnPaint 實作 base.OnPaint (e); } -----end----- -----box----- #程式4 protected override void WndProc(ref Message m) { // TODO: 加入 Form1.WndProc 實作 base.WndProc (ref m); } -----end-----