C#border设置为none后实现鼠标拖动窗体

 #region 拖动窗体
        static bool isDown = false;
        int currL, currT,tempL,tempT;
        /// <summary>
        /// 鼠标按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void snubbing_MouseDown(object sender, MouseEventArgs e)
        {
            isDown = true;
            tempL = e.X;
            tempT = e.Y;
            currL = Cursor.Position.X - e.X;
            currT = Cursor.Position.Y - e.Y;
        }
        /// <summary>
        /// 鼠标松开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void snubbing_MouseUp(object sender, MouseEventArgs e)
        {
            isDown = false;
        }

        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void snubbing_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDown)
            {
                this.Left = Cursor.Position.X - tempL;
                this.Top = Cursor.Position.Y - tempT;
            }
        }
        #endregion

  使用函数API进行窗体拖动

  使用函数前要添加引用:System.Runtime.InteropServices

  #region 使用函数API的窗体移动事件        
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        private const int VM_NCLBUTTONDOWN = 0XA1;//定义鼠标左键按下
        private const int HTCAPTION = 2;
        private void pnlWindow_MouseDown(object sender, MouseEventArgs e)
        {
            //为当前应用程序释放鼠标捕获
            ReleaseCapture();
            //发送消息 让系统误以为在标题栏上按下鼠标
            SendMessage((IntPtr)this.Handle, VM_NCLBUTTONDOWN, HTCAPTION, 0);
        }
        #endregion

  

posted on 2019-07-08 17:31  呼zzZzz  阅读(218)  评论(0)    收藏  举报

导航