[Winform Study Notes (7)] Winform borderless form drag function

Preface

This article mainly introduces how to set the form to borderless mode and several methods to realize the dragging function of borderless form.

text

1. Set borderless mode

Select the form whose borders you want to remove, press F4 to bring up its properties panel, find FormBorderStyle in the properties panel , and select None to set the form to borderless mode; by default, it cannot be dragged at will and is not maximized. Minimize close button.
Insert image description here

2. Borderless form dragging method

1. Implement form movement through Panel control

  1. Implementation method : Add a Panel control at the head of the form, and move the form through the Panel's MouseDown and MouseMove events;

  2. Implementation principle : directly modify the position of the form to achieve the effect of moving the form;

  3. Specific code :

    		private Point mPoint;//定义一个位置信息Point用于存储鼠标位置
            /// <summary>
            /// 鼠标按下
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void uPanel1_MouseDown(object sender, MouseEventArgs e)
            {
          
          
                mPoint = new Point(e.X, e.Y);
            }
            /// <summary>
            /// 鼠标移动
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void uPanel1_MouseMove(object sender, MouseEventArgs e)
            {
          
          
                if (e.Button == MouseButtons.Left)
                {
          
          
                    this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
                }
            }
    
  4. Realization effect :
    Insert image description here

2. Implement form movement through form events

  1. Implementation method : realize form movement through the form MouseDown, MouseMove, and MouseUp events;

  2. Specific code :

    		//通过窗体MouseDown、MouseMove、MouseUp事件实现窗体移动
            Point point; //鼠标按下时的点
            bool isMoving = false;//标识是否拖动
    
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
          
          
                point = e.Location;//按下的点
                isMoving = true;//启动拖动
            }
    
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
          
          
                if (e.Button == MouseButtons.Left && isMoving)
                {
          
          
                    Point pNew = new Point(e.Location.X - point.X, e.Location.Y - point.Y);
                    //Location = new Point(Location.X + pNew.X, Location.Y + pNew.Y);
                    Location += new Size(pNew);
                }
            }
    
            private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
          
          
                isMoving = false;//停止
            }
    
  3. Realization effect :
    Insert image description here

3. Call the system API to move the form

  1. Implementation method : Use the Windows application programming interface Windows API to realize form movement;

  2. Implementation principle : When the left mouse button is pressed, the system thinks it was pressed on the title bar;

  3. Specific code :

    using System.Runtime.InteropServices;
            //调用系统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;//定义鼠标左键按下
            public const int HTCAPTION = 0x0002; //HTCAPTION=2 鼠标在标题栏中
            /// <summary>
            /// 鼠标按下事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
          
          
                //为当前应用程序释放鼠标捕获
                ReleaseCapture();
                //发送消息 让系统误以为在标题栏上按下鼠标
                SendMessage((IntPtr)this.Handle, VM_NCLBUTTONDOWN, HTCAPTION, 0);
            }
    
  4. Note : Need to introduce namespaceusing System.Runtime.InteropServices;

  5. Realization effect :Insert image description here

4. Rewrite WndProc() to implement form movement

  1. Implementation method : Implement form movement by overriding the WndProc() method;

  2. Implementation principle : Change the message when the mouse is pressed in the client area to when the mouse is pressed in the title bar of the non-client area;

  3. Specific code :

    		//重写WndProc:
            //原理:将鼠标在客户区按下的消息更改为在非客户区的标题栏按下
            protected override void WndProc(ref Message m)
            {
          
          
                switch (m.Msg)
                {
          
          
                    case 0x0201://鼠标左键按下的消息
                        m.Msg = 0x00A1;//更改消息为非客户区按下鼠标
                        m.LParam = IntPtr.Zero;//默认值
                        m.WParam = new IntPtr(2);//鼠标放在标题栏内
                        break;
                }
                base.WndProc(ref m);
            }
    
  4. Realization effect :
    Insert image description here

Guess you like

Origin blog.csdn.net/sallyyellow/article/details/132144375