我在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;
            }
        }

解决方案 »

  1.   

    这是我以前写的代码,你尽管用吧,比你的简单多了: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;
    }
    }
    }
    }
      

  2.   

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

  3.   

    不过我觉得有点小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;
                        }
                    }
                }
            }