|
-
July 18th, 2006, 11:45 PM
#1
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;
}
}
}
-
July 19th, 2006, 01:51 AM
#2
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;
}
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|