KItis
KItis

Reputation: 5646

Intercepting windows mouse click

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.

Upvotes: 3

Views: 2600

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 598001

I can think of a couple different ways to approach this:

  1. You can use SetWindowsHookEx() to install a WH_CALLWNDPROC message hook in the target window's thread, and then the hook callback can process window messages like WM_LBUTTON(DOWN|UP) on the target window itself, or BN_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 using GetWindowThreadProcessId().

    A variation of this would be to have SetWindowsHookEx() install a WH_GETMESSAGE hook instead, and then you can use PostMessage() to post a custom window message to the target window. The hook callback can then use SetWindowLongPtr() or SetWindowSubclass() to subclass the target window directly, and/or its parent, for further message processing.

  2. You can use SetWinEventHook() to monitor EVENT_OBJECT_INVOKED events, such as button clicks. No DLL is needed if you use the WINEVENT_OUTOFCONTEXT flag, but the caveat is the installing thread needs an active message loop so events can be handled across process boundaries.

Upvotes: 4

Vada Poché
Vada Poché

Reputation: 780

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

Upvotes: 2

Related Questions