《Windows核心编程》の窗口过程的使用
1)設計一個窗口過程:
下面的實例代碼展現了經典的窗口過程的結構。窗口過程使用switch語句處理傳入的消息參數uMsg,
LRESULT CALLBACK MainWndProc(
??? HWND hwnd,??????? // handle to window
??? UINT uMsg,??????? // message identifier
??? WPARAM wParam,??? // first message parameter
??? LPARAM lParam)??? // second message parameter
{
?
??? switch (uMsg)
??? {
??????? case WM_CREATE:
??????????? // Initialize the window.
??????????? return 0;
?
??????? case WM_PAINT:
??????????? // Paint the window's client area.
??????????? return 0;
?
??????? case WM_SIZE:
??????????? // Set the size and position of the window.
??????????? return 0;
?
??????? case WM_DESTROY:
??????????? // Clean up window-specific data objects.
??????????? return 0;
?
??????? //
??????? // Process other messages.
??????? //
?
??????? default:
??????????? return DefWindowProc(hwnd, uMsg, wParam, lParam);
??? }
??? return 0;
}
?
2)將窗口過程和具體的窗口類相關聯:
我們是在注冊窗口類的時候關聯窗口過程的,過程是在填充WNDCLASS結構時,傳入的lpfnWndProc參數就是窗口過程函數的地址,在窗口類注冊完成時,窗口過程就自動和任何由該窗口類創建的窗口相關聯了:
int APIENTRY WinMain(
??? HINSTANCE hinstance,? // handle to current instance
??? HINSTANCE hinstPrev,? // handle to previous instance
??? LPSTR lpCmdLine,????? // address of command-line string
??? int nCmdShow)???????? // show-window type
{
??? WNDCLASS wc;
?
??? // Register the main window class.
??? wc.style = CS_HREDRAW | CS_VREDRAW;
??? wc.lpfnWndProc = (WNDPROC) MainWndProc;
??? wc.cbClsExtra = 0;
??? wc.cbWndExtra = 0;
??? wc.hInstance = hinstance;
??? wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
??? wc.hCursor = LoadCursor(NULL, IDC_ARROW);
??? wc.hbrBackground = GetStockObject(WHITE_BRUSH);
??? wc.lpszMenuName =? "MainMenu";
??? wc.lpszClassName = "MainWindowClass";
?
??? if (!RegisterClass(&wc))
?????? return FALSE;
?
??? //
??? // Process other messages.
??? //
}
?
3)窗口子類化:
要子類化一個窗口,我們使用SetWindowLong函數,指定要子類化的窗口句柄,同時設置GWL_WNDPROC屬性,并傳入子類窗口過程。SetWindowLong函數返回原來窗口過程的指針,我們可以使用這個指針來傳遞消息給原窗口過程;子類窗口過程必須使用CallWindowProc函數來調用原窗口過程。
下面的代碼展現了如何子類化對話框中編輯控件,子類窗口過程允許編輯控件在獲得輸入焦點的情況下接收所有鍵盤輸入,包括Enter和Tab鍵:
WNDPROC wpOrigEditProc;
?
LRESULT APIENTRY EditBoxProc(
??? HWND hwndDlg,
??? UINT uMsg,
??? WPARAM wParam,
??? LPARAM lParam)
{
??? HWND hwndEdit;
?
??? switch(uMsg)
??? {
??????? case WM_INITDIALOG:
??????????? // Retrieve the handle to the edit control.
??????????? hwndEdit = GetDlgItem(hwndDlg, ID_EDIT);
?
??????????? // Subclass the edit control.
??????????? wpOrigEditProc = (WNDPROC) SetWindowLong(hwndEdit,
??????????????? GWL_WNDPROC, (LONG) EditSubclassProc);
??????????? //
??????????? // Continue the initialization procedure.
??????????? //
??????????? return TRUE;
?
??????? case WM_DESTROY:
??????????? // Remove the subclass from the edit control.
??????????? SetWindowLong(hwndEdit, GWL_WNDPROC,
??????????????? (LONG) wpOrigEditProc);
??????????? //
??????????? // Continue the cleanup procedure.
??????????? //
??????????? break;
??? }
??? return FALSE;
??????? UNREFERENCED_PARAMETER(lParam);
}
?
// Subclass procedure
LRESULT APIENTRY EditSubclassProc(
??? HWND hwnd,
??? UINT uMsg,
??? WPARAM wParam,
??? LPARAM lParam)
{
??? if (uMsg == WM_GETDLGCODE)
??????? return DLGC_WANTALLKEYS;
?
??? return CallWindowProc(wpOrigEditProc, hwnd, uMsg,
??????? wParam, lParam);
}
?
?
轉載于:https://www.cnblogs.com/android-html5/archive/2010/08/28/2533997.html
總結
以上是生活随笔為你收集整理的《Windows核心编程》の窗口过程的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 看了本书《答案在你心中》里面的很多问题都
- 下一篇: MySQLdb安装的错误说明