之前介绍的C#实现用户区左键拖动窗体效果(非标题栏)是通过鼠标点着窗体空白区域然后来拖动窗体,这里介绍的是在C#中鼠标点着某个控件不放来拖动整个窗体。
首先引入命名空间
using System.Runtime.InteropServices;
#region 点击控件移动窗体 [DllImportAttribute("user32.dll")] private extern static bool ReleaseCapture(); [DllImportAttribute("user32.dll")] private extern static int SendMessage(IntPtr handle, int m, int p, int h); #endregion private void label1_MouseDown(object sender, MouseEventArgs e) { //点击控件移动窗体 if (e.Button == MouseButtons.Left) { Cursor= Cursors.SizeAll;//改变鼠标样式 ReleaseCapture(); SendMessage(this.Handle, 0xA1, 0x2, 0); Cursor = Cursors.Default; } }