Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Mouse
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:
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 Smile | :)
 

Thanx in advance!
Posted 18-Oct-12 4:59am
Comments
Silent Guardian at 18-Oct-12 11:13am
   
why not try mousemove event instead of mousedown event
Sergey Alexandrovich Kryukov at 18-Oct-12 13:26pm
   
Why? I think thinks are way simpler -- please see my answer.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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:
 
   public MyNotifyIconControl() {
      // ...
      this.MouseDown += delegate(object sender, MouseEventArgs eventArgs) {
         ActivateColorPicker();
      }; //MouseDown hander
   }
 
[END EDIT]
 
—SA
  Permalink  
v6
Comments
Maciej Los at 18-Oct-12 14:12pm
   
Nice, +5!
Sergey Alexandrovich Kryukov at 18-Oct-12 14:43pm
   
Thank you, Maciej.
--SA
Espen Harlinn at 18-Oct-12 18:46pm
   
5'ed!
Sergey Alexandrovich Kryukov at 18-Oct-12 18:59pm
   
Thank you, Espen.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 18-Oct-12 18:24pm
   
Then we agree...
--SA
Michelle Phoenix at 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)

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 18 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100