现在的位置: 首页 > 综合 > 正文

《Windows核心编程》の窗口过程的使用

2013年08月01日 ⁄ 综合 ⁄ 共 2272字 ⁄ 字号 评论关闭

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函数来调用原窗口过程。

下面的代码展现了如何子类化对话框中编辑控件,子类窗口过程允许编辑控件在获得输入焦点的情况下接收所有键盘输入,包括EnterTab键:

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;

 

       

抱歉!评论已关闭.