CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    40

    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;
    }
    }
    }

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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
  •  





Click Here to Expand Forum to Full Width

Featured