Winform的 - 判断鼠标已经离开的用户控件 [英] Winform - determine if mouse has left user control

查看:212
本文介绍了Winform的 - 判断鼠标已经离开的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用,我认为应该很容易(ISH)的东西挣扎。我有一个windows窗体,并在窗体底部的flowgridlayout面板。这里面我表单与动态用户控件的X个填充它。的控制都是相同的类型。

I am struggling with something that I think should be easily (ish). I have a windows form and a flowgridlayout panel at the bottom of the form. Inside this form I dynamically populate it with X number of User Controls. The controls are all the same type.

的目的是当用户在它打开的另一种形式并且其中所述小鼠是定位它的用户控制真空吸尘器的鼠标。当鼠标离开的形式打开的表单中消失。

The goal is when the user hoovers the mouse over the user control it opens another form and positions it where the mouse is. When mouse leaves the form the opened form disappears.

这几乎伟大工程。的问题是当用户控制具有象其内部的标签或文本框任何东西。它被认为已经离开在UC这样的形式消失。

This almost works great. The problem is when the User Control has anything like a label or text box inside it. It is considered to have left the UC so the form disappears.

我的想法是再使用X和Y告诉如果是UC里面,但我不知道这一点。

My thought was then to use the X and Y to tell if it is inside the UC but I can not figure this out.

我可以问:

1)什么是解决这个问题的最好的方法?
2)我如何code将其作为UC的是动态的,我不能确切地知道他们会。

1) What is the best approach to this? 2) How can I code it, as the UC's are dynamic I can not know exactly where they will be.

感谢

修改

我试图找出鼠标指针,但是没有到达那里。在code以下是UC SmallTagBox_MouseLeave事件中:

I am trying to figure out the mouse pointers but not getting there. The code below is within the UC SmallTagBox_MouseLeave event:

        Point loc = this.Location;
        Point p = this.PointToScreen(this.Location);
        Point p2 = this.PointToScreen(this.Parent.Location);

        Point ms = MousePosition;
        Rectangle screenBounds = new Rectangle(this.PointToScreen(this.Location), this.Size);
        if (!screenBounds.Contains(ms))
        {
            thw.Close();
            thw = null;
        }


  • LOC {X = 275 Y = 3} System.Drawing.Point

  • p {X = 808 Y = 908} System.Drawing.Point

  • p {X = 808 Y = 908} System.Drawing.Point

  • P2 {X = 545 Y = 1542} System.Drawing.Point

  • MS {X = 574 Y = 914} System.Drawing.Point

  • screenBounds {X = 808 Y = 908宽度= 62高度= 29} System.Drawing.Rectangle

  • 我不明白P2(其父)怎么能有更大的Ÿ相对于屏幕的值。

    I do not understand how p2 (its parent) can have a greater Y value relative to the screen.

    推荐答案

    挂钩的所有控件的MouseEnter和鼠标离开事件,然后搞清楚,如果它仍然是形式的内部pretty痛苦。一个简单的定时器可以完成这项工作过:

    Hooking all the controls MouseEnter and MouseLeave events, then figuring out if it is still inside the form is pretty painful. A simple timer can get the job done too:

      public partial class Form1 : Form {
        private Timer mTimer;
        public Form1() {
          InitializeComponent();
          mTimer = new Timer();
          mTimer.Interval = 200;
          mTimer.Tick += mTimer_Tick;
          mTimer.Enabled = true;
        }
        private void mTimer_Tick(object sender, EventArgs e) {
          if (!this.DesktopBounds.Contains(Cursor.Position)) this.Close();
        }
      }
    

    这篇关于Winform的 - 判断鼠标已经离开的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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