在 vb.net 中绘制矩形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27563524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing a rectangle in vb.net
提问by Edgars Mi?kins
I want to draw a simple 2d rectangle on a form.
我想在表单上绘制一个简单的二维矩形。
Because I have never done anything graphical in vb.net, I searched the web and found many instances, that offer solutions similar to this one.
因为我从未在 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
Yet, I don't understand how this works..
What is this e As PaintEventArgs? What input does this sub require? How can I draw a simple rectangle?
然而,我不明白这是如何工作的..这是什么e As PaintEventArgs?这个子需要什么输入?如何绘制一个简单的矩形?
For starters, I want something simple to work so I can experiment on it and eventually learn more advanced stuff.
首先,我想要一些简单的东西,这样我就可以试验它并最终学习更高级的东西。
回答by Ashouri
OK this code works fine before,You can test it for learning
OK 这段代码之前可以正常使用,可以测试学习
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

