Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

winforms - How to detect when the mouse leaves the form?

I have a form with a lot of controls on it. How can I detect when the mouse leaves the form? I've tried wiring up a MouseLeave event for every single control and the form, but that does not work because those events fire all the time as the mouse passes over controls. Is there a way that actually works.?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The only reliable way I know of is a timer. Here's sample code that tweaks the opacity on a roll-over:

  public partial class Form1 : Form {
    Timer timer1 = new Timer();
    public Form1() {
      InitializeComponent();
      this.Opacity = 0.10;
      timer1.Tick += new EventHandler(timer1_Tick);
      timer1.Interval = 200;
      timer1.Enabled = true;
    }

    void timer1_Tick(object sender, EventArgs e) {
      Point pos = Control.MousePosition;
      bool inForm = pos.X >= Left && pos.Y >= Top && pos.X < Right && pos.Y < Bottom;
      this.Opacity = inForm ? 0.99 : 0.10;
    }
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...