如何实现WinForms窗体隐藏

来源:互联网 发布:网络萨顶顶歌曲大全 编辑:程序博客网 时间:2024/04/30 15:36

如QQ程序,窗体在桌面边界失去焦点时,窗体自动隐藏到桌面边缘,当鼠标移动到隐藏的窗体上时,窗体又会显示出来,可以用以下方法实现:

[c-sharp] view plaincopy
  1. // 当窗体的位置发生改变时,定位窗体的位置  
  2. internal AnchorStyles StopAanhor = AnchorStyles.None;  
  3. private void mStopAnhor()  
  4. {  
  5.     if(this.Top <= 0 && this.Left <= 0)  
  6.     {  
  7.          StopAanhor = AnchorStyles.None;  
  8.     }  
  9.     else if (this.Top <= 0)  
  10.     {  
  11.         StopAanhor = AnchorStyles.Top;  
  12.     }  
  13.     else if (this.Left <= 0)  
  14.     {  
  15.         StopAanhor = AnchorStyles.Left;  
  16.     }  
  17.     else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)  
  18.     {  
  19.          StopAanhor = AnchorStyles.Right;  
  20.     }  
  21.     else if (this.Top >= Screen.PrimaryScreen.Bounds.Height - this.Height)  
  22.     {  
  23.        StopAanhor = AnchorStyles.Bottom;  
  24.     }  
  25.     else  
  26.     {  
  27.        StopAanhor = AnchorStyles.None;  
  28.     }  
  29. }  
  30.   
  31. // 当窗体的位置改变时  
  32. private void MainForm_LocationChanged(object sender, EventArgs e)  
  33. {  
  34.     this.mStopAnhor();  
  35. }  
  36. /// <summary>  
  37. /// 时间控件,控制窗体的坐标  
  38. /// </summary>  
  39. /// <param name="sender"></param>  
  40. /// <param name="e"></param>  
  41. private void timer1_Tick(object sender, EventArgs e)  
  42. {  
  43.      if (this.Bounds.Contains(Cursor.Position))  
  44.      {  
  45.           switch (this.StopAanhor)  
  46.           {  
  47.              case AnchorStyles.Top:  
  48.                    //窗体在最上方隐藏时,鼠标接触自动出现  
  49.                       this.Location = new Point(this.Location.X, 0);  
  50.                    break;  
  51.                    //窗体在最左方隐藏时,鼠标接触自动出现  
  52.                case AnchorStyles.Left:  
  53.                    this.Location = new Point(0, this.Location.Y);  
  54.                    break;  
  55.                    //窗体在最右方隐藏时,鼠标接触自动出现  
  56.                case AnchorStyles.Right:  
  57.                    this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);  
  58.                    break;  
  59.            }  
  60.       }  
  61.       else  
  62.       {  
  63.            //窗体隐藏时在靠近边界的一侧边会出现2像素原因:感应鼠标,同时2像素不会影响用户视线  
  64.              switch (this.StopAanhor)  
  65.            {  
  66.                //窗体在顶部时时,隐藏在顶部,底部边界出现2像素  
  67.                 case AnchorStyles.Top:  
  68.                    this.Location = new Point(this.Location.X, (this.Height - 2) * (-1));  
  69.                    break;  
  70.                    //窗体在最左边时时,隐藏在左边,右边边界出现2像素  
  71.                 case AnchorStyles.Left:  
  72.                    this.Location = new Point((-1) * (this.Width - 2), this.Location.Y);  
  73.                    break;  
  74.                    //窗体在最右边时时,隐藏在右边,左边边界出现2像素  
  75.                 case AnchorStyles.Right:  
  76.                    this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, this.Location.Y);  
  77.                    break;  
  78.            }  
  79.       }  
  80.   
  81. }  

0 0