Kedor
Kedor

Reputation: 1498

How to get the coordinates of a click event inside Panel?

How can one get the coordinates of a mouse click inside a panel? For instance I want to be able to put pixels wherever I click with my mouse. I can handle the drawing part, but I don't know how to make my program to listen for that click event, and how to get x/y coordinates of it so it can draw in right place.

I never saw a similar piece of code, and I was unable to find relevant information with google so I can't show any code "how I tried to achieve that" because I simply didn't :/ I have no clue how to start. I am probably searching the wrong keywords, but I am sure that it can be done.

EDIT: Thank you all

Upvotes: 0

Views: 5363

Answers (3)

Hans Passant
Hans Passant

Reputation: 941455

The Click event is not the right choice. It can be generated both by the mouse and the keyboard so it doesn't pass the mouse position. A button for example can be clicked by pressing the space bar. This of course won't happen for a panel. Simply use the MouseUp event instead. Check the passed e->Button property.

A panel is not the good choice either btw. It is not double-buffered so it is likely to start flickering when the drawing gets intricate. It doesn't redraw properly when the size changes. A PictureBox doesn't have these problems. Just treat it like the panel, implement its Paint event and don't set the Image property.

Upvotes: 4

AAlkhabbaz
AAlkhabbaz

Reputation: 197

private: System::Void panel1_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) 
{    
     int x= e->X;
     int y= e->y;
}

Upvotes: 1

Sebastian
Sebastian

Reputation: 8164

Here is a complete hint for your question, with code. For the panel you can use the mouseDown event. And from the event arguments you can get the coords.

Upvotes: 2

Related Questions