Make a borderless form movable?

前端 未结 21 1818
情歌与酒
情歌与酒 2020-11-22 09:48

Is there a way to make a form that has no border (FormBorderStyle is set to \"none\") movable when the mouse is clicked down on the form just as if there was a border?

相关标签:
21条回答
  • 2020-11-22 10:28

    Adding a MouseLeftButtonDown event handler to the MainWindow worked for me.

    In the event function that gets automatically generated, add the below code:

    base.OnMouseLeftButtonDown(e);
    this.DragMove();
    
    0 讨论(0)
  • 2020-11-22 10:29

    Ref. video Link

    This is tested and easy to understand.

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case 0x84:
                base.WndProc(ref m);
                if((int)m.Result == 0x1)
                    m.Result = (IntPtr)0x2;
                return;
        }
    
        base.WndProc(ref m);
    }
    
    0 讨论(0)
  • 2020-11-22 10:31

    This article on CodeProject details a technique. Is basically boils down to:

    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;
    
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool ReleaseCapture();
    
    private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {     
        if (e.Button == MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }
    

    This essentially does exactly the same as grabbing the title bar of a window, from the window manager's point of view.

    0 讨论(0)
  • 2020-11-22 10:31

    Another simpler way to do the same thing.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // set this.FormBorderStyle to None here if needed
            // if set to none, make sure you have a way to close the form!
        }
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_NCHITTEST)
                m.Result = (IntPtr)(HT_CAPTION);
        }
    
        private const int WM_NCHITTEST = 0x84;
        private const int HT_CLIENT = 0x1;
        private const int HT_CAPTION = 0x2;
    }
    
    0 讨论(0)
  • 2020-11-22 10:32

    Easiest way is:

    First create a label named label1. Go to label1's events > mouse events > Label1_Mouse Move and write these:

    if (e.Button == MouseButtons.Left){
        Left += e.X;
        Top += e.Y;`
    }
    
    0 讨论(0)
  • 2020-11-22 10:35
    public Point mouseLocation;
    private void frmInstallDevice_MouseDown(object sender, MouseEventArgs e)
    {
      mouseLocation = new Point(-e.X, -e.Y);
    }
    
    private void frmInstallDevice_MouseMove(object sender, MouseEventArgs e)
    {
      if (e.Button == MouseButtons.Left)
      {
        Point mousePos = Control.MousePosition;
        mousePos.Offset(mouseLocation.X, mouseLocation.Y);
        Location = mousePos;
      }
    }
    

    this can solve ur problem....

    0 讨论(0)
提交回复
热议问题