How to get mouse click coordinates on PictureBox on WinForms form?
1 answer
Well, apparently, you need to subscribe to MouseClick (not to Click - it does not have mouse coordinates). In the resulting MouseEventArgs there is a Location or separately X and Y
If p is your PictureBox , then you need something like this:
p.MouseClick += OnPictureBoxClicked; void OnPictureBoxClicked(object sender, MouseEventArgs args) { var location = args.Location; // у вас есть координаты клика относительно формы } Well or pack OnPictureBoxClicked in lambda.
- That is, the coordinates relative to the form, not PictureBox? - Clarence
- @ Arina: It seems to be yes: msdn.microsoft.com/en-us/library/… - VladD
- @Arina: If you need a relatively
PictureBox, you can use theForm.PointToScreen+PictureBox.PointToClientpair. stackoverflow.com/a/1478105/276994 - VladD pm - 2
MouseClickin the arguments passes the coordinates of the mouse relative to the controller that is subscribed to the event, i.e. if the handler hangs on the form - then the coordinates are relative to the form, if on thePictureBox- then relative to thePictureBox- rdorn - @rdorn: Hmm, and in MSDN (link in the second comment) it seems that it is always relative to the form. - VladD
|