C++ Winapi HWND 获取WndProc配置

C++ Winapi HWND Get WndProc configuration

我需要获取当前 WndProc 及其消息和配置,并向其中添加我自己的代码。为什么我需要这个?因为我在定义 window(及其子控件)和 WndProc 的 IDE 下工作,所以我需要修改它,因为它包含与每个控件相关的所有操作.如果我将控件指向自定义 WndProc,该控件将丢失 IDE 设置的所有操作和配置。建议?

方案:

HWND button; //My Button
LONG_PTR wndProc = GetWindowLongPtr(button, GWL_WNDPROC); //Getting the WndProc

wndProc -> Get this `WndProc` source code

LRESULT CALLBACK WndProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){

    wndProc (all the data);
    + my messages

}

您当然无法获得 "old" WndProc 的源代码,但您可以在新的 wnd 过程中使用 CallWindowProc() 调用它。查看这篇文章:

When you subclass a window, it's the original window procedure of the window you subclass you have to call when you want to call the original window procedure

引用:

... your subclass function should go something like this:

wndProcOrig = 
    (WNDPROC)SetWindowLongPtr(hwndButton, GWLP_WNDPROC, (LONG_PTR)SubclassWndProc);  

LRESULT CALLBACK SubclassWndProc(HWND hwnd, UINT wm, WPARAM wParam, LPARAM lParam)
{
   switch (wm) {
   ...
   default:
       return CallWindowProc(wndprocOrig, hwnd, wm, wParam, lParam);
   }
}