c#輕鬆實現磁性視窗

來源:互聯網
上載者:User

標籤:

/// <summary>
/// 磁性表單函數
/// </summary>
/// <param name="form">表單控制項(一般傳this即可)</param>
/// <param name="space">自訂的與螢幕邊緣的距離</param>
/// <param name="isWorkingArea">是否在螢幕工作區進行該操作(true表示不包括工作列,false則包括整個螢幕的範圍)</param>
public void Form_Welt(Control form, int space, bool isWorkingArea)
{
//擷取表單的左上方的x,y座標
int x = form.Location.X;
int y = form.Location.Y;

int sW = 0;
int sH = 0;

if (isWorkingArea)
{
//擷取螢幕的工作區(不包括工作列)的寬度和高度
sW = Screen.PrimaryScreen.WorkingArea.Width;
sH = Screen.PrimaryScreen.WorkingArea.Height;
}
else
{
//擷取整個螢幕(包括工作列)的寬度和高度
sW = Screen.PrimaryScreen.Bounds.Width;
sH = Screen.PrimaryScreen.Bounds.Height;
}

//如果表單的左邊緣和螢幕左邊緣的距離在使用者定義的範圍內,則執行左貼邊
if ((x <= space && x > 0) || (Math.Abs(x) <= space && x < 0)) //Math.Abs(x)是取絕對值
{
form.Location = new Point(0, y);
}

//如果表單的上邊緣和螢幕上邊緣的距離在使用者定義的範圍內,則執行上貼邊
if ((y <= space && y > 0) || (Math.Abs(y) <= space && y < 0))
{
form.Location = new Point(x, 0);
}


//表單右邊緣跟螢幕右邊緣的距離
int rightW = sW - form.Right;
//表單下邊緣跟螢幕下邊緣的距離
int bottomW = sH - form.Bottom;

//判斷右邊的情況
if ((rightW <= space && form.Right < sW) || (Math.Abs(rightW) <= space && rightW < 0))
{
form.Location = new Point(sW - form.Width, y);
}
//判斷下邊的情況
if ((bottomW <= 10 && form.Bottom < sH) || (Math.Abs(bottomW) <= space && bottomW < 0))
{
form.Location = new Point(x, sH - form.Height);
}
}

 

看到千千靜聽的視窗可以在接近螢幕邊緣時貼在邊緣上覺得不錯,自己也有這個需要,所以寫了這個方法,測試了感覺還蠻不錯的,哈哈~
使用的時候只要在想應用的表單的Form_Move(object sender,EventAges e)事件裡面調用即可
ps:不過有時表單可能會比較閃,這個可能是代碼還有待改善,或者是在Form_Move事件裡面來調用不大合適,反正功能是實現了,要是哪位有更好的方法,歡迎回複交流一下啊~

c#輕鬆實現磁性視窗

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.