You can use SetWindowsHookEx , with WH_CALLWNDPROC or some other type, but here is an example.
WH_CBT can give you great opportunities, because you can get the code HCBT_CREATEWND from it, and it is sent to you right before creating the window, which gives you the opportunity to provide your own proc window instead of the real one, and then you can get all the messages with it.
Remember that great opportunities also mean great responsibility. Say that you โsubclassedโ some window by providing your proc window if your application that sets the hook exits, the next thing you will see is the application whose messages you looked in will work if you did not specify the address the original proc window back to where it belongs. The advantage of this type of interception is the ability to wait for a specific window (say, with a specific window class or name) that will be created and enter this process before any window that interests you is even created.
source share