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;
}
}
想法是每次移动鼠标时计算出与上次鼠标的差值,也就是本窗体应该移动的距离,加上这个差值达到移动效果
但效果不一样,大家试一下回答我

解决方案 »

  1.   

    你非要用这种方法做啊? 我用这样的方式做过,但是以失败而告终;最后从高人手中得到一方法,如下:using System;
    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);
            }
        }
    }
      

  2.   

    你非要用这种方法做啊? 我用这样的方式做过,但是以失败而告终;最后从高人手中得到一方法,如下:using System;
    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);
            }
        }
    }
      

  3.   

    MouseMove里的设置位置代码需要这么写Point location = new Point(e.X - m__RectOrg.X + this.Location.X,
    e.Y - m__RectOrg.Y + this.Location.Y);
    this.Location = location;