C#中让窗体自动靠边隐藏


1:  private void Yincangtimer_Tick(object sender, EventArgs e)//窗体隐藏事件
   2:          {
   3:              int a = Control.MousePosition.Y;//光标的在屏幕中的 Y 坐标
   4:              int b = Control.MousePosition.X;//光标的在屏幕中的 X 坐标
   5:              int height = Screen.PrimaryScreen.WorkingArea.Height;//屏幕的高
   6:              int width = Screen.PrimaryScreen.WorkingArea.Width;//屏幕的宽
   7:              int x = this.Left;//窗体的在屏幕中的X坐标
   8:              int y = this.Top;//窗体的在屏幕中的Y坐标
   9:              //判断光标是否在窗体内
  10:              if (b >= x && b <= (this.Width + x) && a >= y && a <= (this.Top + this.Height))
  11:              {
  12:                  return;
  13:              }
  14:              else
  15:              {//隐藏窗体
  16:                  if ((x + this.Width) >= width)
  17:                  {
  18:                      this.Top = this.Top;
  19:                      this.Left = width - 5;
  20:                  }
  21:                  else if (x <= 0)
  22:                  {
  23:                      this.Top = this.Top;
  24:                      this.Left = 5 - this.Width;
  25:                  }
  26:                  else if (y <= 0)
  27:                  {
  28:                      this.Left = this.Left;
  29:                      this.Top = 5 - this.Height;
  30:   
  31:                  }
  32:                  else
  33:                  {
  34:   
  35:                      return;
  36:                  }
  37:              }
  38:          }
  39:   
  40:  //光标离开窗体
  41:          private void MainForm_MouseLeave(object sender, EventArgs e)
  42:          {
  43:              Yincangtimer.Start();
  44:          }
  45:          //光标进入窗体
  46:          private void MainForm_MouseEnter(object sender, EventArgs e)
  47:          {
  48:              int height = Screen.PrimaryScreen.WorkingArea.Height;//屏幕的高
  49:              int width = Screen.PrimaryScreen.WorkingArea.Width;//屏幕的宽
  50:         
  51:              if (this.Left < 0)
  52:              {
  53:                  this.Left = 0;
  54:                  this.Top = this.Top;
  55:                  Yincangtimer.Stop();
  56:              }
  57:              else if (this.Left > width - this.Width)
  58:              {
  59:                  this.Left = width - this.Width;
  60:                  this.Top = this.Top;
  61:                  Yincangtimer.Stop();
  62:              }
  63:              else if (this.Top <= 0)
  64:              {
  65:                  this.Left = this.Left;
  66:                  this.Top = 0;
  67:                  Yincangtimer.Stop();
  68:              }
  69:   
  70:              else
  71:              { return; }
  72:          }
原文地址:https://www.cnblogs.com/sczw-maqing/p/3259158.html