VB.Net画图的实例(2009-08-11 16:42:59)
Public Class DrawsForm
Dim graph As Graphics = Me.CreateGraphics
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
''绘制直线
Dim mypen1 As Pen = New Pen(Color.Blue, 5)
Dim mypen2 As Pen = New Pen(Color.Red, 5)
mypen1.LineJoin = Drawing2D.LineJoin.Round ''2条线连接类型
mypen2.LineJoin = Drawing2D.LineJoin.Round
Dim Point1 As New Point(50, 50)
Dim Point2 As New Point(60, 100)
mypen1.DashStyle = Drawing2D.DashStyle.Dot
mypen2.DashStyle = Drawing2D.DashStyle.Dash
graph.DrawLine(mypen1, Point1, Point2)
graph.DrawLine(mypen2, 50, 50, 100, 100)
mypen1.Dispose()
mypen2.Dispose()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
graph.Clear(Me.BackColor) '清除,就是用背景色填充
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
''绘制圆
Dim mypen1 As Pen = New Pen(Color.Blue, 5)
Dim mypen2 As Pen = New Pen(Color.Red, 5)
Dim rectangle1 As Rectangle = New Rectangle(50, 50, 100, 100)
'graph.DrawEllipse(mypen1, 50, 50, 100, 100) ' 绘制圆
graph.DrawEllipse(mypen1, rectangle1) ' 绘制圆
graph.DrawEllipse(mypen2, 50, 50, 150, 200) '绘制椭圆
mypen1.Dispose()
mypen2.Dispose()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
''绘制矩形
Dim mypen1 As Pen = New Pen(Color.Blue, 5)
Dim mypen2 As Pen = New Pen(Color.Red, 5)
' Dim rectangle1 As Rectangle = New Rectangle(50, 50, 100, 100)
'Dim rectangle2 As Rectangle = New Rectangle(10, 10, 90, 150)
graph.DrawRectangle(mypen1, 50, 50, 100, 100)
graph.DrawRectangle(mypen2, 10, 10, 90, 150)
'graph.DrawRectangle(mypen1, rectangle1)
'graph.DrawRectangle(mypen2, rectangle2)
mypen1.Dispose()
mypen2.Dispose()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
''绘制曲线
Dim mypen As Pen = New Pen(Color.Blue, 5)
Dim points1(20) As Point
For i As Integer = 1 To 20 '初始化曲线上点数组
points1(i).X = i ^ 3
points1(i).Y = i * 10
Next
graph.DrawCurve(mypen, points1)
mypen.Dispose()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
''绘制折线
Dim mypen As Pen = New Pen(Color.Blue, 5)
Dim points1(3) As Point
points1(0).X = 0
points1(0).Y = 0
points1(1).X = 100
points1(1).Y = 100
points1(2).X = 150
points1(2).Y = 50
points1(3).X = 300
points1(3).Y = 300
graph.DrawLines(mypen, points1)
mypen.Dispose()
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
''绘制文本
Dim myfont As Font = New Font("黑体", 12)
Dim mypen As Pen = New Pen(Color.Red, 5)
Dim forma As StringFormat = New StringFormat(StringFormatFlags.DirectionVertical)
graph.DrawString("绘制文本", myfont, mypen.Brush, 100, 100, forma)
End Sub
End Class