c++ Winapi HWND获取windproc配置

C++ Winapi HWND Get WndProc configuration

本文关键字:windproc 配置 获取 HWND Winapi c++      更新时间:2023-10-16

我需要获得当前的WndProc及其消息和配置,并将我自己的代码添加到其中。我为什么需要这个?因为我在一个IDE下工作,它定义了一个带有WndProc的窗口(及其子控件),我需要修改它,因为它包含与每个控件相关的所有操作。如果我将控件指向自定义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
}

你当然不能获得"旧的" windowproc的源代码,但是你可以在你的新windowproc中使用CallWindowProc()调用它。

当你子类化一个窗口时,它是窗口的原始窗口过程当你想调用原始窗口过程时你必须调用的子类

:

…你的子类函数应该像这样:

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