【问题标题】:WndProc as class methodWndProc 作为类方法
【发布时间】:2016-02-03 13:44:28
【问题描述】:

我有一个 Window 类,它包含一个设置窗口句柄 (HWND) 属性的方法。该方法执行以下功能:

_hWnd = CreateWindowEx(dwExStyle, _wcex.lpszClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, _hInstance, this);

我传递 this 参数是因为我使用静态方法作为 WndProc() 函数的包装器,然后它将调用重定向到(非静态)方法。对 CreateWindowEx() 的调用应该将指针放入一个结构中,并在它调用 WndProc() 时将该结构的内存地址传回给我。但是当试图从 lParam 参数中恢复对象指针时,我似乎无法恢复指向我的对象的指针,好像 windows API 传递给 lParam 的值是错误的。

我现在已经上传了完整的代码:

#include <Windows.h>
#include "Window.h"

LRESULT CALLBACK WndProc_main(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM);

int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE prevInstance, _In_opt_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
    const WCHAR szClassName_main[] = L"Main Window Class";

    WNDCLASSEX wcex_main;
    wcex_main.cbSize = sizeof(WNDCLASSEX);
    wcex_main.style = CS_HREDRAW | CS_VREDRAW;
    wcex_main.lpfnWndProc = NULL;
    wcex_main.cbClsExtra = 0;
    wcex_main.cbWndExtra = 0;
    wcex_main.hInstance = hInstance;
    wcex_main.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcex_main.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex_main.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);   // MSDN: +1 should be added to chosen color.
    wcex_main.lpszMenuName = NULL;
    wcex_main.lpszClassName = szClassName_main;
    wcex_main.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    Window window_main = Window(hInstance, wcex_main, WndProc_main);

    window_main.setWindowHandle(WS_EX_CLIENTEDGE, L"Main Window Title", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, NULL);
    window_main.DisplayWindow(nCmdShow);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return static_cast<int> (msg.wParam);
}

LRESULT CALLBACK WndProc_main(_In_ HWND hWnd, _In_ UINT msg, _In_ WPARAM wParam, _In_ LPARAM lParam)
{
    switch (msg)
    {
    case WM_CLOSE:
        DestroyWindow(hWnd);
    case WM_DESTROY:
        PostQuitMessage(EXIT_SUCCESS);
    default:
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    return EXIT_SUCCESS;
}

窗口.h

#ifndef WINDOW_H
#define WINDOW_H

#include <Windows.h>

class Window
{
    public:
        // Constructors
        Window(_In_ HINSTANCE, _In_ WNDCLASSEX, _In_ WNDPROC);
        int setWindowHandle(_In_ DWORD dwExStyle, _In_opt_ LPCWSTR lpWindowName,_In_ DWORD dwStyle, _In_ int x,
                            _In_ int y, _In_ int nWidth, _In_ int nHeight,_In_opt_ HWND hWndParent,
                            _In_opt_ HMENU hMenu, _In_opt_ LPVOID lpParam);
        void DisplayWindow(_In_ int nCmdShow);;

        // Destructor
        ~Window();

        // Public methods
        int registerWindowClass();
        int bindProcFunc(_In_ WNDPROC);
        static LRESULT CALLBACK WndProcWrapper(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM);

    protected:
    private:
        HINSTANCE _hInstance;
        WNDCLASSEX _wcex;
        WNDPROC _WndProc;
        HWND _hWnd;
};

#endif // WINDOW_H

窗口.cpp

#include "Window.h"


Window::Window(HINSTANCE hInstance, WNDCLASSEX wcex, WNDPROC WndProc)
{
    _hInstance = hInstance;
    _wcex = wcex;
    _wcex.lpfnWndProc = WndProcWrapper;
    registerWindowClass();
    bindProcFunc(WndProc);
}

Window::~Window()
{
    // destructor
}

int Window::registerWindowClass()
{
    if (!RegisterClassEx(&_wcex))
    {
        DWORD error = GetLastError();
        if (error == ERROR_CLASS_ALREADY_EXISTS)
        {
            return EXIT_SUCCESS;
        }
        else
        {
            MessageBox(NULL, L"Window class registration failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
            return EXIT_FAILURE;
        }
    }
    return EXIT_SUCCESS;
}

int Window::bindProcFunc(WNDPROC WndProc)
{
    _WndProc = WndProc;
    return EXIT_SUCCESS;
}

LRESULT CALLBACK Window::WndProcWrapper(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    Window* pThis;
    if (msg = WM_NCCREATE)
    {
        pThis = static_cast<Window*> ((reinterpret_cast<CREATESTRUCT*>(lParam))->lpCreateParams);
        SetLastError(0);
        if (!SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis)))
        {
            if (GetLastError() != 0)
            {
                MessageBox(NULL, L"You messed up.", L"Error!", MB_ICONEXCLAMATION | MB_OK);
                return FALSE;
            }
        }
    }
    else
    {
        pThis = reinterpret_cast<Window*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
    }

    if(pThis)
    {
        return pThis->_WndProc(hWnd, msg, wParam, lParam);
    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
}

int Window::setWindowHandle(_In_ DWORD dwExStyle, _In_opt_ LPCWSTR lpWindowName,_In_ DWORD dwStyle, _In_ int x,
                            _In_ int y, _In_ int nWidth, _In_ int nHeight, _In_opt_ HWND hWndParent,
                            _In_opt_ HMENU hMenu, _In_opt_ LPVOID lpParam)
{       
    _hWnd = CreateWindowEx(dwExStyle, _wcex.lpszClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, _hInstance, this);

    if(_hWnd == NULL)
    {
        MessageBox(NULL, L"Window Handle creation failed", L"Error!", MB_ICONEXCLAMATION | MB_OK);
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

void Window::DisplayWindow(_In_ int nCmdShow)
{
    ShowWindow(_hWnd, nCmdShow);
    UpdateWindow(_hWnd);
}

我在这里https://stackoverflow.com/questions/21369256/how-to-use-wndproc-as-a-class-function 和其他一些网站上找到了这段代码,我检查了 CreateWindowEx() 的输入参数,但我似乎找不到哪里出错了。代码编译时没有任何警告。谁能帮我?我使用的是 Visual Studio 2013,编译为 64 位。

P.S:我也尝试用 WM_CREATE 替换 WM_NCCREATE,但这似乎没有帮助。

【问题讨论】:

  • 窗口是用对话框模板创建的吗?
  • 你的想法是对的。类的静态方法可以用作窗口函数。我用了几十年。您的代码不完整。问题出在其他地方。检查窗口类的创建并使用调试器。
  • 编辑:我现在已经上传了完整的代码。
  • @Hans 我正在定义将在其他地方使用的 WndProc() 函数,因此我可以将它们绑定到不同类型窗口的对象的 _wndProc 属性,而不是将其硬编码到类中.该问题与 WndProc_main() 无关,因为我什至无法使用 pThis-> 访问对象的属性,这意味着指针错误。

标签: c++ winapi


【解决方案1】:

我对这个问题感兴趣,花了一些时间调试它,将特殊标记放入该 CREATESTRUCT 并在内存窗口中查找它们等。

然后我很幸运:在我让它运行的时候,我注意到 WM_NCCREATE case 被重复输入,仔细一看我明白了:

if (msg = WM_NCCREATE)

这将获取第一条消息 (WM_GETMINMAXINFO),进行一些不适当的转换,等等......

【讨论】:

  • 这可能是错误哈;我没看到,但我忘了那里有一个“=”符号。是的,这解决了它。
  • 是的,因为我认为 WM_NCCREATE 是第一条消息(错误的假设!)并且因为它像我预期的那样进入了 if 语句,所以我从来没有真正想过检查消息。
  • @Nicolai Microsoft 的编译器将发出关于此(C4706) 的警告,假设您正在以警告级别 4 (/W4) 或更高级别进行编译。你应该,会为你和弗拉德节省很多时间。 :-)
  • 另外,在一个更迂腐的注释中,我不得不指出WM_GETMINMAXINFO 不是必然新窗口收到的第一条消息。事实上,最好不要对消息的顺序做出任何事情,这就是代码专门检查WM_NCCREATE 的原因。你一定会得到那个(连同WM_NCCALCSIZEWM_CREATE),但你不能保证以任何顺序得到消息。子窗口根本不会得到WM_GETMINAMXINFO。同样,这里不批评代码,只是隐含假设 WM_GETMINMAXINFO 将始终是第一条消息。
  • @Cody 是的,在我修复了错误之后,我想知道是否可以要求 Microsoft 实现这样的功能,以便在 if 语句中进行赋值时发出警告,然后我发现了警告级别:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
  • 2018-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多