How to drag form without border?

8

I'm trying to implement a way to drag my form without a border when I click and hold the left mouse button on it, but I did not succeed. Here is an example of my attempt to implement.

1 ° I created the% and%% variables as global within the% class of my form to receive the mouse position:

int X = 0;
int Y = 0;

2 ° I then implemented the following events, assigning the values to X and Y .

MouseDown Event :

private void frmExemploFormSemBorda_MouseDown(object sender, MouseEventArgs e)
{
     if (e.Button != MouseButtons.Left) 
          return;

     X = this.Left - MousePosition.X;
     Y = this.Left - MousePosition.Y;
}

MouseMove Event :

private void frmExemploFormSemBorda_MouseMove(object sender, MouseEventArgs e)
{
     if (e.Button != MouseButtons.Left)
          return;

     X = this.Left + MousePosition.X;
     Y = this.Left + MousePosition.Y;
}

3 And finally, I called the events frmExemploFormSemBorda and X in the constructor of class Y of my form:

public frmExemploFormSemBorda()
{
     InitializeComponent();

     this.MouseDown += new MouseEventHandler(frmExemploFormSemBorda_MouseDown);
     this.MouseMove += new MouseEventHandler(frmExemploFormSemBorda_MouseMove);
}

Here is the complete code for my example :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExemploFormSemBorda
{
    public partial class frmExemploFormSemBorda : Form
    {
        int X = 0;
        int Y = 0;

        public frmExemploFormSemBorda()
        {
            InitializeComponent();

            this.MouseDown += new MouseEventHandler(frmExemploFormSemBorda_MouseDown);
            this.MouseMove += new MouseEventHandler(frmExemploFormSemBorda_MouseMove);
        }

        private void frmExemploFormSemBorda_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) 
                return;

            X = this.Left - MousePosition.X;
            Y = this.Left - MousePosition.Y;
        }

        private void frmExemploFormSemBorda_MouseMove(object sender, MouseEventArgs e)
        {

            if (e.Button != MouseButtons.Left)
                return;

            X = this.Left + MousePosition.X;
            Y = this.Left + MousePosition.Y;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

Thank you in advance.

    
asked by anonymous 06.11.2015 / 01:59

1 answer

6

You can do it that way.

In event MouseDown it is defined that the user clicked the form and the point that it clicked is saved in the clickedAt variable. In event MouseMove is checked if the user has clicked on form , if yes it changes the position as the mouse is dragged. The MouseUp event is triggered when the user releases the mouse button and sets the mouseClicked variable to false .

I do not know if it was clear, in case the answer was confusing, let me know that I will edit it.

bool mouseClicked;
Point clickedAt;

private void form_MouseMove(object sender, MouseEventArgs e)
{
    if (mouseClicked)
    {
        this.Location = new Point(Cursor.Position.X - clickedAt.X, Cursor.Position.Y - clickedAt.Y);
    }
}

private void form_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button != MouseButtons.Left) 
        return;

    mouseClicked = true;
    clickedAt= e.Location;
}

private void form_MouseUp(object sender, MouseEventArgs e)
{
    mouseClicked = false;
}
    
06.11.2015 / 12:01