应该有很多方法可以实现,这里介绍两种方法:一种方法就是自己编程实现窗口的位置随鼠标改变;另一种就是直接利用Windows的API。
设计一个窗体时,把以下代码加入到窗口中,就可以实现这个功能:
1
private
bool
IsMouseDownInForm
=
false
;
2 private Point p;
3 private void Form1_MouseDown( object sender, MouseEventArgs e)
4 {
5 IsMouseDownInForm = true ;
6 p = e.Location;
7 }
8 private void Form1_MouseUp( object sender, MouseEventArgs e)
9 {
10 IsMouseDownInForm = false ;
11 }
12 private void Form1_MouseMove( object sender, MouseEventArgs e)
13 {
14 if (IsMouseDownInForm)
15 {
16 Left += e.Location.X - p.X;
17 Top += e.Location.Y - p.Y;
18 }
19 }
2 private Point p;
3 private void Form1_MouseDown( object sender, MouseEventArgs e)
4 {
5 IsMouseDownInForm = true ;
6 p = e.Location;
7 }
8 private void Form1_MouseUp( object sender, MouseEventArgs e)
9 {
10 IsMouseDownInForm = false ;
11 }
12 private void Form1_MouseMove( object sender, MouseEventArgs e)
13 {
14 if (IsMouseDownInForm)
15 {
16 Left += e.Location.X - p.X;
17 Top += e.Location.Y - p.Y;
18 }
19 }
或者
1
private
Point mouse_offset;
2 private void Form1_MouseDown( object sender, MouseEventArgs e)
3 {
4 mouse_offset = new Point( - e.X, - e.Y);
5 }
6
7 private void Form1_MouseMove( object sender, MouseEventArgs e)
8 {
9 if (e.Button == MouseButtons.Left)
10 {
11 Point mousePos = Control.MousePosition;
12 mousePos.Offset(mouse_offset.X, mouse_offset.Y);
13 Location = mousePos;
14 }
15 }
2 private void Form1_MouseDown( object sender, MouseEventArgs e)
3 {
4 mouse_offset = new Point( - e.X, - e.Y);
5 }
6
7 private void Form1_MouseMove( object sender, MouseEventArgs e)
8 {
9 if (e.Button == MouseButtons.Left)
10 {
11 Point mousePos = Control.MousePosition;
12 mousePos.Offset(mouse_offset.X, mouse_offset.Y);
13 Location = mousePos;
14 }
15 }
当然,也可以设计一个窗口基类,override Form的OnMouseMove、OnMouseUp、OnMouseDown方法,实现这个小功能。
第二种方法是利用Windows的API,熟悉Windows API编程的人应该很容易就理解了。
首先,利用平台调用,引入User32.dll中以下两个函数的调用:
1
using
System.Runtime.InteropServices;
2 public const int WM_NCLBUTTONDOWN = 0xa1 ;
3 public const int HT_CAPTION = 0x2 ;
4 [DllImportAttribute( " user32.dll " )]
5 public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
6 [DllImportAttribute( " user32.dll " )]
7 public static extern bool ReleaseCapture();
8 然后,你只需要在窗口的MouseDown事件处理中加入以下代码就可以了:
9 private void Form1_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e)
10 {
11 if (e.Button == MouseButtons.Left)
12 {
13 ReleaseCapture();
14 SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0 );
15 }
16 }
2 public const int WM_NCLBUTTONDOWN = 0xa1 ;
3 public const int HT_CAPTION = 0x2 ;
4 [DllImportAttribute( " user32.dll " )]
5 public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
6 [DllImportAttribute( " user32.dll " )]
7 public static extern bool ReleaseCapture();
8 然后,你只需要在窗口的MouseDown事件处理中加入以下代码就可以了:
9 private void Form1_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e)
10 {
11 if (e.Button == MouseButtons.Left)
12 {
13 ReleaseCapture();
14 SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0 );
15 }
16 }