c# WinForm 中鼠标控制窗体的移动
初步找到两个方法
方法1:这种方法还没有尝试过
private const int wm_nchittest = 0x84;
private const int htclient = 0x1;
private const int htcaption = 0x2;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case wm_nchittest:
base.WndProc(ref m);
if ((int)m.Result == htclient)
m.Result = (IntPtr)htcaption;
return;
}
base.WndProc(ref m);
}
方法2:已经试过很多次,感觉比较通俗易懂^_^
private Point mouse_offset = new Point(0,0);
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.mouse_offset = new Point(-e.X,-e.Y);
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
Point mousepos = Control.MousePosition;
mousepos.Offset(this.mouse_offset.X,this.mouse_offset.Y-SystemInformation.CaptionHeight);
this.Location = mousepos;
}
}
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.mouse_offset = new Point(-e.X,-e.Y);
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
Point mousepos = Control.MousePosition;
mousepos.Offset(this.mouse_offset.X,this.mouse_offset.Y-SystemInformation.CaptionHeight);
this.Location = mousepos;
}
}