窗体吸附 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;
}
}
}

}

posted @ 2011-05-26 09:49  把爱延续  阅读(782)  评论(2编辑  收藏  举报