无边框窗口拖动

方法一:API调用

 //加入引用  using System.Runtime.InteropServices;

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        void FrmWeather_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, 0xA1, 0x02, 0);
            }
        }

        //调用
        //MouseDown += new MouseEventHandler(FrmWeather_MouseDown);


方法二:

//鼠标移动位置变量 
        Point mouseOff;
        //标签是否为左键   
        bool leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //得到变量的值   
                mouseOff = new Point(-e.X, -e.Y);
                //点击左键按下时标注为true;   
                leftFlag = true;                  
            }
        }
        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                //设置移动后的位置   
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  
                Location = mouseSet;
            }
        }
        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                //释放鼠标后标注为false;   
                leftFlag = false;
            }
        }  

        //调用
         //   MouseDown += new MouseEventHandler(Form_MouseDown);
         //   MouseMove += new MouseEventHandler(Form_MouseMove);
         //   MouseUp += new MouseEventHandler(Form_MouseUp);  


 

posted @ 2017-06-13 16:53  claireyuancy  阅读(198)  评论(0编辑  收藏  举报