如何检测鼠标是否整个窗体和子控件内? [英] How to detect if the mouse is inside the whole form and child controls?

查看:300
本文介绍了如何检测鼠标是否整个窗体和子控件内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要当用户在该表及其所有子控件移动鼠标,并当它离开的形式来检测。我试过的MouseEnter 鼠标离开表单的事件,我试过 WM_MOUSEMOVE &安培; WM_MOUSELEAVE WM_NCMOUSEMOVE &安培; WM_NCMOUSELEAVE 对Windows消息,但没有似乎工作,因为我想...

I need to detect when the user moves the mouse over the Form and all its child controls and also when it leaves the Form. I tried the MouseEnter and MouseLeave events of the Form, I tried the WM_MOUSEMOVE & WM_MOUSELEAVE and WM_NCMOUSEMOVE & WM_NCMOUSELEAVE pairs of windows messages but none seem to work as I want...

我的大多数形式是由许多种子控件占用,没有太多的客户端区域可见。这意味着,如果我快速移动鼠标鼠标移动将不会被检测到,虽然鼠标的形式中。

Most of my Form is occupied by child controls of many sorts, there's not much client area visible. This means that if I move the mouse very quickly the mouse movement will not be detected, although the mouse is inside the Form.

例如,我有一个是停靠在底部和桌面和文本框之间的一个TextBox,这里只有一个非常小的边界。如果我迅速从底部到文本框移动鼠标,鼠标移动将不会被检测到,但鼠标的文本框内,因此,在表单内。

For instance, I have a TextBox that is docked at the bottom and between the desktop and the TextBox, there's only a very small border. If I quickly move the mouse from the bottom into the TextBox, the mouse movement won't be detected, but the mouse is inside the TextBox, therefore, inside the Form.

如何才能实现我需要什么?

How can I achieve what I need?

推荐答案

您可以挂钩你想要的主消息循环和preprocess /后处理的(WM_MOUSEMOVE)消息。

You can hook the main message loop and preprocess/postprocess any (WM_MOUSEMOVE) message what you want.

public class Form1 : Form {
	private MouseMoveMessageFilter mouseMessageFilter;
	protected override void OnLoad( EventArgs e ) {
		base.OnLoad( e );

		this.mouseMessageFilter = new MouseMoveMessageFilter();
		this.mouseMessageFilter.TargetForm = this;
		Application.AddMessageFilter( this.mouseMessageFilter );
	}

	protected override void OnClosed( EventArgs e ) {
		base.OnClosed( e );

		Application.RemoveMessageFilter( this.mouseMessageFilter );
	}

	class MouseMoveMessageFilter : IMessageFilter {
		public Form TargetForm { get; set; }

		public bool PreFilterMessage( ref Message m ) {
			int numMsg = m.Msg;
			if ( numMsg == 0x0200 /*WM_MOUSEMOVE*/) {
				this.TargetForm.Text = string.Format( "X:{0}, Y:{1}", Control.MousePosition.X, Control.MousePosition.Y );
			}

			return false;
		}

	}
}

这篇关于如何检测鼠标是否整个窗体和子控件内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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