日期:2014-05-18  浏览次数:20914 次

关于 窗体(WINFORM) 事件 MouseLeave 的问题。
我在程序里,想使用 frm_MouseLeave(object sender, EventArgs e) 事件,想实现 在 鼠标 离开 窗体 的时候,让窗体自动收缩到屏幕上面,即 this.Location = new Point(this.Location.X, -this.Size.Height + 5)。

目前出现的问题是:

当我鼠标移动到 窗体(frm) 上的 图片(pictureBox) 控件上,就会立刻触发 frm_MouseLeave 事件,而不是移动到 窗体(frm)之外触发。

因此我想,能不能有一个事件,能够对窗体整体做触发,而不是遇到 窗体内的控件就 触发,否则我就控制不住窗体的自动收缩。

谢谢大家!

------解决方案--------------------
1)估计还要一个控制变量
2)判断引发事件的sender是form还是pictureBox
------解决方案--------------------
取屏幕坐标判断

 Rectangle _Info = new Rectangle(Location.X, Location.Y, this.Width, this.Height);
if (_Info.Contains(Cursor.Position))
{
this.Text = "ok";
}
else
{
this.Text = "no";
}
------解决方案--------------------
根据你上面说的那样
如果鼠标从pictureBox(等)直接出去是不会触发FORM的MouseLeave 的
------解决方案--------------------
帮顶!
------解决方案--------------------
你别用事件判断了~~时间控件看看能解决不?
------解决方案--------------------
C# code

private void frmZhiMa_MouseLeave(object sender, EventArgs e)
{
    if (!this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)))  //<--
    {
        if (this.Location.Y < 20)
        {
             this.Location = new Point(this.Location.X, -this.Size.Height + 5);
        }
    }
}

------解决方案--------------------
在TabControl 的MouseLeave里面一样的判断。
C# code

public FormTest()
        {
            InitializeComponent();
            tabControl1.MouseLeave += new EventHandler(tabControl1_MouseLeave);
        }

        void tabControl1_MouseLeave(object sender, EventArgs e)
        {
            if (!this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)))
            {
                System.Console.WriteLine("move out tabControl1_MouseLeave");
            }
        }