Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the TopLeft screen-coordinate of a PictureBox on a Windows Forms
    text
    copied!<p>I have a a <code>PictureBox</code> on my Windows Forms.</p> <p>I am drawing a <code>Rectangle</code> on the <code>PictureBox</code>, with <code>ControlPaint.DrawReversibleFrame()</code>, and want to code some boundaries, so I am only drawing on the <code>PictureBox</code> and not the whole screen.</p> <p>How do I find the screen-coordinates of the topleft point of the <code>PictureBox</code>?</p> <p><strong>EDIT with solution</strong>: Here's my solution, if anybody need to code some PictureBox boundaries.</p> <pre><code> if (_isDragging) // If the mouse is being dragged, undraw and redraw the rectangle as the mouse moves. { pictureBoxMap.Refresh(); ControlPaint.DrawReversibleFrame(_theRectangleScreenCoords, BackColor, FrameStyle.Dashed); // Hide the previous rectangle by calling the DrawReversibleFrame method with the same parameters. Point endPoint = ((Control)sender).PointToScreen(new Point(e.X, e.Y)); var topLeftPictureBoxMap = pictureBoxMap.PointToScreen(new Point(0, 0)); int width = endPoint.X - _startPointTheRectangleScreenCoords.X; int height = endPoint.Y - _startPointTheRectangleScreenCoords.Y; // limit rectangle in x-axis var diff_x = pictureBoxMap.Width - (_startPointTheRectangleScreenCoords.X - topLeftPictureBoxMap.X); var diff_x_2 = (pictureBoxMap.Width - (_startPointTheRectangleScreenCoords.X - topLeftPictureBoxMap.X)) - pictureBoxMap.Width; if (width &gt; diff_x) { width = diff_x; } else if(width &lt; diff_x_2) { width = diff_x_2; } // limit rectangle i y-aksen var diff_Y = pictureBoxMap.Height - (_startPointTheRectangleScreenCoords.Y - topLeftPictureBoxMap.Y); var diff_Y_2 = (pictureBoxMap.Height - (_startPointTheRectangleScreenCoords.Y - topLeftPictureBoxMap.Y)) - pictureBoxMap.Height; if (height &gt; diff_Y) { height = diff_Y; } else if(height &lt; diff_Y_2) { height = diff_Y_2; } _theRectangleScreenCoords = new Rectangle( _startPointTheRectangleScreenCoords.X, _startPointTheRectangleScreenCoords.Y, width, height); ControlPaint.DrawReversibleFrame(_theRectangleScreenCoords, Color.Red, FrameStyle.Dashed); // Draw the new rectangle by calling DrawReversibleFrame again. } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload