简体   繁体   English

WndProc作为类方法

[英]WndProc as class method

I have a Window class which contains a method to set the attribute for the window handle (HWND). 我有一个Window类,其中包含一种设置窗口句柄(HWND)属性的方法。 The method executes the following function: 该方法执行以下功能:

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

I am passing the this parameter because I'm using a static method as a wrapper around the WndProc() function, which will then redirect the call to (non-static) method. 我传递此参数是因为我将静态方法用作WndProc()函数的包装器,该函数随后会将调用重定向到(非静态)方法。 The call to CreateWindowEx() should put the pointer in a struct and pass me the memory address of that struct back when it calls WndProc(). 对CreateWindowEx()的调用应将指针放在结构中,并在调用WndProc()时将该结构的内存地址传递给我。 But when trying to recover the object pointer from the lParam parameter, I cannot seem to recover the pointer to my object, as if the value that the windows API passes to lParam is wrong. 但是,当尝试从lParam参数恢复对象指针时,似乎无法恢复指向我的对象的指针,好像Windows API传递给lParam的值是错误的。

I have uploaded the full code now: 我已经上传了完整的代码:

#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;
}

Window.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

Window.cpp Window.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);
}

I found this code on here https://stackoverflow.com/questions/21369256/how-to-use-wndproc-as-a-class-function and on a few other sites, and I checked the input parameters to CreateWindowEx(), and I just can't seem to find where it goes wrong. 我在这里https://stackoverflow.com/questions/21369256/how-to-use-wndproc-as-a-class-function和其他一些站点上找到了此代码,并检查了CreateWindowEx()的输入参数,但似乎找不到错误所在。 The code compiles without any warnings. 该代码编译时没有任何警告。 Can anyone help me? 谁能帮我? I'm using Visual Studio 2013, compiling for 64 bit. 我正在使用Visual Studio 2013,编译为64位。

PS: I also tried replacing WM_NCCREATE with WM_CREATE, but that doesn't seem to help. PS:我还尝试将WM_NCCREATE替换为WM_CREATE,但这似乎无济于事。

I got interested in this problem, spent some time debugging it, placing special marker into that CREATESTRUCT and looking for them in the memory window, etc. 我对此问题产生了兴趣,花了一些时间对其进行调试,在CREATESTRUCT中放置了特殊标记,并在内存窗口中寻找它们,等等。

Then I got lucky: while I let it run, I noticed that WM_NCCREATE case was entered repeatedly, and taking a closer look I got it: 然后我很幸运:当我让它运行时,我注意到WM_NCCREATE案例是重复输入的,仔细观察我发现:

if (msg = WM_NCCREATE)

That would grab the first message (WM_GETMINMAXINFO), do some inappropriate casting, and so on... 那将获取第一个消息(WM_GETMINMAXINFO),进行一些不适当的转换,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM