MouseHover和鼠标离开事件控制 [英] MouseHover and MouseLeave Events controlling

查看:229
本文介绍了MouseHover和鼠标离开事件控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是建立一个简单的表格用一个简单的效力不透明度时,鼠标还没有结束的形式减少,当鼠标移动到它的形式变得不透明。我目前遇到两个困难: -




  1. 首先,我这样做 -

      this.MouseHover + =新的EventHandler(Form1_MouseHover); 
    this.MouseLeave + =新的EventHandler(Form1_MouseLeave);



    但我不得不在形式上1 RichTextBox中也和鼠标去了它,形式再度迷失透明度。我不得不添加这也: -

      richTextBox1.MouseHover + =新的EventHandler(Form1_MouseHover); 
    richTextBox1.MouseLeave + =新的EventHandler(Form1_MouseLeave);



    想知道是否有什么更好的办法,因为还有RichTextBox的和形式的界限之间的一些差距,当鼠标指针变为有形式正在失去透明度。


  2. 如果鼠标是不是在形式(假设最初),形式较少不透明的。现在,我想的形式尽快鼠标越过它变得不透明,但是,当重于形式的鼠标移动完全停止它才会发生。如果我继续在移动形式的鼠标,它不会变得不透明。这与方法的事件和问题都存储在消息队列中的所有或我可以做一些事情,因为我已经看到了我想实现的效果的应用程序。



解决方案

在的MouseEnter / Leave事件是太不可靠做到这一点。最好的办法是只使用来检查,如果鼠标仍然是窗口内的计时器。在表单上放一个计时器,使代码看起来是这样的:

 公共部分Form1类:表格{
公Form1的(){
的InitializeComponent();
this.Opacity = 0.99;
timer1.Interval = 200;
timer1.Enabled = TRUE;
timer1.Tick + = timer1_Tick;
}
保护覆盖无效的OnLoad(EventArgs的发送){
base.OnLoad(E);
timer1_Tick(这一点,E);
}
私人无效timer1_Tick(对象发件人,EventArgs五){
this.Opacity = this.Bounds.Contains(this.PointToClient(Cursor.Position))? 0.99:0.20;
}
}



BTW:避免增加不透明度为1.0,即力量重新创建本机窗口,可以有很多的副作用。使用0.99是最好的。


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:-

  1. 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.

  2. 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.

解决方案

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:

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.

这篇关于MouseHover和鼠标离开事件控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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