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.
as soon as I get home I will implement, I will give you a feedback, thank you very much for this help.
– gato
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
– gato