How to drag form without border?

Asked

Viewed 1,947 times

8

I’m trying to implement a way to drag my form no edge while clicking and holding down the left mouse button on it, however I did not succeed. Below follows the example of my attempt at implementation.

1° I created the variaves X and Y as global within the class frmExemploFormSemBorda 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.

Event Mousedown:

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

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

Event Mousemove:

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 made the call of events MouseDown and MouseMove in the class builder frmExemploFormSemBorda of my form:

public frmExemploFormSemBorda()
{
     InitializeComponent();

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

Follow here the full code of 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();
        }
    }
}

Thanks in advance.

1 answer

6


You can do it this way.

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

Obs.: I do not know if it was clear, in case the answer was confused let me know that I 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;
}
  • 1

    as soon as I get home I will implement, I will give you a feedback, thank you very much for this help.

  • jbueno thank you very much worked here, I did like this in your reply and it worked fine, man I spent a few days trying to implement and I could not get anything, thank you very much! : D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.