2011-12-17 6 views
2

WindowsフォームにPictureBoxがあります。WindowsフォームでPictureBoxのTopLeft画面座標を取得する方法

私はControlPaint.DrawReversibleFrame()で、PictureBoxRectangleを描く、といくつかの境界をコーディングしたいので、私は唯一の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. 
       } 

答えて

4

ControlがあなたのPictureBoxで使用Control.PointToScreen(new Point(0, 0))。 を参照してくださいhttp://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoscreen.aspx

+0

私はもう一度試してみましょう:) – radbyx

+0

確かに、あなたの時間を取る、私はどこにも行きません。 C - := –

+1

あなたが掲示したリンクに従い、このメソッドは2つの整数ではなく、ポイントをとることに注意してください。 –

関連する問題