c#界面鼠标拖动

 之前做过拖动,找了两种方法:

1、

 private Point myPoint;
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point myPosition = Control.MousePosition;
                myPosition.Offset(myPoint.X, myPoint.Y);
                this.DesktopLocation = myPosition;
            }
        }
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point myPosition = Control.MousePosition;
                myPosition.Offset(myPoint.X, myPoint.Y);
                this.DesktopLocation = myPosition;
            }
        }
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            myPoint = new Point(-e.X, -e.Y);
        }
      

这种方法要加三个事件Form1_MouseMove,Form1_MouseUp,Form1_MouseDown,可以实现但是拖动过程中有残影。

2、

 [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
            catch 

            { }
        }

这种方法没有残影。

你可能感兴趣的:(object,user,C#)