Click here to Skip to main content
15,912,932 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to create a program that, any time it is running, will track all user left mouse clicks anywhere on the user's screen.

The program will then detect if the mouse click was inside a window (can be any application - IE window, AIM window, etc.).

If it was, I will check to see if the mouse click was between the rectangle defined by points P1, P2 in the aforementioned window. If it is, I will grab the window handle and move the window to location X, Y. That is all.

I know how to move/resize the window if I have the handle (using WinAPI).

What I am having problems coding is, how to return the coordinates of all mouse left-clicks outside of the application? And how do I check if a window is located at those coordinates?

Thanks!
Posted

 
Share this answer
 
I've never tried what you are asking, but you should be able to do this by overriding the WndProc method on each form. This is where the form processes the Windows messages such as mouse clicks. Keep in mind that interpreting the messages will require marshalling to unmanaged libraries. On the upside, there are a lot of code examples and other resources on the web for how to do this.

Basically, you need to check the Message parameter to see what message is being passed. If it is a left mouse down event, call the API to find out the cursor's location on the window. When you are done, make sure to call MyBase.WndProc(m) so the messages can be processed normally.

If you have only a few forms, you can override the method on each one. If you have a lot, you can put the actual processing code in a global module and have the overridden method call that. Or it may be easier to create a MyBaseForm object with the override, and have all your forms inherit from that.

Let us know how it works out, it sounds like you might have article material here when you are done.

Added: Ok, I reread your question. It looks like what you want is the ability to capture mouse clicks that occur outside of your application. That is called a global hook, which is a lot messier to write. Look around the Code Project and I'm sure you can find several articles that will provide an in-depth solution.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900