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

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

 

        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);

}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值