c# 實現(xiàn)窗體拖到屏幕邊緣自動隱藏
更新時間:2009年02月14日 14:31:02 作者:
讓窗體拖到屏幕邊緣自動隱藏的原理,通過Form1_LocationChanged的方法,當窗體位置發(fā)生改變時,判斷其是否在屏幕邊緣,在則隱藏。再通過Timer控件經(jīng)過指定時間判斷出鼠標的位置,若鼠標在屏幕左邊、上邊或右邊,這根據(jù)窗體的位置,調(diào)出窗體。
以下給出源代碼: (注:hide為窗體名稱)
private void hide_Load(object sender, EventArgs e)
{
System.Windows.Forms.Timer StopRectTimer = new System.Windows.Forms.Timer();
StopRectTimer.Tick += new EventHandler(timer1_Tick);
StopRectTimer.Interval = 100;
StopRectTimer.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Bounds.Contains(Cursor.Position))
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, 0);
break;
case AnchorStyles.Left:
this.Location = new Point(0, this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
break;
}
}
else
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, (this.Height - 2) * (-1));
break;
case AnchorStyles.Left:
this.Location = new Point((-1) * (this.Width - 2), this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, this.Location.Y);
break;
}
}
}
internal AnchorStyles StopAanhor = AnchorStyles.None;
private void mStopAnhor()
{
if (this.Top <= 0)
{
StopAanhor = AnchorStyles.Top;
}
else if (this.Left <= 0)
{
StopAanhor = AnchorStyles.Left;
}
else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
{
StopAanhor = AnchorStyles.Right;
}
else
{
StopAanhor = AnchorStyles.None;
}
}
private void hide_LocationChanged(object sender, EventArgs e)
{
this.mStopAnhor();
}
復制代碼 代碼如下:
private void hide_Load(object sender, EventArgs e)
{
System.Windows.Forms.Timer StopRectTimer = new System.Windows.Forms.Timer();
StopRectTimer.Tick += new EventHandler(timer1_Tick);
StopRectTimer.Interval = 100;
StopRectTimer.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Bounds.Contains(Cursor.Position))
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, 0);
break;
case AnchorStyles.Left:
this.Location = new Point(0, this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
break;
}
}
else
{
switch (this.StopAanhor)
{
case AnchorStyles.Top:
this.Location = new Point(this.Location.X, (this.Height - 2) * (-1));
break;
case AnchorStyles.Left:
this.Location = new Point((-1) * (this.Width - 2), this.Location.Y);
break;
case AnchorStyles.Right:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, this.Location.Y);
break;
}
}
}
internal AnchorStyles StopAanhor = AnchorStyles.None;
private void mStopAnhor()
{
if (this.Top <= 0)
{
StopAanhor = AnchorStyles.Top;
}
else if (this.Left <= 0)
{
StopAanhor = AnchorStyles.Left;
}
else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
{
StopAanhor = AnchorStyles.Right;
}
else
{
StopAanhor = AnchorStyles.None;
}
}
private void hide_LocationChanged(object sender, EventArgs e)
{
this.mStopAnhor();
}
相關文章
C#開發(fā)Windows服務實例之實現(xiàn)禁止QQ運行
這篇文章主要介紹了通過C#開發(fā)Windows服務,查殺qq進程的服務功能,需要的朋友可以參考下2013-10-10C#常用數(shù)據(jù)結(jié)構(gòu)和算法總結(jié)
這篇文章主要介紹了C#常用數(shù)據(jù)結(jié)構(gòu)和算法,這里我們總結(jié)了一些知識點,可以幫助大家理解這些概念。2016-06-06一看就懂:圖解C#中的值類型、引用類型、棧、堆、ref、out
這篇文章主要介紹了一看就懂:圖解C#中的值類型、引用類型、棧、堆、ref、out,本文用淺顯易懂的語言組織介紹了這些容易混淆的概念,需要的朋友可以參考下2015-06-06