【Winfrom-无边框窗体】Winform如何拖动无边框窗体?

去掉边框

      this.FormBorderStyle = FormBorderStyle.None;

方法一:

     Point mouseOff;//鼠标移动位置变量
        bool leftFlag;//标签是否为左键

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y);
                leftFlag = true;
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);//设置移动后的位置
                Location = mouseSet;
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为False
            }

        }

 

方法二:调用非托管的动态链接库,通过控件的鼠标按下事件(MouseDown)发送一个拖动的消息,可以给控件添加MouseDown事件后,拖动这个控件来移动窗体

     [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [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)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

  

方法三:通过重载消息处理实现。重写窗口过程(WndProc),处理一些非客户区消息(WM_NCxxxx),C#中重写窗口过程不用再调用SetWindowLong API,直接override一个WndProc就可以了,不用声明api函数。

鼠标的拖动只对窗体本身有效,不能在窗口的控件区域电机拖动

     protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0x84)
            {
                switch (m.Result.ToInt32())
                {
                    case 1:
                        m.Result = new IntPtr(2);
                        break;
                }
            }
        }

  

方法四:直接在控件上写事件,主窗体设置无边框,让一个panel或PictureBox停靠在主窗体

请看   http://www.cnblogs.com/Sukie-s-home/p/5216980.html

 

posted @ 2016-02-26 16:04  发明创造小能手  阅读(919)  评论(0编辑  收藏  举报
levels of contents