How to get mouse click coordinates on PictureBox on WinForms form?

  • WinForms or WPF? - VladD
  • WinForms, fixed the question - Clarence

1 answer 1

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 the Form.PointToScreen + PictureBox.PointToClient pair. stackoverflow.com/a/1478105/276994 - VladD pm
  • 2
    MouseClick in 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 the PictureBox - then relative to the PictureBox - rdorn
  • @rdorn: Hmm, and in MSDN (link in the second comment) it seems that it is always relative to the form. - VladD