程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#界面鼠標拖動

c#界面鼠標拖動

編輯: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

            { }
        }

這種方法沒有殘影。

 作者“yysyangyangyangshan的專欄”

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved