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

标签 c++ winapi

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

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

谢谢

最佳答案

您可以通过让静态 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/

相关文章:

c# - C++ CLR win32 或其他?

c# - 特定网络接口(interface) IPv4 可用性 - 无连接、本地、Internet

c++ - 读取.txt文件并组织成二维数组

c++ - 小型标量类型和谷歌 Protocol Buffer

c# - 用于 usb 设备通知的 ServiceControlHandler,OnStop() 不可实现

winapi - 捕获 Win32 消息

c++ - 如何获取 WinRT 上的逻辑 CPU 数量?

c++ - 外部库 boost 版本问题

c++ - 程序将无法在 Visual Studio 之外正常运行

c++ - 为什么此代码不创建竞争条件?