Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
In my application I want to set MouseHook on button click event,the hook for mouse move event is set and the output is displayed on listbox but the hook for mouse wheel,mouse down and mouse up is set but output is not displayed on listbox. I don't know the actually reason of it,it is due to code problem or another problem. The code written by me as follow.
public int HookProc(int ncode, int wparam, IntPtr lparam)
		{

			if (ncode >= 0)
			{
				MouseHookStruct mousehookstruct = (MouseHookStruct)Marshal.PtrToStructure(lparam, typeof(MouseHookStruct));
				MouseButtons button = MouseButtons.None;
				short mouseDelta = 0;
				int clickCount = 0;
				bool mouseDown = false;
				bool mouseUp = false;
				switch (wparam)
				{
					case WM_LBUTTONDOWN:
						mouseDown = true;
						button = MouseButtons.Left;
						clickCount = 1;
						break;
					case WM_LBUTTONUP:
						mouseDown = true;
						button = MouseButtons.Left;
						clickCount = 1;
						break;
					case WM_MOUSEWHEEL:
						mouseDelta = (short)((mousehookstruct.MouseData) >> 16 & 0xffff);
						break;

				}

				mousehook6mar.MouseEventExtArgs e = new mousehook6mar.MouseEventExtArgs(button,
													clickCount,
												   mousehookstruct.Point.X,
												   mousehookstruct.Point.Y,
												   mouseDelta);


				if (s_MouseUp != null && mouseUp)
				{
					
						s_MouseUp.Invoke(null, e);
					
				}
					if (s_MouseDown != null && mouseDown)
					{
						
							s_MouseDown.Invoke(null, e);
						
						
					}
					if (s_MouseWheel != null && mouseDelta != 0)
					{
						
							s_MouseWheel.Invoke(null, e);
						
						

					}

please tell me, what is the problem.
Thanks.
Posted
Updated 7-Mar-11 17:53pm
v2

It looks like you're trying to set a mouse hook to capture events in the same process. If you need to capture events from various .NET controls in your process, you never need it. The class System.Windows.Forms.Controls has events MouseClick, MouseEnter, MouseUp, MouseDown, MouseScroll, a complete set of keyboard events and so on. As all the controls of the form form hierarchy, you can recursively visit them all and programmatically add all the event handlers you need. Best thing about this approach is that it is platform-independent (for example, surely works on Linux under Mono).

Alternatively, you can override the methods System.Windows.Forms.Form.DefWndProc to process raw messages. This is less supportable method, not platform-independent.

As to the hook techniques, they are extremely delicate and error prone. I don't see why you may need than. Probably, you could explain that, couldn't you?

—SA
 
Share this answer
 
v2
Comments
Olivier Levrey 8-Mar-11 3:47am    
I agree with you, handling "normal" events should be enough. My 5.
Sergey Alexandrovich Kryukov 8-Mar-11 3:58am    
I knew you understand my point :-)
Thank you, Olivier,
--SA
Olivier Levrey 8-Mar-11 4:15am    
You are welcome ;)
Espen Harlinn 13-Mar-11 8:52am    
Seems like OP is just trying to learn how it works - it's useful if you want to create something like a screen grabber :)
Sergey Alexandrovich Kryukov 13-Mar-11 16:35pm    
Well, I agree, thank you.
--SA
Hi,

have a look at this great CP article that handles mouse and keyboard hooks:
Processing Global Mouse and Keyboard Hooks in C#[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Mar-11 1:31am    
The article is valuable, my 5 for the reference. I only don't see why (please see my answer).
--SA
Espen Harlinn 13-Mar-11 8:50am    
Good link, my 5

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