Could somebody guide me to find out a windows API function to use in order to intercept a mouse click on certain button on a particular window. I have used spy ++ and learned that when I move the finder tool on to a button or any thing in the window I am able to read information about handler and other things. So is there a way to intercept mouse clicks and inject our code before some action is performed. Thank you very much for sharing any idea about this.
2 Answers
I can think of a couple different ways to approach this:
You can use
SetWindowsHookEx()
to install aWH_CALLWNDPROC
message hook in the target window's thread, and then the hook callback can process window messages likeWM_LBUTTON(DOWN|UP)
on the target window itself, orBN_CLICKED
on the target window's parent. The caveat is the callback needs to be implemented in a DLL so the hook can be injected into the target process. You can get the target window's thread ID usingGetWindowThreadProcessId()
.A variation of this would be to have
SetWindowsHookEx()
install aWH_GETMESSAGE
hook instead, and then you can usePostMessage()
to post a custom window message to the target window. The hook callback can then useSetWindowLongPtr()
orSetWindowSubclass()
to subclass the target window directly, and/or its parent, for further message processing.You can use
SetWinEventHook()
to monitorEVENT_OBJECT_INVOKED
events, such as button clicks. No DLL is needed if you use theWINEVENT_OUTOFCONTEXT
flag, but the caveat is the installing thread needs an active message loop so events can be handled across process boundaries.
You will need to call SetWindowsHookEx API to set a windows hook. Here's an article with source code, that demonstrates its usage: Hooks and DLLs