c# - 如何获取 Windows 窗体上 PictureBox 的左上角屏幕坐标

标签 c# .net winforms coordinates picturebox

我的 Windows 窗体上有一个 PictureBox

我正在使用 ControlPaint.DrawReversibleFrame()PictureBox 上绘制一个矩形,并且想要编写一些边界,所以我仅在 PictureBox 上绘制,而不是在整个屏幕上绘制。

如何找到 PictureBox 左上角的屏幕坐标?

使用解决方案进行编辑:如果有人需要编写一些 PictureBox 边界,这是我的解决方案。

                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 > diff_x)
                    {
                        width = diff_x;
                    }
                    else if(width < 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 > diff_Y)
                    {
                        height = diff_Y;
                    }
                    else if(height < 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.
                }

最佳答案

使用Control.PointToScreen( new Point(0, 0) ),其中Control是您的PictureBox。 请参阅http://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoscreen.aspx

关于c# - 如何获取 Windows 窗体上 PictureBox 的左上角屏幕坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8546070/

相关文章:

c# - 窗体 : Binding a custom control property to a BindingList

c# - 无限数量的子弹

.net - 如何突破ASP.NET webservice的限制?

c# - 将换行符附加到字符串的最佳方法,最后一个除外

winforms - 具有新的 Office High DPI 支持的 Outlook VSTO

c# - 错误 1 ​​可访问性不一致 : return type is less accessible than method

c# - 实例化实体模型时看不到 dbcontext 方法即 savechanges

c# - 使用 transactionscope 时停止事务升级为分布式的推荐做法

c# - 如何在每个配置的基础上添加程序集引用

c# - 我如何遍历 FileInfo[] 并更新 Paint 事件?