private void Main_MouseDown(object sender, MouseEventArgs e)
{
m__RectOrg.X = e.X;
m__RectOrg.Y = e.Y;
}private void Main_MouseMove(object sender, MouseEventArgs e)
{
if (MouseButtons.Left == e.Button)
{
Left -= m__RectOrg.X - e.X;
Top += m__RectOrg.Y - e.Y;m__RectOrg.X = e.X;
m__RectOrg.Y = e.Y;
}
}
想法是每次移动鼠标时计算出与上次鼠标的差值,也就是本窗体应该移动的距离,加上这个差值达到移动效果
但效果不一样,大家试一下回答我
{
m__RectOrg.X = e.X;
m__RectOrg.Y = e.Y;
}private void Main_MouseMove(object sender, MouseEventArgs e)
{
if (MouseButtons.Left == e.Button)
{
Left -= m__RectOrg.X - e.X;
Top += m__RectOrg.Y - e.Y;m__RectOrg.X = e.X;
m__RectOrg.Y = e.Y;
}
}
想法是每次移动鼠标时计算出与上次鼠标的差值,也就是本窗体应该移动的距离,加上这个差值达到移动效果
但效果不一样,大家试一下回答我
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace DraggingForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} [DllImport("user32.dll")] //导入user32.dll
public static extern bool ReleaseCapture(); [DllImport("user32.dll")] //使用系统SendMessage API public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
private void Form1_MouseDown(object sender, MouseEventArgs e) //MouseDown事件中就这样写就行了 {
ReleaseCapture();
SendMessage(this.Handle, 0x0112, 0XF010+0x002,0);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace DraggingForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} [DllImport("user32.dll")] //导入user32.dll
public static extern bool ReleaseCapture(); [DllImport("user32.dll")] //使用系统SendMessage API public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
private void Form1_MouseDown(object sender, MouseEventArgs e) //MouseDown事件中就这样写就行了 {
ReleaseCapture();
SendMessage(this.Handle, 0x0112, 0XF010+0x002,0);
}
}
}
e.Y - m__RectOrg.Y + this.Location.Y);
this.Location = location;