捕获一个矩形

时间:2013-03-08 08:37:20

标签: vb.net

这很有效。它在面板上绘制一个简单的矩形:

    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)

2 个答案:

答案 0 :(得分: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 :(得分: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)