关于 窗体(WINFORM) 事件 MouseLeave 的问题。

lwlfree 2008-12-24 11:19:47
我在程序里,想使用 frm_MouseLeave(object sender, EventArgs e) 事件,想实现 在 鼠标 离开 窗体 的时候,让窗体自动收缩到屏幕上面,即 this.Location = new Point(this.Location.X, -this.Size.Height + 5)。

目前出现的问题是:

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

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

谢谢大家!
...全文
568 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
liujb526 2008-12-25
  • 打赏
  • 举报
回复
呵呵,学习
flyjimi 2008-12-25
  • 打赏
  • 举报
回复
在TabControl 的MouseLeave里面一样的判断。

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");
}
}
lwlfree 2008-12-25
  • 打赏
  • 举报
回复
现在剩下最后一步了, 由于我的窗体最下部分 使用的 是一个 TabControl ,大约有 2/3 窗体大小,而且是把窗体2/3全部遮盖了,因此,上半部分 1/3 处,使用 MouseLeave 是完全没有问题的,而如果鼠标 从 下半部分 即 被TC遮盖住的那部分出去的话,是无法触发 MouseLeave 事件的,因此窗体也就无法向上缩回去。

我想知道,可以使用其他的什么方法来解决此(下半部分)的问题呢?

使用TIMER的话可以解决此问题,但是 如果 窗体缩回去之后,我从 WINDOWS 的右下托盘处 点击 托盘图标,窗体就出不来了~~~因此还需要寻求其他解决方法。再次感谢大家,再加100分。
lwlfree 2008-12-25
  • 打赏
  • 举报
回复
感谢 zgke , gomoku 给予的帮助!感谢大家!
子夜1978 2008-12-25
  • 打赏
  • 举报
回复
学习下!!
gomoku 2008-12-25
  • 打赏
  • 举报
回复

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);
}
}
}


不要用this.Bounds(也就是new Rectangle(Location.X, Location.Y, this.Width, this.Height))来判断。

由于有边框等,客户区(this.ClientRectangle)要比整个窗口要小一些。
MouseLeave在鼠标离开ClientRectangle就触发了,而这时候,鼠标在ClientRectangle外,却可能还在窗口内。
这就是为什么你在3楼的if (!_Info.Contains(Cursor.Position))有时候返回false的原因。

用ClientRectangle来判断。
lwlfree 2008-12-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lwlfree 的回复:]
刚到公司,顶起来,恳请大家继续帮助小弟,谢谢!
[/Quote]

您是说使用 TIMER 控件吗?时间设置为 100 毫秒,对吗?
zgke 2008-12-25
  • 打赏
  • 举报
回复
你别用事件判断了~~时间控件看看能解决不?
lwlfree 2008-12-25
  • 打赏
  • 举报
回复
刚到公司,顶起来,恳请大家继续帮助小弟,谢谢!
lwlfree 2008-12-25
  • 打赏
  • 举报
回复
最终的解决方案:


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



private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
if (!this.Visible)
{
this.Show();
}
if (this.Location.Y == -this.Size.Height + 5)
{
this.Location = new Point(this.Location.X, 0);
}
timer1.Enabled = false;
}


然后在 MouseEnter 时间中加入了:timer1.Enabled = true;

目前运转正常,结贴,再次感谢大家的给予的帮助!!!特别鸣谢 zgke 、 gomoku 给予的帮助!
ijwsoft 2008-12-25
  • 打赏
  • 举报
回复
建议你用FORM_CLOSING事件
zgke 2008-12-24
  • 打赏
  • 举报
回复
取屏幕坐标判断

Rectangle _Info = new Rectangle(Location.X, Location.Y, this.Width, this.Height);
if (_Info.Contains(Cursor.Position))
{
this.Text = "ok";
}
else
{
this.Text = "no";
}
长沙三毛 2008-12-24
  • 打赏
  • 举报
回复
1)估计还要一个控制变量
2)判断引发事件的sender是form还是pictureBox
lwlfree 2008-12-24
  • 打赏
  • 举报
回复
回4楼,pictureBox 在 FORM 内,与 FORM 的边缘还有20个像素左右的举例,呈 “回” 字型,因此无论怎样,都会经过 FORM 上。

感谢大家的答疑~~~恳请大家继续帮助小弟,谢谢!
python二级题库 2008-12-24
  • 打赏
  • 举报
回复
帮顶!
waiwenlianxi 2008-12-24
  • 打赏
  • 举报
回复
根据你上面说的那样
如果鼠标从pictureBox(等)直接出去是不会触发FORM的MouseLeave 的
lwlfree 2008-12-24
  • 打赏
  • 举报
回复
感谢楼上二位,我采用了 zgke 的方式,在 MouseLeave 事件中,加入了上述代码,果然 可以 避开 pictureBox,但是当我离开窗体的时候,有时候有点失灵,无法收回,但是有时候却可以收回,不知道原因出在哪里?



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


还请各位帮忙,寻求解决方案。

110,565

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧