1、Windows绘图 一、实验目的: 了解windows绘图的一般过程; 增强对课本基本原理的理解; 以vc++为主,学习高级语言图形编程技术 二、实验过程: 该实验采用visual basic作为编程语言,首先根据VB的特性制定一下的整体思路: 主要实现曲线、圆、点、直线等图形的绘制,利用鼠标的mousemove、mousedown、mouseup三个事件,分情况进行处理,以此实现对不同选项的响应。 界面如下: 主要代码: Dim ljs As Integer '必须定义为全局变量 Dim drawstate As Boolean '定义画笔状
2、态,左键按下为有效 Dim prex As Single '坐标初始值,在左键按下后记录 Dim prey As Single '坐标初始值,在左键按下后记录 Private Sub Combo1_Change() Picture1.DrawWidth = Val(Combo1) End Sub Private Sub Command1_Click() ljs = 1 Label4.Caption = "温馨提示:请拖动鼠标左键绘制曲线,右键单击取消该操作" 'line End Sub Private Sub Com
3、mand2_Click() ljs = 2 Label4.Caption = "温馨提示:单击鼠标左键确定圆心,拖动直至松开鼠标左键,右键单击取消该操作" 'circle End Sub Private Sub Command3_Click() ljs = 3 Label4.Caption = "温馨提示,单击鼠标左键即可" 'point End Sub Private Sub Command4_Click() ljs = 4 'line Label4.Caption = "温馨提示:按住鼠标左键,拖动直至松开鼠标左键" End Sub Private
4、 Sub Command5_Click() Form2.Show Form1.Visible = True End Sub Private Sub Command6_Click() ljs = 6 Label4.Caption = "注记,输入文字后,按鼠标左键确定位置" Char = InputBox("请输入注记文字", "注记", "surveying") End Sub Private Sub Form_Load() Text1 = "" Text2 = "" Label4.Caption = "欢迎使用本绘图程序,请根据提示进行操作" End Sub
5、 '以下为三个鼠标事件,针对前面的选项依次处理 Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) '分选项进行讨论 If ljs = 1 Then If Button = 1 Then Picture1.DrawWidth = 1 drawstate = True '此处做一个标记 prex = X prey = Y End If If Button
6、 2 Then ljs = 0 ' 鼠标右键终止当前画笔 End If End If If ljs = 2 Then If Button = 1 Then drawstate = True '此处做一个标记 prex = X prey = Y End If If Button = 2 Then ljs = 0 ' 鼠标右键终止当前画笔 End If End If If ljs = 3 Th
7、en If Button = 1 Then Picture1.DrawWidth = 5 'drawwidth是全局属性 Picture1.PSet (X, Y) End If If Button = 2 Then ljs = 0 End If End If If ljs = 4 Then If Button = 1 Then prex = X prey = Y End If End If End Sub Private Sub Picture1_MouseMove(Button As Integer, Shi
8、ft As Integer, X As Single, Y As Single) Text1 = X Text2 = Y '显示当前坐标 If ljs = 1 Then If drawstate = True Then Picture1.DrawWidth = 1 Picture1.Line (prex, prey)-(X, Y) '指定在picture1中操作 prex = X prey = Y End If End If : End Sub Private Sub
9、Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If ljs = 1 Then If Button = 1 Then drawstate = False End If End If If ljs = 2 Then Picture1.DrawWidth = 1 Dim r As Single If Button = 1 Then drawstate = False End If
10、 r = Sqr((X - prex) * (X - prex) + (Y - prey) * (Y - prey)) Picture1.Circle (prex, prey), r End If If ljs = 4 Then Picture1.DrawWidth = 1 If Button = 1 Then Picture1.Line (prex, prey)-(X, Y) End If End If End Sub 签名代码 Private Sub Command1_Click() Form2.Show Fo
11、rm3.Visible = False End Sub Private Sub Command2_Click() Dim i As Integer Picture1.Scale (0, 0)-(31, 31) For i = 1 To 30 Picture1.Line (i, 0)-(i, 41) Picture1.Line (0, i)-(31, i) Next i Command3.Enabled = True End Sub Private Sub Command3_Click() Dim i As Integer Dim j As Integer
12、 Picture1.DrawWidth = 1 For i = 1 To 30 For j = 1 To 30 Picture1.Circle (i, j), 0.2 '必须指明对picture进行操作,默认是form Next j Next i Command4.Enabled = True End Sub Private Sub Command4_Click() Label2.Caption = "李继尚个人签名" Picture1.DrawWidth = 5 '可通过禁止该按钮的使用防止字体过大 Dim i As Integer For i
13、 = 2 To 9 Picture1.PSet (i, 3), vbBlue '点的颜色 Next i For i = 2 To 6 Picture1.PSet (6, i), vbBlue Next i For i = 4 To 8 Picture1.PSet (i, 6), vbBlue Next i Picture1.PSet (5, 4), vbBlue Picture1.PSet (4, 5), vbBlue Picture1.PSet (3, 6), vbBlue Picture1.PSet (7, 4), vbBlue Picture1.PS
14、et (8, 5), vbBlue Picture1.PSet (9, 6), vbBlue Picture1.PSet (7, 7), vbBlue For i = 8 To 11 Picture1.PSet (6, i), vbBlue Next i For i = 3 To 9 Picture1.PSet (i, 9), vbBlue Next i For i = 3 To 10 Picture1.PSet (14, i), vbBlue Next i For i = 11 To 13 Picture1.PSet (i, 6), vbBlue Next
15、 i Picture1.PSet (13, 3), vbBlue Picture1.PSet (13, 4), vbBlue For i = 11 To 13 Picture1.PSet (i, 8), vbBlue Next i Picture1.PSet (12, 7), vbBlue Picture1.PSet (12, 9), vbBlue Picture1.PSet (11, 10), vbBlue Picture1.PSet (12, 5), vbBlue For i = 15 To 19 Picture1.PSet (i, 6), vbBlue Ne
16、xt i For i = 3 To 9 Picture1.PSet (17, i), vbBlue Next i Picture1.PSet (15, 4), vbBlue Picture1.PSet (16, 5), vbBlue Picture1.PSet (19, 4), vbBlue Picture1.PSet (18, 5), vbBlue Picture1.PSet (16, 7), vbBlue Picture1.PSet (15, 8), vbBlue Picture1.PSet (18, 7), vbBlue Picture1.PSet (19, 8)
17、 vbBlue For i = 14 To 19 Picture1.PSet (i, 10), vbBlue Next i For i = 5 To 10 Picture1.PSet (23, i), vbBlue Next i Picture1.PSet (24, 4), vbBlue Picture1.PSet (23, 3), vbBlue Picture1.PSet (26, 4), vbBlue Picture1.PSet (27, 3), vbBlue Picture1.PSet (23, 5), vbBlue Picture1.PSet (23, 1
18、0), vbBlue For i = 5 To 10 Picture1.PSet (28, i), vbBlue Next i Picture1.PSet (27, 9), vbBlue For i = 23 To 28 Picture1.PSet (i, 5), vbBlue Next i For i = 2 To 5 Picture1.PSet (25, i), vbBlue Next i Picture1.PSet (25, 7), vbBlue Picture1.PSet (26, 7), vbBlue Picture1.PSet (25,
19、8), vbBlue Picture1.PSet (26, 8), vbBlue End Sub Private Sub Form_Load() Text1 = "" Text2 = "" Command3.Enabled = False Command4.Enabled = False Label2.Caption = "" End Sub Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Text1 = X Text2 = Y End Sub 四、实验结果: 五、实验体会: 熟练运用visual basic,编写了简单的绘图程序。






