C ++ Winapi HWND获取WndProc配置

时间:2015-07-27 09:58:22

标签: c++ winapi wndproc hwnd

我需要获取当前WndProc及其消息和配置,并将自己的代码添加到其中。我为什么需要这个?因为我在使用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

}

1 个答案:

答案 0 :(得分:2)

当然,您无法获得“旧”WndProc的源代码,但您可以在新的wnd proc中使用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

引用:

  

...你的子类函数应该是这样的:

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);
   }
}