0

我有一个 EDIT 控件,我尝试对其进行子类化,当我这样做时,整个窗口都是空的:

// Global variables
HINSTANCE hInst;
WNDPROC oldEditWndProc;
INT_PTR CALLBACK EditWndProc(HWND, UINT, WPARAM, LPARAM);

// Messages handler of the parent window
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    HDC hDC;
    HWND myedit;

    switch (message)
    {
    case WM_CREATE:
        // Create the EDIT control
        myedit= CreateWindowEx(
                               WS_EX_CLIENTEDGE|WS_EX_CONTROLPARENT, 
                               L"EDIT", 
                               L"",
                               WS_CHILD|WS_VISIBLE|WS_TABSTOP|ES_LEFT, 
                               20, 120, 
                               160, 20, 
                               hWnd, 
                               NULL, 
                               hInst, 
                               NULL
                              );
        oldEditWndProc = (WNDPROC)GetWindowLongPtr (myedit, GWLP_WNDPROC);
        // This is the line where problems begin
        SetWindowLongPtr (myedit, GWLP_WNDPROC, (LONG_PTR)EditWndProc);
        break;
    case WM_PAINT:
        hDC = BeginPaint(hWnd, &ps);
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// EDIT control own handler
INT_PTR CALLBACK EditWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_GETDLGCODE:
            return DLGC_WANTTAB;
            break;
    }
    return (INT_PTR)FALSE;
}

我只是想不通为什么调用时整个窗口都是空的SetWindowLongPtr

4

2 回答 2

3

“空”是指它没有显示在屏幕上?

如果是,那是因为当您子类化一个 Window 时,它的所有消息都将发送到新的 MessageProc,它不具备在其 defaultProc 函数中正确显示它的所有功能(例如 WM_PAINT,甚至是按钮单击)。

所以你需要这样的东西:

//store the old window proc somewhere
WNDPROC oldWindowProc = (WNDPROC)SetWindowLong(subWindowHandle, GWL_WNDPROC, long(NewMessageProc));

//and in the message proc
switch(uMsg)
{
    //your "case"s here
    default:
    {
        CallWindowProc(oldWindowProc, windowHandle, uMsg, wParam, lParam);
    }
}

这将调用适当的 WM_PAINT 和其他消息到负责绘制和单击以及其他功能的 messageProc(子类窗口的 DefWindowProc())。

或者,您也可以在 NewMessageProc 中为所有子类窗口实现 WM_PAINT,但无需调用它们的 DefWindowProc,您需要为它们可能收到的每个事件(鼠标移动、鼠标单击、按钮单击等)执行操作,因此使用它们的原始DefWindowProc 更容易。

于 2013-03-23T21:05:30.897 回答
1

在您的编辑窗口过程中,您需要调用一些适当的默认消息处理。

比如原来的window proc。

在代码的抽象级别上,最简单的方法是使用SetWindowSubclass来进行子类化

于 2013-03-23T21:05:50.050 回答