FormBorderStyle  的属性我已经设置为 None没有窗体的边框.怎么能拖动窗体??

解决方案 »

  1.   

    三种方案自己选吧:
     // API
    using System.Runtime.InteropServices;[DllImport("User32.DLL")]
    public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
    [DllImport("User32.DLL")]
    public static extern bool ReleaseCapture();
    public const uint WM_SYSCOMMAND = 0x0112;
    public const int SC_MOVE = 61456;
    public const int HTCAPTION = 2;private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0);
    }//---------------
    // 消息
    protected override void WndProc(ref Message m)
    {
        const int WM_NCHITTEST = 0x0084;
        const int WM_NCLBUTTONDBLCLK  = 0x00A3;
        const int WM_SYSCOMMAND = 0x0112;
        const int HTCLIENT = 1;
        const int HTCAPTION = 2;
        switch (m.Msg)
        {
            case WM_NCHITTEST:
                base.WndProc(ref m);
                if ((int)m.Result == HTCLIENT)
                    m.Result = (IntPtr)HTCAPTION;
                return;
            case WM_NCLBUTTONDBLCLK:
                return;
        }
        base.WndProc(ref m);
    } //---------------
    // 坐标计算
    Point downPoint;
    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        downPoint = new Point(e.X, e.Y); 
    }private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            Location = new Point(Location.X + e.X - downPoint.X, 
                Location.Y + e.Y - downPoint.Y);
        } 
    }
      

  2.   


    public partial class Form1 : Form
        {
            Point MyOffset;
            public Form1()
            {
                InitializeComponent();
                this.MouseDown += new MouseEventHandler(Form1_MouseDown);
                this.MouseMove += new MouseEventHandler(Form1_MouseMove);
            }        void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                //throw new Exception("The method or operation is not implemented.");
                if (e.Button == MouseButtons.Left)
                {
                    Point MyPos = Control.MousePosition;
                    MyPos.Offset(MyOffset.X, MyOffset.Y);
                    this.DesktopLocation = MyPos;
                }
            }        void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                //throw new Exception("The method or operation is not implemented.");
                MyOffset = new Point(-e.X, -e.Y);
            }  
        }
    代码已经测试通过.
      

  3.   

     FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
      

  4.   

            int x;
            int y;
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    x = e.X;
                    y = e.Y;
                }
            }        private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    this.Left += (e.X - x);
                    this.Top += (e.Y - y);
                }
            }