0

We can get X and Y points which by mouse move on the picturebox like;

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
     double Xcoordinate = e.X;
     double Ycoordinate = e.Y;

     label1.Text = Xcoordinate.ToString();
     label2.Text = Ycoordinate.ToString();
}

My question How can I get Xcoordinate and Ycoordinate from other events for ex; MouseClick event or my new defined function?

Actually I want to reach XCoordinate and Ycoordinate parameter from FormLoad. How can I do that?

ceng
  • 62
  • 12
  • thanks for minus. Pls give your opion dear genius – ceng May 06 '14 at 13:03
  • possible duplicate of http://stackoverflow.com/questions/1316681/getting-mouse-position-in-c-sharp – Nzall May 06 '14 at 13:04
  • I renewed my question . Just imagine my computer has two cursor (two mouse) And, I have two picturebox (picturebox1 and picturebox2). I want to reach cursor positions of these two pictureboxes from other event or function. – ceng May 06 '14 at 13:24
  • private void MoveCursor() { label1.Text = Convert.ToString(new Point(pictureBox1.???????? } – ceng May 06 '14 at 13:26
  • I'm sorry, but how did you manage to have 2 mice and 2 cursors at once? I didn't know that was even possible. and how would you know which mouse position you need? – Nzall May 06 '14 at 13:55
  • I mean, mouse1 on picturebox1 and will show us e.X and e.Y points at label1, likewise mouse2 on picturebox2 and it will show us points at label2.Anyway It is going crayz. Have a nice day – ceng May 06 '14 at 14:03
  • But I want to reach xcoordinate and ycoordinate parameter at FormLoad – ceng May 06 '14 at 14:04
  • So you have 2 pictureboxes, each with a mouse cursor, and you want to show the X and Y coordinates of each pointer in a label. Still doesn't explain how you managed to get hold of a second mouse cursor in the same Windows installation. – Nzall May 06 '14 at 14:08
  • if u do for just one picturebox, I will do it more ;). But values should reacheable from MainForm – ceng May 06 '14 at 14:15
  • I ask again for the 3rd time how you got that second mouse pointer. You cannot have more than 1 mouse pointer at once on Windows unless you have an app that provides a second cursor. If you developed that app yourself you wouldn't ask this question because you'd know how to access that second mouse. So it has to be a 3rd party app, and the app you're using completely changes how you access this second mouse pointer. We literally cannot answer your question until you answered our question first: How did you get that second mouse? – Nzall May 06 '14 at 14:29
  • ok calm down, I will use 2 joysticks in order to mouse. I am waiting your response about my above question. have a nice day – ceng May 06 '14 at 15:04
  • That solves the hardware issue, but not the software issues. I repeat again: Microsoft Windows does not have native support for more than 1 mouse cursor. Since you want to use C#, you'll have to use Microsoft Windows. Because Windows does not have native support for 2 or more mouse cursors, You'll have to use an extra piece of software for this cursor. My question is mainly: What software will you use for that? Depending on how this application works you need to access the secondary mouse pointer in a different way. As said, we need to know the software you use or we can't help you. – Nzall May 06 '14 at 19:36
  • Dear Nate, I am using Microsoft Visual Studio C# (Student version). "Windows does not have native support for 2 or more mouse cursors" Please check http://www.codeproject.com/Articles/185522/Using-the-Raw-Input-API-to-Process-Joystick-Input – ceng May 08 '14 at 11:03
  • This is not with mouse cursors. Those crosses in those pictureboxes are not mouse cursors, they're pointers. In any case, you can access those picturebox pointer locations through the API for the picturebox. check MSDN for explanations on how to use those. – Nzall May 08 '14 at 11:10
  • Dear nate, I am not expert but I know that u can use cursors in order to cross. Meanwhile could you read again my question. regards – ceng May 08 '14 at 11:24

4 Answers4

1

Use Cursor Position property..

 private void MoveCursor()
    {
       // Set the Current cursor, move the cursor's Position, 
       // and set its clipping rectangle to the form.  

       this.Cursor = new Cursor(Cursor.Current.Handle);
       Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
       Cursor.Clip = new Rectangle(this.Location, this.Size);
    }
Aman
  • 2,196
  • 2
  • 17
  • 19
  • I renewed my question I am a dumb man. Just imagine my computer has two cursor (two mouse) And, I have two picturebox (picturebox1 and picturebox2). I want to reach cursor positions of these two picturebox from other event or function. – ceng May 06 '14 at 13:18
0

The MouseMove event happens to give you the mouse position. This is not included in other EventArgs. You can always get the mouse positon through Cursor.Position.

Jon B
  • 51,025
  • 31
  • 133
  • 161
0

The static method Control.MousePosition will get you the absolute position of the mouse pointer on the screen. You can translate it using Control.PointToClient to get the coordinates local to a control of interest.

If I remember correctly, the one caveat is that MouseEventArgs gives you the mouse position as it was when the message was posted to the event loop, while Control.MousePosition gives you the position right now. For most applications, this difference is probably not a big deal.

adv12
  • 8,443
  • 2
  • 24
  • 48
  • I renewed my question I am a dumb man. Just imagine my computer has two cursor (two mouse) And, I have two picturebox (picturebox1 and picturebox2). I want to reach cursor positions of these two picturebox from other event or function – ceng May 06 '14 at 13:17
0

You can Use this Solution to Get co-ordinate of picture Box at the time of other event

protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            textBox1.Text = e.X.ToString();
            textBox2.Text = e.Y.ToString();
        }

       private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            textBox1.Text = e.X.ToString();
            textBox2.Text = e.Y.ToString();

        }
    } 

Or try It also

pictureBox1.MouseClick += (s, e) => MessageBox.Show(String.Format("Mouse Clicked at X: {0} Y: {1}", e.X, e.Y));
Aman
  • 2,196
  • 2
  • 17
  • 19
  • Dear Aman, I want to kindly say to you. I defined xCorrdinate=e.X and yCoordinate=e.Y at OnMouseClick. Actually I want to reach xCoordinate and yCoordinate parameter from FormLoad. How can I do that? What should I search on internet. thanks – ceng May 06 '14 at 13:55