2014-12-19 125 views
0

我想在窗体上绘制一个简单的2D矩形。在vb.net中绘制一个矩形

因为我从来没有在vb.net中做过图形化的任何事情,所以我在网上搜索了很多实例,提供了类似于这个实例的解决方案。

Public Sub DrawRectangleRectangle(ByVal e As PaintEventArgs) 

    ' Create pen. 
    Dim blackPen As New Pen(Color.Black, 3) 

    ' Create rectangle. 
    Dim rect As New Rectangle(0, 0, 200, 200) 

    ' Draw rectangle to screen. 
    e.Graphics.DrawRectangle(blackPen, rect) 
End Sub 

不过,我不明白这是如何工作.. 这是什么e As PaintEventArgs?这个子要求什么输入?我如何绘制一个简单的矩形?对于初学者,我想要一些简单的工作,所以我可以对它进行试验并最终学习更先进的东西。

回答

5

确定此代码之前,做工精细,您可以测试它的学习

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

    'dimension variables of local scope 

    Dim myGraphics As Graphics 

    Dim myRectangle As Rectangle 

    Dim myPen As New Pen(Color.Blue) 

    'return the current form as a drawing surface 

    myGraphics = Graphics.FromHwnd(ActiveForm().Handle) 

    'create a rectangle based on x,y coordinates, width, & height 

    myRectangle = New Rectangle(x:=5, y:=5, Width:=10, Height:=40) 

    'draw rectangle from pen and rectangle objects 

    myGraphics.DrawRectangle(pen:=myPen, rect:=myRectangle) 

    'create a rectangle based on Point and Size objects 

    myRectangle = New Rectangle(Location:=New Point(10, 10), Size:=New Size(Width:=20, Height:=60)) 

    'draw another rectangle from Pen and new Rectangle object 

    myGraphics.DrawRectangle(pen:=myPen, rect:=myRectangle) 

    'draw a rectangle from a Pen object, a rectangle's x & y, 

     ' width, & height 

    myGraphics.DrawRectangle(pen:=myPen, x:=20, y:=20, Width:=30, Height:=80) 

End Sub 
+0

谢谢。这正是我需要的。 – 2014-12-19 10:14:11