实例说明
在本实例中,我们将介绍使用最基本的绘图方法。在"绘图"菜单中可以选择画各种图形;在"选项"菜单中可以选择粗细、颜色等各种选项。程序运行结果如图63-1所示。
图63-1 运行结果
技术要点
l 矢量图形和位图图形的概念
l 绘制各种基本图形
实现过程
■ 新建项目
打开Visual Studio.NET,选择"新建项目",在项目类型窗口中选择"Visual Basic项目",在模板窗口中,选择"Windows应用程序",在名称域中输入"DrawPic",然后选择保存路径。单击"确认"。
■ 制作菜单
从工具箱中向窗体上添加一个MainMenu控件,并新建菜单项"文件"、"绘图"、"选项"三个主菜单。在文件菜单下有"打开","保存"等子菜单;在"绘图"菜单下面有"直线","椭圆","矩形","圆","圆弧"等子菜单。
Dim DrawMode As Short
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim gf As Graphics = e.Graphics
Dim newpen As New Pen(Color.Black,2)
Select Case DrawMode
Case 1
'画线
newpen.Color = Color.Cornsilk
gf.DrawLine(newpen,50,200,200)
Case 2
'画矩形
newpen.Color = Color.LawnGreen
gf.DrawRectangle(newpen,10,20,170,180)
Case 3
'画椭圆
newpen.Color = Color.Olive
gf.DrawEllipse(newpen,180)
Case 4
'画圆
newpen.Color = Color.PeachPuff
gf.DrawEllipse(newpen,150,150)
Case 5
'画圆弧
newpen.Color = Color.Maroon
gf.DrawArc(newpen,300)
End Select
End Sub
Private Sub mnuDrawLine_Click(ByVal sender As Object,ByVal e As System.EventArgs) Handles mnuDrawLine.Click
DrawMode = 1
End Sub
Private Sub mnuDrawRect_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles mnuDrawRect.Click
DrawMode = 2
End Sub
Private Sub mnuDrawEllip_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles mnuDrawEllip.Click
DrawMode = 3
End Sub
Private Sub mnuDrawCircle_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles mnuDrawCircle.Click
DrawMode = 4
End Sub
Private Sub mnuDrawArc_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles mnuDrawArc.Click
DrawMode = 5
End Sub
■ 运行程序
单击菜单"调试|启动"或单击 图标运行程序。
小结
通过对本实例的学习,读者应该掌握了基本的画图原理和方法。对画点、线、矩形等各种形状的图形应该熟知于心。我们将在以后的实例中使用到这些方法。