I'm trying to make a button follow the cursor, but my code only seems to just go to the mouse cursor right after execution and just stay there. Can anyone give me the correct code and help me?
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Location = Cursor.Position;
}
}
}
CodePudding user response:
I would suggest overriding the OnMouseMove
method of the MainForm
and using the Location
property of the MouseEventArgs e
argument. This will provide the correct Client
coordinates of the cursor taking into account where MainForm
is located on the screen. Then you can offset that value using the Width
and Height
properties of buttonMoveMe
to center it on the mouse.
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
protected override void onm ouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
var point = new Point(
e.Location.X - (buttonMoveMe.Width / 2),
e.Location.Y - (buttonMoveMe.Height / 2));
buttonMoveMe.Location = point;
}
}
Be aware that this event _will not be received when the mouse is over the button itself. The button will be getting the MouseMove
events (not the MainForm) in this case. In other words, this basic suggestion only moves the button when the mouse leaves the button.
CodePudding user response:
I think you need to use the MouseMove
event.
So these are things you need to do:
- Create a button control which is going to follow the cursor. (I will call it
yourButton
) - Open event menu of the control (click on it in the constructor).
- Find event
MouseMove
insideMouse
category. - Double click on it. (This will automatically create the method which will be linked to the event)
- In this method write:
yourButton.Location = e.Location;
So it should look like that:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
yourButton.Location = e.Location;
}
In my example I made button follow my cursor inside the form.
If you need shorter way, you can just copy-paste the method I wrote before and in the form class constructor add:
this.MouseMove = Form1_MouseMove;
In this way your code would look like that:
public Form1()
{
InitializeComponent();
this.MouseMove = Form1_MouseMove;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
yourButton.Location = e.Location;
}
Hope this will help you, have a nice day!
CodePudding user response:
Your Form1_Load
method is only executed, as its name says, when the form is loaded.
You want to execute your code every time the mouse move, si you can do it like this (not tested):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.MouseMove = Form1_MouseMove;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
button1.Location = Cursor.Position;
}
}