窗体吸附 Timer + 判断Location (简单实用)

//实现接近屏幕窗体边源吸附
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;
}
}
}

}



返回导读目录,阅读更多随笔



分割线,以下为博客签名:

软件臭虫情未了
  • 编码一分钟
  • 测试十年功


随笔如有错误或不恰当之处、为希望不误导他人,望大侠们给予批评指正。

原文地址:https://www.cnblogs.com/08shiyan/p/2057837.html