在我們將Winform自帶的邊框隱藏之后,我們需要自己編寫窗口的移動。
思路就是
1.獲得點擊左鍵時當前鼠標的坐標
2.獲得移動后鼠標的坐標
3.窗體的坐標=移動后的鼠標坐標-移動前的鼠標坐標
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
private Point mouseOff; //鼠標移動位置變量 private bool leftFlag; //鼠標是否為左鍵 private void Form1_MouseDown( object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mouseOff = new Point(-e.X, -e.Y); //獲得當前鼠標的坐標 leftFlag = true ; } } private void Form1_MouseMove( object sender, MouseEventArgs e) { if (leftFlag) { Point mouseSet = Control.MousePosition; //獲得移動后鼠標的坐標 mouseSet.Offset(mouseOff.X, mouseOff.Y); //設置移動后的位置 Location = mouseSet; } } private void Form1_MouseUp( object sender, MouseEventArgs e) { if (leftFlag) { leftFlag = false ; } } |
以上這篇C#Winform窗口移動方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/Maybe_ch/article/details/81482054