-
Mouse Coordinates
Hey I need to get the current position of the mouse relevant to the form. I found the code: public static Point MousePosition { get; } ... And I tried:
private void button1_Click(object sender, EventArgs e)
{
Coord.Text = "X: " + Control.MousePosition.X + ", Y: " + Control.MousePosition.Y;
}
But theres some error with the public static Point MousePosition...
Truthfully im an extreme n00b, I try to avoid wasting your guys time by looking everywhere and making this a last resort to look for on help but I searched everywhere and thats best I could find. My guess is I have it in the wrong spot but thats best place I could find to put it. Any ideas?
namespace RS_Autominer
{
public partial class Form1 : Form
{
public static Point MousePosition { get; }
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Coord.Text = "X: " + Control.MousePosition.X + ", Y: " + Control.MousePosition.Y;
}
}
}
-
Re: Mouse Coordinates
Control.MousePosition returns screen coordinates. If you want them to be relative to a control, you have to call the control's instance method PointToClient(). In your case, It could be
Code:
private void button1_Click(object sender, EventArgs e)
{
Point inClient = this.button1.PointToClient(Control.MousePosition);
Coord.Text = "X: " + inClient.X + ", Y: " + inClient.Y;
}