2013-03-08 75 views
2

这有效。它利用一个简单的矩形面板上:捕获矩形

Dim g As Graphics 
    Dim fPen As Pen 
    g = aPanel.CreateGraphics() 

    fPen = New Pen(Color.Blue) 
    Dim PointX As Point = New Point(10, 20) 
    Dim PointY As Point = New Point(50, 50) 

    g.DrawRectangle(fPen, PointY.X, PointY.Y, 50, 50) 

一切都是对象 - 但我如何引用这ractangle?
我希望在代码中稍后创建椭圆时(即在矩形中绘制椭圆)时,使用此矩形作为参数之一 - 为什么我不能执行以下操作?

Dim g As Graphics 
    Dim fPen As Pen 
    g = aPanel.CreateGraphics() 

    fPen = New Pen(Color.Blue) 
    Dim PointX As Point = New Point(10, 20) 
    Dim PointY As Point = New Point(50, 50) 

    Dim r As Rectangle 
    r = New Rectangle(g.DrawRectangle(fPen, PointY.X, PointY.Y, 50, 50)) '<<<errors here 
    g.DrawEllipse(fPen, r) 
+0

'DrawRectangle'不返回任何东西(它是一个'Sub'')。 – Styxxy 2013-03-08 08:42:08

+0

使用CreateGraphics绘制的任何东西都会消失,如果窗口最小化或者其他窗口在其前面通过。您应该在面板的Paint事件中进行绘画。 – 2013-03-08 15:23:32

回答

1

声明你的矩形,并使用其值:

Dim r As New Rectangle(10, 50, 50, 50) 

g.DrawRectangle(fPen, r.Location.X, r.Location.Y, r.Width, r.Height) 
g.DrawEllipse(fPen, r) 
1

Graphics对象上的DrawRectangle method,因为它是一个Sub不返回任何值。

您首先必须创建Rectangle的实例,您可以稍后使用该实例绘制矩形和椭圆。

Dim pointY As New Point(50, 50) 
Dim rectSize As New Size(50, 50) 
Dim rect As New Rectangle(pointY, rectSize) 

g.DrawRectangle(fPen, rect) 
g.DrawEllipse(fPen, rect) 
+0

没有必要使用不同的笔,它不会导致绘制所有你想要的东西的麻烦。 – SysDragon 2013-03-08 08:58:50

+0

你是对的,当你做“填充”时需要它。 – Styxxy 2013-03-08 10:24:27