方法一:API调用
//添加引用 using System.Runtime.InteropServices; [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); void FrmWeather_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { ReleaseCapture(); SendMessage(Handle, 0xA1, 0x02, 0); } } //调用 //MouseDown += new MouseEventHandler(FrmWeather_MouseDown);
方法二:
//鼠标移动位置变量 Point mouseOff; //标签是否为左键 bool leftFlag; private void Form_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { //得到变量的值 mouseOff = new Point(-e.X, -e.Y); //点击左键按下时标注为true; leftFlag = true; } } private void Form_MouseMove(object sender, MouseEventArgs e) { if (leftFlag) { Point mouseSet = Control.MousePosition; //设置移动后的位置 mouseSet.Offset(mouseOff.X, mouseOff.Y); Location = mouseSet; } } private void Form_MouseUp(object sender, MouseEventArgs e) { if (leftFlag) { //释放鼠标后标注为false; leftFlag = false; } } //调用 // MouseDown += new MouseEventHandler(Form_MouseDown); // MouseMove += new MouseEventHandler(Form_MouseMove); // MouseUp += new MouseEventHandler(Form_MouseUp);
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-05 13:48:28