如何设置鼠标钩上按钮单击事件 [英] How to set Mouse hook on button click event

查看:65
本文介绍了如何设置鼠标钩上按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想在按钮单击事件上设置MouseHook,设置了mouseHook的钩子事件,并且输出显示在列表框上,但是设置了用于鼠标滚轮,鼠标下移和鼠标上钩的钩子,但未显示输出在列表框上.我不知道它的真正原因,这是由于代码问题或其他问题.我写的代码如下.

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.

推荐答案

似乎您正在尝试设置一个鼠标钩来捕获同一过程中的事件.如果您需要从流程中的各种.NET控件捕获事件,则永远不需要它.类System.Windows.Forms.Controls具有事件MouseClickMouseEnterMouseUpMouseDownMouseScroll,完整的键盘事件集,等等.作为表单表单层次结构的所有控件,您可以递归地访问它们,并以编程方式添加所需的所有事件处理程序.这种方法最好的一点是,它与平台无关(例如,肯定可以在Mono下的Linux上运行).

或者,您可以覆盖方法System.Windows.Forms.Form.DefWndProc来处理原始消息.这是不太受支持的方法,而不是与平台无关的.

至于钩子技术,它们非常精致且容易出错.我不明白为什么您可能需要.也许您可以解释一下,对吗?

—SA
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




看看有关处理鼠标和键盘挂钩的出色CP文章:
在C#中处理全局鼠标和键盘挂钩 [
Hi,

have a look at this great CP article that handles mouse and keyboard hooks:
Processing Global Mouse and Keyboard Hooks in C#[^]


这篇关于如何设置鼠标钩上按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆