How to get the position of a Click?
Asked Answered
S

3

19

I'm currently making a game where the player will click on one of his units (which are pictureboxes) and a circle will become visible with the player's unit in the center. (Circle is also a picturebox) When the player clicks on the picturebox of the circle I need to figure out if the position of the click is inside the radius of the circle. My question is how do I get the position of the click?

Stubborn answered 14/8, 2011 at 5:29 Comment(1)
If you're working towards making a game, using the winforms framework is not the best choice. How about starting out with a console based game and working your way up to XNA (Or any other C# game engine).Trici
D
29

In your click handler, do:

MousePosition.X
MousePosition.Y

Example:

// 
// pictureBox1 Init
// 
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);


private void pictureBox1_Click(object sender, EventArgs e)
{
    MessageBox.Show(string.Format("X: {0} Y: {1}", MousePosition.X, MousePosition.Y));
}

Shows: "X: 537 Y: 946"

One more thing:

The MouseEventArgs with coordinates only receives MouseUp and MouseDown. A MouseClick can't receive your coordinates, because a click consists of a MouseUp and a MouseDown, and both can have different coordinates.

One more solution (I think this is best):

private int X;
private int Y;

private void pictureBox1_Click(object sender, EventArgs e)
{
    MessageBox.Show(string.Format("X: {0} Y: {1}", X, Y));
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    X = e.X;
    Y = e.Y;
}
Digitalize answered 14/8, 2011 at 5:36 Comment(2)
Could I get a code type example because I can't get the click to work when I click a picturebox I did a test: protected override void OnMouseClick(MouseEventArgs e) { base.OnMouseClick(e); if (MousePosition.X > 200) { MessageBox.Show("YESSSSSSS"); } }Stubborn
The MouseEventArgs with coordinates receive only MouseUp and MouseDown. MouseClick can't recive you cordinates because click consists from MouseUp and MouseDown and it both can have different coordinates.Digitalize
N
8

With Yahia's answer, I learned that the EventArgs can be cast to MouseEventArgs.

private void pictureBox1_Click(object sender, EventArgs e)
{
    MouseEventArgs e2 = (MouseEventArgs) e;
    MessageBox.Show(string.Format("X: {0} Y: {1}", e2.X, e2.Y));
}
Nika answered 30/5, 2018 at 19:55 Comment(0)
Z
3

use the MouseClick event of the PictureBox for this sort of thing...

see
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseclick.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs.aspx

Zweig answered 14/8, 2011 at 5:37 Comment(3)
I still don't see how I can get the coordinates of the clickStubborn
The OnMouseClick receives MouseEventArgs as parameter which has X and Y coordinates... see the links above...Zweig
I just made it on xna insteadStubborn

© 2022 - 2024 — McMap. All rights reserved.