ProtectedVoid
ProtectedVoid

Reputation: 1315

C++ Winapi HWND Get WndProc configuration

I need to get the current WndProc with its messages and configuration and add my own code to it. Why do I need this? Because I'm working under an IDE that defines a window (and its children controls) with a WndProc, and I need to modify it because It contains all the actions related with every control. If I point a control to a custom WndProc the control loses all the actions and configuration set by the IDE. Suggestions?

Scheme:

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

}

Upvotes: 2

Views: 2085

Answers (1)

Nikolay
Nikolay

Reputation: 12235

You can't get source code of "old" WndProc of course, but you can call it using CallWindowProc() in your new wnd proc. Check out this article:

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

Quote:

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

Upvotes: 2

Related Questions