我想做到QQ一样自动隐藏

xiaoxiao198327 2006-10-20 09:27:46
我在FORM的MOVELEAVE和MOVEENTER中分别写了如下代码,但是好像不能做到很好,麻烦大家帮我改一下:
private void MainForm_MouseHover(object sender, EventArgs e) {
//窗体靠左边隐藏的时候
if (this.Left == 5 - this.Width) {
while (this.Left < 0) {
this.Left++;
}
return;
}

if (this.Left == Screen.PrimaryScreen.WorkingArea.Width - 5) {
while (this.Left > Screen.PrimaryScreen.WorkingArea.Width - this.Width) {
this.Left--;
}
return;
}

if (this.Top == 5 - this.Height) {
while (this.Top < 0) {
this.Top++;
}
return;
}
}

private void MainForm_MouseLeave(object sender, EventArgs e) {
if (this.Top <= 0) {
if (this.Left <=0) {
this.Top = 0;
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Left = 5 - this.Width;
return;
} else if (this.Left >= Screen.PrimaryScreen.WorkingArea.Width - this.Width + 5) {
this.Top = 0;
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Left = Screen.PrimaryScreen.WorkingArea.Width - 5;
return;
} else {
this.Top = 5 - this.Height;
return;
}
}

if (this.Left <= 0) {
this.Top = 0;
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Left = 5 - this.Width;
return;
}

if (this.Left >= Screen.PrimaryScreen.WorkingArea.Width - this.Width + 5) {
this.Top = 0;
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Left = Screen.PrimaryScreen.WorkingArea.Width - 5;
return;
}


}
...全文
352 14 打赏 收藏 举报
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zhangzh2003 2007-04-20
  • 打赏
  • 举报
回复
mark
yuanzhihua520 2007-01-11
  • 打赏
  • 举报
回复
up
minghw 2006-12-11
  • 打赏
  • 举报
回复
very NB
cswry 2006-12-11
  • 打赏
  • 举报
回复
学习了,厉害啊
huangchengjin630 2006-11-05
  • 打赏
  • 举报
回复
还是不行啊,奇怪,这段代码应该贴在哪里
-过客- 2006-10-31
  • 打赏
  • 举报
回复
mark
ycqing 2006-10-31
  • 打赏
  • 举报
回复
aaaz
luting199 2006-10-31
  • 打赏
  • 举报
回复
顶,厉害./
zhangzengping 2006-10-31
  • 打赏
  • 举报
回复
mark
careast 2006-10-20
  • 打赏
  • 举报
回复
不过我觉得有点小BUG,就是把窗体拉过上边缘和右边缘或左边缘的话,就会露个角而不是之前的边缘,再显示的时候就不会完全显示。我给出解决方法就是如下:
private void timer1_Tick(object sender, EventArgs e) {
if (this.WindowState != System.Windows.Forms.FormWindowState.Minimized) {
if (Cursor.Position.X > this.Left && Cursor.Position.X < this.Right && Cursor.Position.Y > this.Top && Cursor.Position.Y < this.Bottom) {
if (this.Top < 0) {
this.Top = -5;
this.Show();
} else if (this.Left < 0) {
this.Left = -5;
this.Show();
} else if (this.Left + this.Width >= Screen.PrimaryScreen.WorkingArea.Width) {
this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width + 5;
this.Show();
}
} else {
/******************变化处******************/
if (this.Top <= 4) {
this.Top = 5 - this.Height;
if (this.Left <= 4) {
this.Left =-5;
} else if (this.Left + this.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4) {
this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width + 5;
}
/******************变化处******************/
} else if (this.Left <= 4) {
this.Left = 5 - this.Width;
} else if (this.Left + this.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4) {
this.Left = Screen.PrimaryScreen.WorkingArea.Width - 5;
}
}
}
}
careast 2006-10-20
  • 打赏
  • 举报
回复
dugupiaoyun(独孤飘云)兄的思路,我来解释一下:

///FORM中有个计时器,每隔多少秒轮询一下鼠标的位置,
///在FORM没有最小化的情况下的情况下,
///根据鼠标在FORM上不同的为位置选择FORM的显示和隐藏
///1、如果鼠标在FORM中而且FORM靠屏幕边缘的话,则显示
///2、如果鼠标在FORM外而且FORM靠屏幕边缘的话,则隐藏
private void timer1_Tick(object sender, System.EventArgs e)
{
//FORM没有最小化的情况下
if (this.WindowState!=System.Windows.Forms.FormWindowState.Minimized)
{
if (Cursor.Position.X>this.Left && Cursor.Position.X<this.Right && Cursor.Position.Y>this.Top && Cursor.Position.Y<this.Bottom)
{//鼠标的位置在FORM之内,则显示
if (this.Top<0)
{//如果FORM靠近上边缘
this.Top=-5;
this.Show();
}
else if (this.Left<0)
{//如果FORM靠近左边缘
this.Left=-5;
this.Show();
}
else if (this.Left+this.Width>=Screen.PrimaryScreen.WorkingArea.Width)
{//如果FORM靠近右边缘 this.Left=Screen.PrimaryScreen.WorkingArea.Width-this.Width+5;
this.Show();
}
}
else
{//鼠标的位置在FORM之外,则隐藏
if(this.Top<=4)
{//如果FORM靠近上边缘
this.Top=5-this.Height;
}
else if(this.Left<=4)
{//如果FORM靠近左边缘
this.Left=5-this.Width;
}
else if(this.Left+this.Width>=Screen.PrimaryScreen.WorkingArea.Width-4)
{//如果FORM靠近右边缘 this.Left=Screen.PrimaryScreen.WorkingArea.Width-5;
}
}
}
}
xiaoxiao198327 2006-10-20
  • 打赏
  • 举报
回复
能加一下注释吗?最好详细一点,谢谢
dugupiaoyun 2006-10-20
  • 打赏
  • 举报
回复
这是我以前写的代码,你尽管用吧,比你的简单多了:

private void timer1_Tick(object sender, System.EventArgs e)
{
if (this.WindowState!=System.Windows.Forms.FormWindowState.Minimized)
{
if (Cursor.Position.X>this.Left && Cursor.Position.X<this.Right && Cursor.Position.Y>this.Top && Cursor.Position.Y<this.Bottom)
{
if (this.Top<0)
{
this.Top=-5;
this.Show();
}
else if (this.Left<0)
{
this.Left=-5;
this.Show();
}
else if (this.Left+this.Width>=Screen.PrimaryScreen.WorkingArea.Width)
{
this.Left=Screen.PrimaryScreen.WorkingArea.Width-this.Width+5;
this.Show();
}
}
else
{
if(this.Top<=4)
{
this.Top=5-this.Height;
}
else if(this.Left<=4)
{
this.Left=5-this.Width;
}
else if(this.Left+this.Width>=Screen.PrimaryScreen.WorkingArea.Width-4)
{
this.Left=Screen.PrimaryScreen.WorkingArea.Width-5;
}
}
}
}
股神 2006-10-20
  • 打赏
  • 举报
回复
Up
发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2006-10-20 09:27
社区公告

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