Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intercept a message sent to any TWinControl on my form?

I'm faced with the daunting task of having to intercept and handle the WM_GETOBJECT message whenever it is sent to any TWinControl on a Form.

  • every panel
  • every nested panel
  • every edit box
  • every combo box
  • every button
  • every toolbar
  • every datetime picker
  • every image
  • every listview
  • every treeivew
  • every win control
  • on every form

Obviously i'd prefer not to have to individually subclass every control:

  • it's difficult to do correctly (may people use SetWindowSubclass when they want to start subclassing, and RemoveWindowSubclass when they're done, and don't realize the crash they just introduced)
  • it's difficult to do correctly
  • it requires subclassing every control individually, likely through a child control iteration function (which has the common bug of failing if you apply it when the form handle is created and removing them when the form handle is destroyed)

Is there a way to be involved in the handling of every message sent directly to a child control using SendMessage

  • similar to how TApplicationEvents.OnMessage can intercept every posted message
  • similar to how KeyPreview allows a form to see every send Key message
like image 640
Ian Boyd Avatar asked Mar 07 '23 13:03

Ian Boyd


1 Answers

If you don't want to subclass each individual control (which is certainly an option, and one that can be simplified using interposer classes, for instance), then you can instead use a thread-specific WH_CALLWNDPROC or WH_CALLWNDPROCRET hook via the Win32 API SetWindowsHookEx() function. The hooks will tell you which HWND is receiving each message, and you don't need to implement the hooks in a DLL when hooking a thread in the same process as the hooker.

If you need the TWinControl* pointer for a given HWND, you can use the VCL's FindControl() function in the Vcl.Controls unit.

like image 55
Remy Lebeau Avatar answered Mar 25 '23 05:03

Remy Lebeau