码迷,mamicode.com
首页 > Windows程序 > 详细

无边框窗体拖动

时间:2015-07-24 12:49:33      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:winform   无边框   窗体拖动   

方法一: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);  


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

无边框窗体拖动

标签:winform   无边框   窗体拖动   

原文地址:http://blog.csdn.net/u013816709/article/details/47039285

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!