C++ Winapi HWND 获取 WndProc 配置

标签 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

}

最佳答案

当然,您无法获取“旧”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);
   }
}

关于C++ Winapi HWND 获取 WndProc 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31650118/

相关文章:

c++ - 这种将数据从标准容器移动到共享指针的方法是否正确?

c++ - int a = 5 和 int a(5) 在 C++ 中是否等价?

windows - QueryWorkingSet 在其结果中包含无效页面

windows - 在 JNA 中创建一个 native Windows 窗口,并使用 GWL_WNDPROC 创建一些 GetWindowLong

c# - .Net 中奇怪的 WndProc 错误。更具体地说是 VB.Net

c# - 键盘事件只作用一次

c++ - 无法在 VS2017 中包含 rapidjson 库

c++ - 何时使用静态变量 C++

c++ - 创建带有精美细边框的文本字段。 Winapi

c - 多个窗口,单个托盘图标