C#去邊框以及無邊框窗體移動


1.去邊框  :

1) 選中要去除邊框的窗體,按F4調出窗體屬性。

2)在屬性框中找到FormBorderStyle選擇none。

2.去掉邊框后實現對窗口程序的拖動

1)雙擊窗體,進入程序設計界面 添加  using System.Runtime.InteropServices; 

在 { InitializeComponent();  } 后添加以下代碼


[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);


bool beginMove = false;//初始化鼠標位置
int currentXPosition;
int currentYPosition;


2)回到窗體界面,點擊

如圖所示:屬性框上部有個小閃電,點擊進入 事件頁面

 

 

2)分別點擊進入MouseDown 、 MouseMove、MouseUp 並分別添加以下代碼


//獲取鼠標按下時的位置
private void loginForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
beginMove = true;
currentXPosition = MousePosition.X;//鼠標的x坐標為當前窗體左上角x坐標
currentYPosition = MousePosition.Y;//鼠標的y坐標為當前窗體左上角y坐標
}
}
//獲取鼠標移動到的位置
private void loginForm_MouseMove(object sender, MouseEventArgs e)
{
if (beginMove)
{
this.Left += MousePosition.X - currentXPosition;//根據鼠標x坐標確定窗體的左邊坐標x
this.Top += MousePosition.Y - currentYPosition;//根據鼠標的y坐標窗體的頂部,即Y坐標
currentXPosition = MousePosition.X;
currentYPosition = MousePosition.Y;
}
}

//釋放鼠標時的位置
private void loginForm_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
currentXPosition = 0; //設置初始狀態
currentYPosition = 0;
beginMove = false;
}
}

3.測試你的窗體程序吧
---------------------
作者:研途路
來源:CSDN
原文:https://blog.csdn.net/LPVeneno/article/details/53507112
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM