c++ - WM_DESTROY 未在包装的 WndProc 中调用

标签 c++ winapi window wrapper wndproc

为了将 WNDPROC 用作对象方法,我采用了您在那里找到的典型解决方案,但看起来 WM_DESTROY 消息并未发送到对象窗口自身的WNDPROC,关闭窗口后程序不退出。

我的窗口类如下所示(删除了不相关的代码):

class MyWindow : MyApp
{
public:
    MyWindow();
    void Create(void);
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
private:
    HWND _hWnd;
};

void MyWindow::Create()
{
    // Here I register my class and call CreateWindowEx
    // Everything works fine so far

    // Part of the code where I assign the static WNDPROC
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = MyApp::WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = this->Instance;
    wcex.hIcon = LoadIcon(this->Instance, MAKEINTRESOURCE(32512));
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = "MyWindowClass";
    wcex.hIconSm = LoadIcon(this->Instance, MAKEINTRESOURCE(32512));

    RegisterClassExW(&wcex);

    this->_hWnd = CreateWindowExW(
        WS_EX_TOOLWINDOW | WS_EX_TOOLWINDOW,
        wcex.lpszClassName,
        "Window Title",
        WS_POPUP,
        10, 10,
        600, 400,
        nullptr, 
        nullptr, 
        this->Instance,
        nullptr
    );

    ShowWindow(this->_hWnd, SW_SHOW);
    UpdateWindow(this->_hWnd);
}

LRESULT CALLBACK MyWindow::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_CREATE:
    {
        // If I place a MessageBox here, it shows up
    }
    break;
    case WM_DESTROY:
        // It never gets to this point

        // Decrease windows count
        this->WindowsCount--;

        PostQuitMessage(0);
        break;
    break;
    default:
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    return 0;
}

现在是一个包含静态 WNDPROC 的类,它是在创建时分配的

class MyApp
{
public:
    static HINSTANCE Instance;
    static int WindowsCount;

    MyApp();
    static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
};

和实现

LRESULT CALLBACK MyApp::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    // Window object
    MyWindow* myWindow = NULL;

    if (msg == WM_CREATE) {
        myWindow = reinterpret_cast<MyWindow *>(((LPCREATESTRUCT)lParam)->lpCreateParams);
        SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)myWindow);
    }
    else {
        myWindow = reinterpret_cast<MyWindow *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
    }

    // If window not identified, identify now
    if (myWindow) {
        return myWindow->WndProc(hWnd, msg, wParam, lParam);
    }

    // Call window object's processor
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

WM_CLOSE 消息也没有被捕获。我真的不明白为什么这些消息不传递

最佳答案

您正在设置 lpParam CreateWindowEx() 的参数至 nullptr , 所以 myWindow总是 nullptrMyApp::WndProc() ,因此 MyWindow::WndProc()永远不会被调用。你需要通过 this而不是 nullptr .

您也没有进行任何错误检查以确保 RegisterClassExW()CreateWindowEx()在调用 ShowWindow() 之前成功/UpdateWindow() .

另外,考虑使用 SetWindowSubclass() 而不是 (Get|Set)WindowLongPtr(GWLP_USERDATA) .参见 Subclassing Controls在 MSDN 上,以及 Raymond Chen 在 Safer Subclassing 上的博客文章.

关于c++ - WM_DESTROY 未在包装的 WndProc 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48029275/

相关文章:

actionscript-3 - 以编程方式设置 AIR 窗口位置?

c++ - C++ 基类在构造后存储 'this' 的值以供将来的虚拟调用安全吗?

c++ - 使用 TCP 从客户端向服务器发送字节的问题

c++ - 根据长度对集合 <string> 进行排序

C winapi hwnd返回空值

html - 如何在页面加载时滚动到页面中间?

c++ - 如何在 CMake 文件中添加定义 QT_NO_DEBUG_OUTPUT?

c++ - 为 while 循环创建超时的最简洁方法是什么?

pdf - 如何获取字体 PostScript 名称?

c# - C#如何在默认浏览器中打开