C# MouseHover 和 MouseLeave 事件控制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12552809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MouseHover and MouseLeave Events controlling
提问by vish213
I was building a simple form with one simple effect- opacity reduced when mouse is not over the form, and form becomes opaque when mouse is over it. I am currently encountering couple of difficulties:-
我正在构建一个简单的表单,它有一个简单的效果——当鼠标不在表单上时,不透明度会降低,当鼠标在表单上时,表单变得不透明。我目前遇到了几个困难:-
Firstly, I did this-
this.MouseHover += new EventHandler(Form1_MouseHover); this.MouseLeave += new EventHandler(Form1_MouseLeave);But I had 1 richtextbox in form too, and as mouse went over it, the form lost opacity again. I had to add this too:-
richTextBox1.MouseHover+=new EventHandler(Form1_MouseHover); richTextBox1.MouseLeave+=new EventHandler(Form1_MouseLeave);wondering if there was any better way,because there is still some gap between richtextbox and form boundaries, and form is losing opacity when mouse cursor goes there.
If the mouse is NOT over the form (suppose initially), the form is less opaque. Now, I want form to become opaque as soon as mouse goes over it, but it only happens when mouse movement over form stops completely. If I keep moving mouse over the form, it does not become opaque. Is this a problem with the way events are stored in message queue and all that or will I be able to do something, because I have seen applications with the effect I am trying to implement.
首先,我做了这个——
this.MouseHover += new EventHandler(Form1_MouseHover); this.MouseLeave += new EventHandler(Form1_MouseLeave);但是我的表单中也有 1 个 Richtextbox,当鼠标经过它时,表单再次失去了不透明度。我也必须添加这个:-
richTextBox1.MouseHover+=new EventHandler(Form1_MouseHover); richTextBox1.MouseLeave+=new EventHandler(Form1_MouseLeave);想知道是否有更好的方法,因为富文本框和表单边界之间仍然存在一些差距,并且当鼠标光标移到那里时,表单会失去不透明度。
如果鼠标不在表单上(假设最初),则表单不透明。现在,我希望表单在鼠标经过时立即变得不透明,但只有当鼠标在表单上的移动完全停止时才会发生。如果我一直在表单上移动鼠标,它不会变得不透明。这是事件存储在消息队列中的方式的问题吗?或者我是否能够做一些事情,因为我已经看到了具有我正在尝试实现的效果的应用程序。
采纳答案by Hans Passant
The MouseEnter/Leave events are too unreliable to do this. Best thing to do is just use a Timer that checks if the mouse is still inside the window. Drop a Timer on the form and make the code look like this:
MouseEnter/Leave 事件太不可靠了,无法做到这一点。最好的办法是使用一个计时器来检查鼠标是否仍在窗口内。在表单上放置一个 Timer 并使代码如下所示:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.Opacity = 0.99;
timer1.Interval = 200;
timer1.Enabled = true;
timer1.Tick += timer1_Tick;
}
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
timer1_Tick(this, e);
}
private void timer1_Tick(object sender, EventArgs e) {
this.Opacity = this.Bounds.Contains(this.PointToClient(Cursor.Position)) ? 0.99 : 0.20;
}
}
Btw: avoid increasing the Opacity to 1.0, that forces the native window to be recreated and that can have a lot of side-effects. Using 0.99 is best.
顺便说一句:避免将 Opacity 增加到 1.0,这会强制重新创建本机窗口,并且会产生很多副作用。最好使用 0.99。
回答by Samy Arous
I might be wrong, but why would you use MouseHover event? MouseHoverdetects when the mouse stop moving on the form and is usually used to show Tooltips.
我可能是错的,但你为什么要使用 MouseHover 事件?MouseHover检测鼠标何时停止在窗体上移动,通常用于显示工具提示。
The event you are looking for is MouseEnterwhich is the opposite of MouseLeaveand detects when the mouse enters the client rect of a window.
您正在寻找的事件是MouseEnter,它与MouseLeave相反,并检测鼠标何时进入窗口的客户端矩形。
In the Leave event, just check if the cursor position is in the window client rect to know if it did actually leave the form or if it is just on top of child control.
在 Leave 事件中,只需检查光标位置是否在窗口客户端矩形中即可知道它是否确实离开了表单,或者它是否只是在子控件的顶部。
Ofc if you use a region, you'll have to adapt the code.
Ofc 如果您使用区域,则必须调整代码。
private void Form1_MouseEnter(object sender, EventArgs e)
{
this.Opacity = 1;
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
if (!this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)))
{
this.Opacity = 0.5;
}
}
回答by Afshin
private void Form1_MouseEnter(object sender, EventArgs e)
{
this.Opacity = 1.0;
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
this.Opacity = 0.8;
}
回答by PUG
Add a timer control then use below in timer's tick event. Above answers won't work if you have custom/user controls in your form. So have to use ClientRectangle
添加一个计时器控件,然后在计时器的滴答事件中使用下面的内容。如果您的表单中有自定义/用户控件,则上述答案将不起作用。所以必须使用ClientRectangle
this.Opacity = this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)) ? 0.99 : 0.20;

