Click here to Skip to main content
15,881,638 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hello all,

Please help!

Some background:
I am busy developing a Color Picker type application, making use of this excellent library for the hooks:

Global Mouse and Keyboard Library[^]


The application runs in the taskbar, and is activated when the user clicks on "start capturing". When the "start capturing" button event occurs, I activate a hook into the mouse events.

for example:
C#
mouseHook.MouseDown += new System.Windows.Forms.MouseEventHandler(mouseHook_MouseDown);


Whenever the user then clicks the left mouse button then, the XY co-ords get captured. The capturing works 100%, however...

The problem is, that should the user click on anything "active", example the Windows Start button, or a form's close button, apart from capturing the XY's, the clicked on object's action happens (ex start menu opens up, form closes etc).

Is there anyway of capturing the mouse XY position, without activating whatever it was that was clicked on? (Ideally I will have the user click "stop capturing" again at the end to remove the hook)

I've tried Googling this problem, but it seems a bit hard to search terms for this one :)


Thanx in advance!
Posted
Comments
Silent Guardian 18-Oct-12 11:13am    
why not try mousemove event instead of mousedown event
Sergey Alexandrovich Kryukov 18-Oct-12 13:26pm    
Why? I think thinks are way simpler -- please see my answer.
--SA

You need to capture all such events, because I think, Windows Hook API does not give you other options. But, in handling of this event, you need to discriminate the further action by the coordinates of the click. Just do nothing if the click goes out of the clipping area of your notification icon application. A little problem will be only passing right rectangle extents to the code handling the hooks. As the hook needs to be in the native DLL, to be global, and because the hook event is handled in some different thread, this would be a matter of IPC.

However, I suspect things are much easier in fact. I do understand it you need to setup a global Windows Hook to activate your application on some keyboard sequence, because at this moment you don't know what application window is activated and what control is in focus. I don't understand why would you need a Hook (any one, global or not) to handle this mouse click. If you click in the notification area, the click even will already be dispatched in your application, you only need to handle in a usual way, and it's simper than you wrote. It could be something like this:
C#
public class MyNotifyIconControl :  System.Windows.Forms.NotifyIcon {

   public MyNotifyIconControl() {
      // ...
      this.MouseDown += (sender, eventArgs) => { ActivateColorPicker(); };
   }

   void ActivateColorPicker() {
      if (alredyActivated) return;
      // do something...
   }

   bool alreadyActivated //...

}


Getting the idea?

[EDIT]

Even if you are using C# prior to v.3.0 when lambda syntax was not yet introduced, using anonymous method is still the simplest:

C#
public MyNotifyIconControl() {
   // ...
   this.MouseDown += delegate(object sender, MouseEventArgs eventArgs) {
      ActivateColorPicker();
   }; //MouseDown hander
}


[END EDIT]

—SA
 
Share this answer
 
v6
Comments
Maciej Los 18-Oct-12 14:12pm    
Nice, +5!
Sergey Alexandrovich Kryukov 18-Oct-12 14:43pm    
Thank you, Maciej.
--SA
Espen Harlinn 18-Oct-12 18:46pm    
5'ed!
Sergey Alexandrovich Kryukov 18-Oct-12 18:59pm    
Thank you, Espen.
--SA
So you want your application to "eat" the mouse click so it doesn't go to the next hook in the chain. In that library, there should be a call to a function called CallNextHookEx. That function call has to be skipped if you want your app to "eat" the mouse click so the system doesn't see it.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Oct-12 13:28pm    
Why do you think the global hook is even needed, in this simple case? Probably, OP over-engineered things being just mislead by the referenced article. For keyboard hook, it still makes sense, but not the mouse -- in this particular simple case. Please see my answer.
--SA
Dave Kreskowiak 18-Oct-12 16:50pm    
I never said I cared about what he was doing or why. I only told him why it was doing what it was.

I know it's over engineered. Badly.

Sergey Alexandrovich Kryukov 18-Oct-12 18:24pm    
Then we agree...
--SA
Michelle Phoenix 19-Oct-12 2:26am    
Thank you for the responses so far.

The reasoning behind the global mouse hook is to enable to application to capture mouse clicks even if the clicks are not on the application itself (anywhere on the screen basically). The application form can be minimized for all practical purposes, but should still capture mouse clicks.

Thanx!

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