Y As Single)
unload me
End Sub
【例9-1】
Private Sub Command1_Click()
Dim I As Integer For I = 0 To 5
Shape1(I).Shape = I ‘将控件数组中第I个控件的形状属性改
为I
Next End Sub
Private Sub Command2_Click()
Dim I As Integer For I = 0 To 5
Shape1(I).BackStyle = 1 ‘将控件的背景类型更改为“覆盖” Shape1(I).BackColor = QBColor(I) ‘更改控件的背景颜色 Next End Sub
Private Sub Command3_Click()
Dim I As Integer For I = 0 To 5
Shape1(I).FillColor = RGB(255, 255, 255) ‘将控件的填充颜色改为白
色
Shape1(I).FillStyle = I + 2 ‘更改控件的填充方式。因其值为0时为覆盖
方式,其值为1时为透明方式,看不出填充效果,故将值设为从2开始。
Next
End Sub
【例9-2】
Private Sub Command1_Click()
Line(-2,0)-(10,0) ‘画出X轴 Line(0,3)-(0,-3) ‘画出Y轴 For i = 0 To 6.28 Step 0.001 j = Sin(i)
PSet(i, j) ‘ 画出正弦函数图像 Next i End Sub
Private Sub Form_Load()
Form1.Scale(-2,3)-(10,-3) ‘定义坐标系 End Sub
【例9-3】
31
Private Sub Form_Click()
Line(500,700)-(2500,700),vbRed Line(1500,100)-(700,2000),vbGreen Line(1500,100)-(2300,2000),vbGreen Line(2500,700)-(700,2000),vbBlue Line(500,700)-(2300,2000),vbBlue End Sub 【例9-4】
Private Sub Form_Click()
Cls
r = Form2.ScaleHeight / 2 ' 圆半径为窗体高度的1/2 x0 = Form2.ScaleWidth / 2 ' 圆的中心坐标(窗体中间) y0 = Form2.ScaleHeight / 2
st = 3.1415926 / 10 ' 将圆等分为 20 份
For i = 0 To 6.283185 Step st ' 用直线将圆周上的这些点两两相连 For j = 0 To i Step st X1 = x0 + r * Cos(i) Y1 = y0 - r * Sin(i) X2 = x0 + r * Cos(j) Y2 = y0 - r * Sin(j) Line (X1, Y1)-(X2, Y2) Next j, i End Sub 【例9-5】
Private Sub Form_Click() Cls
r = Form1.ScaleHeight/4 ‘ 圆半径为窗体高度的1/4 x0 = Form1.ScaleWidth/2 ‘ 圆的中心坐标(窗体中间) y0 = Form1.ScaleHeight/2
st = 3.1415926/25 ‘ 将圆等分为 50 份
For i = 0 To 6.283185 Step st ‘ 以每一个等分点为圆心画圆 x = x0 + r * Cos(i) y = y0 - r * Sin(i) Circle(x, y),r * 0.8 Next i End Sub 【例9-6】
Private Sub Form_Click() Dim numm As Integer Picture1.AutoSize = True ‘图片框大小与图片大小一致 roww = Int(Form1.Width / Picture1.Width) + 1 ‘计算每行可放置多少个图片
coll = Int(Form1.Height / Picture1.Height) + 1 ‘计算每列可放置多少个图片
32
For i = 0 To roww For j = 0 To coll
Form1.PaintPicture Picture1.Picture, i * Picture1.Width, j * Picture1.Height, Picture1.Width, Picture1.Height
‘在窗体的行、列上复制图片 Next j Next i
Picture1.Visible = False End Sub
Private Sub Form_Load()
Picture1.Picture = LoadPicture(\‘在图片框中装入图片 Form1.Caption = \图像平铺\End Sub 【例9-7】
Private Sub Form_Click()
Picture1.Cls End Sub
Private Sub Form_Load()
Picture1.ScaleHeight = 100 '设置比例为100. Picture1.ScaleWidth = 100
Picture1.AutoRedraw = True '打开AutoRedraw Picture1.ForeColor = 0 '设置ForeColor Picture1.FillColor = QBColor(9) '设置FillColor Picture1.FillStyle = 0 '设置FillStyle Picture1.Circle (50, 50), 30 '画一个圆
Picture1.AutoRedraw = False '关闭AutoRedraw End Sub
Private Sub Picture1_Click()
Dim I
Picture1.ForeColor =RGB(Rnd * 255,0,0) '选择随机颜色. For I = 5 To 95 Step 10 '画线. Picture1.Line (I,0)-(I,100) Next I
End Sub 【例9-8】
Private Sub Form_Paint()
Dim HalfX,HalfY '声明变量.
HalfX = ScaleLeft + ScaleWidth/2 '圆半径设置到宽度的一半。
HalfY = ScaleTop + ScaleHeight/2 '圆半径设置到高度的一半。画一个圆。 If HalfX > HalfY Then
Circle (HalfX, HalfY), HalfY
33
Else
Circle (HalfX, HalfY), HalfX End If End Sub
Private Sub Form_Resize() Refresh
End Sub 实训
Private Sub Form_Click()
Form1.Scale(-2 * 3.14159,1)-(2 * 3.14159,-1) Form1.Line (-2 * 3.14159,0)-(2 * 3.14159,0) Form1.Line(0,1)-(0,-1)
Form1.CurrentX = 0.2:Form1.CurrentY = -0.1:Print\Form1.CurrentX = -3.2:Form1.CurrentY = -0.1:Print\Form1.CurrentX = 3.3:Form1.CurrentY = -0.1:Print\
Form1.CurrentX = -6.2:Form1.CurrentY = -0.1: Print \Form1.CurrentX = 5.7:Form1.CurrentY = -0.1:Print \Form1.CurrentX = 0.2:Form1.CurrentY = 0.5:Print \Form1.CurrentX = 0.2:Form1.CurrentY = -0.5:Print\For I = -6.282 To 6.282 Step 0.02 Form1.PSet (I, Cos(I)) Next I End Sub
第10章 课前体验
Private Sub Combo1_Click()
File1.Pattern = Combo1.Text ‘对文件类型进行过滤 End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path ‘目录列表框与文件列表框同步 End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive ‘目录列表框与驱动器列表框同步 End Sub
Private Sub Form_Load()
File1.Pattern = \ ‘程序运行时,默认显示EXE文件End Sub
【例10-1】
34
Private Sub Form_Load()
Open \打开输出文件
Print #1,\语句测试\向文件中输出一行数据 Print #1, '向文件中输出一个空行 Print #1,\第一分区\第二分区\分区格式输出数据
Print #1,\你好!\按紧凑、分区两种格式输出数据 Print #1, Spc(2); \前面输出2个空格\函数向文件中写入两个空格 Print #1, Tab(5); \从第5列开始输出\函数指定从第5列开始输出 Print #1, \行1\换行测试 Print #1, \行2\
Close #1 '关闭文件 End Sub 【例10-2】
(1)把文本文件的内容一行一行地读入文本框
Text1.Text = “” ‘将文本框置空 Open“D:\\Myfile.txt” For Input As #1
‘以Input方式打开文件“D:\\Myfile.txt”文件,文件号为1。
Do While Not EOF(1) ‘判断文件是否结束
Line Input #1,InputData ‘一次从文件中读入一行,赋给变量
InputData
Text1.Text = Text1.Text+InputData + vbCrlf
‘将InputData变量的值与文本框值连接
Loop Close #1
(2)把文本文件的内容一个字符一个字符地读入文框 Dim InputData As String Text1.Text = “”
Open “D:\\Myfile.txt” For Input As #1
Do While Not EOF(1) ‘判断文件是否结束
InputData=Input(1,#1) ‘每次读一个字符
Text1.Text=Text1.Text+InputData ‘与文本框内容相连接 Loop Close #1
(3)把文本文件的内容一次性读入文本框(仅限于包含西文字符的文本方件)
Text1.Text = \
Open \
Text1.Text = Input(LOF(1), #1)
‘将整个文件的内容全部读出,在文本框中显示出来。
Close #1 【例10-3】
Private Type Student '用户自定义数据类型
Name As String * 8
35