Winform - 确定鼠标是否离开了用户控制 [英] Winform - determine if mouse has left user control

查看:45
本文介绍了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) 我该如何编码,因为 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.

谢谢

编辑

我试图找出鼠标指针,但没有到达那里.下面的代码在 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(其父级)如何相对于屏幕具有更大的 Y 值.

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

    推荐答案

    挂钩所有控件 MouseEnter 和 MouseLeave 事件,然后弄清楚它是否仍在表单中是非常痛苦的.一个简单的计时器也可以完成工作:

    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天全站免登陆