WndProc的类方法
Class method for WndProc
本文出色地解释了调用类成员WndProc的选项。我在stackoverflow中看到了这个响应,但在CreateWindow之后关联类成员WndProc的主要问题是,正如前面提到的文章中所解释的,一些消息(包括重要的WM_CREATE)将丢失。
我的问题:我想听听专家的意见,看看下面公开的方法或新方法中哪一个是创建类成员WndProc的最佳方法(性能、可维护性…)。
简要介绍了文章中公开的两个最终解决方案(假设它存在一个带有WndProc方法的Window类):
-
具有
this
全局指针存储的每个窗口数据,使用CRITICAL_SECTION对其进行保护,使其线程安全(从此处提取):// The helper window procedure // It is called by Windows, and thus it's a non-member function // This message handler will only be called after successful SetWindowLong call // We can assume that pointer returned by GetWindowLong is valid // It will route messages to our member message handler LRESULT CALLBACK WndProc2(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { // Get a window pointer associated with this window Window *w = (Window *) GetWindowLong(hwnd, GWL_USERDATA); // It should be valid, assert so _ASSERT(w); // Redirect messages to the window procedure of the associated window return w->WndProc(hwnd, msg, wp, lp); } // The temporary global this pointer // It will be used only between CreateWindow is called and the first message is processed by WndProc // WARNING: it is not thread-safe. Window *g_pWindow; // Critical section protecting the global Window pointer CRITICAL_SECTION g_WindowCS; // The helper window procedure LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { // Stash global Window pointer into per-window data area SetWindowLong(hwnd, GWL_USERDATA, (long) g_pWindow); // Unlock global critical section g_pWindow->HaveCSLock = false; LeaveCriticalSection(&g_WindowCS); // Reset the window message handler SetWindowLong(hwnd, GWL_WNDPROC, (long) WndProc2); // Dispatch first message to the member message handler return WndProc2(hwnd, msg, wp, lp); }
现在我们可以创建窗口:
InitializeCriticalSection(&g_WindowCS); // Enter the critical section before you write to protected data EnterCriticalSection(&g_WindowCS); // Set global Window pointer to our Window instance // Moved the assignment here, where we have exclusive access to the pointer g_pWindow = &w; // Set a flag indicating that the window has the critical section lock // Note: this must be executed after the above assignment g_pWindow->HaveCSLock = true; // Create window // Note: lpParam is not used HWND hwnd = CreateWindow(TEXT("BaseWnd"), TEXT("Hello, World!"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, hinst, 0); // Leave critical section if window creation failed and our window procedure hasn't released it if (g_pWindow->HaveCSLock) LeaveCriticalSection(&g_WindowCS); // Destroy critical section // In production code, you'd do this when application terminates, not immediately after CreateWindow call DeleteCriticalSection(&g_WindowCS);
-
使用CBT挂钩程序(从这里提取):
// The helper window procedure // It is called by Windows, and thus it's a non-member function // This message handler will only be called after successful SetWindowLong call from the hook // We can assume that pointer returned by GetWindowLong is valid // It will route messages to our member message handler LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { // Get a window pointer associated with this window Window *w = (Window *) GetWindowLong(hwnd, GWL_USERDATA); // It should be valid, assert so _ASSERT(w); // Redirect messages to the window procedure of the associated window return w->WndProc(hwnd, msg, wp, lp); } // The CBT hook procedure // It is called during CreateWindow call before WndProc receives any messages // Its job is to set per-window Window pointer to the one passed through lpParam to CreateWindow LRESULT CALLBACK CBTProc(int code, WPARAM wp, LPARAM lp) { if (code != HCBT_CREATEWND) { // Ignore everything but create window requests // Note: generally, HCBT_CREATEWND is the only notification we will get, // assuming the thread is hooked only for the duration of CreateWindow call. // However, we may receive other notifications, in which case they will not be passed to other CBT hooks. return 0; } // Grab a pointer passed to CreateWindow as lpParam std::pair<Window *, HHOOK> *p = (std::pair<Window *, HHOOK> *) LPCBT_CREATEWND(lp)->lpcs->lpCreateParams; // Only handle this window if it wasn't handled before, to prevent rehooking windows when CreateWindow is called recursively // ie, when you create windows from a WM_CREATE handler if (p->first) { // Stash the associated Window pointer, which is the first member of the pair, into per-window data area SetWindowLong((HWND) wp, GWL_USERDATA, (long) p->first); // Mark this window as handled p->first = 0; } // Call the next hook in chain, using the second member of the pair return CallNextHookEx(p->second, code, wp, lp); }
现在我们可以创建窗口:
// Install the CBT hook // Note: hook the thread immediately before, and unhook it immediately after CreateWindow call. // The hook procedure can only process window creation nofitications, and it shouldn't be called for other types of notifications // Additionally, calling hook for other events is wasteful since it won't do anything useful anyway HHOOK hook = SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadId()); _ASSERT(hook); // Create window // Pass a pair consisting of window object pointer and hook as lpParam std::pair<Window *, HHOOK> p(&w, hook); HWND hwnd = CreateWindow(TEXT("BaseWnd"), TEXT("Hello, World!"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, hinst, &p); // Unhook first UnhookWindowsHookEx(hook);
我个人不会使用这两种方法。全局变量方法有效,但感觉很肮脏。尤其是锁。而CBT的钩子,远远超过了顶部。尽管它指向正确的方向。
在创建过程中将状态信息传递给窗口过程的标准方法是通过CreateWindow
或CreateWindowEx
的lpParam
参数。因此,技术如下:
- 在
CreateWindow
或CreateWindowEx
的lpParam
参数中传递实例指针 - 在
WM_NCCREATE
处理程序中读取此值。该消息将信息作为CREATESTRUCT
结构的一部分提供 - 仍然在
WM_NCCREATE
中调用SetWindowLongPtr
以将窗口的用户数据设置为实例指针 - 所有未来对窗口过程的调用现在都可以通过调用
GetWindowLongPtr
来获得实例指针
Raymond Chen在这里说明了详细信息:如何使WNDPROC或DLGPROC成为C++类的成员?
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 在静态库中嵌入类方法
- 如何制作一个将函数作为参数的类方法
- 从父类方法返回子类对象
- 使用用户定义的参数调用future/async并调用类方法
- 重载类方法的不明确调用
- 单独定义模板化嵌套类方法的正确语法
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- 如何访问由共享指针保存的类方法?
- 将子类方法声明为基类的友元
- 我的模板类方法返回错误类型?
- Qt将信号与另一个类方法连接
- 我无法使用C++指针指向类方法返回的 std::vector
- 如何在成为指向基类的指针后保留对子类方法的使用?
- 检查类方法中是否(此 == nullptr)
- 从基类实例调用派生类方法而不进行强制转换
- 函数从唯一代码调用正确的子类方法
- C++ - 如何在不静态的情况下将回调绑定到类方法?
- C++ |DLL / EXE - 如何从导出的类调用另一个类方法?
- WndProc的类方法