gpt4 book ai didi

c++ - 面向对象的 C++ win32?

转载 作者:可可西里 更新时间:2023-11-01 17:04:55 24 4
gpt4 key购买 nike

我想创建自己的类来处理创建窗口和窗口过程,但我注意到窗口过程必须是静态的!我现在想知道是否可以使窗口过程面向对象?我已经阅读了一些关于面向对象窗口的教程,但它们总是使过程静态 -.- 那有什么用? :/

任何有关如何解决此问题的链接或信息将不胜感激,

谢谢

最佳答案

您可以通过让静态 WndProc 将所有内容委托(delegate)给成员来解决这个问题:

// Forward declarations
class MyWindowClass;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

std::map<HWND, MyWindowClass *> windowMap;

// Your class
class MyWindowClass {
private:
HWND m_handle;

// The member WndProc
LRESULT MyWndProc(UINT message, WPARAM wParam, LPARAM lParam) { /* ... */ }

public:
MyWindowClass()
{
/* TODO: Create the window here and assign its handle to m_handle */
/* Pass &WndProc as the pointer to the Window procedure */

// Register the window
windowMap[m_handle] = this;
}
};

// The delegating WndProc
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
std::map<HWND, MyWindowClass *>::iterator it = windowMap.find(hWnd);
if (it != windowMap.end())
return it->second->MyWndProc(message, wParam, lParam);
return 0;
}

关于c++ - 面向对象的 C++ win32?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3380294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com