【例6-13】 Dim a As Integer, b as integer'声明模块级变量 Private Sub Commandl Click =100:b=8对模块级变量赋值 Pint"调用sl前,模块级变量:"a="a"b=",b Call sI‘调用通用过程subl Print"调用sl后,模块级变量:","a=";a;"b="; End Sub Subs10通用过程 A=55b=6对模块级变量赋值 Pint"通用过程sl对模块级变量赋值";"a="a,"b=";b nd Sub 【例6-14】 (4)在窗体Fom1的代码窗口的顶部,声明模块级变量a和b,分别用来储存程序运行 后单击左右两个命令按钮的次数。代码如下: Private a as integ Private b As Integer (5)编写左边的命令按钮的 Command1 Clic事件过程。代码如下: Private Sub Command1 Click Dim s As string I=I+1 S=”单击按钮"&i&"次,左按钮"&a&"次” MsgBox, vboKonly,"提示 End Sub (6)编写右边的命令按钮的 Command2Clc事件过程。代码如下 Private Sub Command2 Click Dim s As string I=I+1 B=b+1 S=”单击按钮"&i&"次右按钮"&b&"次” MsgBox s, vboKonly"提示 End Sub 【例6-15】 Sub changed 声明动态变量d 声明静态变量s S=s+1
16 【例 6-13】 Dim a As Integer,b As Integer '声明模块级变量 Private Sub Command1_Click() A = 100:b = 8 '对模块级变量赋值 Print Print"调用 s1 前,模块级变量:";"a=";a;"b=";b Call s1 '调用通用过程 sub1 Print Print"调用 s1 后,模块级变量:";"a=";a;"b=";b End Sub Sub s1() '通用过程 A = 55:b = 66 '对模块级变量赋值 Print Print"通用过程 s1 对模块级变量赋值:";"a=";a;"b=";b End Sub 【例 6-14】 (4)在窗体 Form1 的代码窗口的顶部,声明模块级变量 a 和 b,分别用来储存程序运行 后单击左右两个命令按钮的次数。代码如下: Private a As Integer Private b As Integer (5)编写左边的命令按钮的 Command1_Clic 事件过程。代码如下: Private Sub Command1_Click() Dim s As String I = I + 1 A = a + 1 S = "单击按钮"& i &"次,左按钮"& a &"次" MsgBox s,vbOKOnly,"提示" End Sub (6)编写右边的命令按钮的 Command2_Clic 事件过程。代码如下: Private Sub Command2_Click() Dim s As String I = I + 1 B = b + 1 S = "单击按钮"& i &"次,右按钮"& b &"次" MsgBox s,vbOKOnly,"提示" End Sub 【例 6-15】 Sub change() Dim d As Integer '声明动态变量 d Static s As Integer '声明静态变量 s D = d + 1 S = s + 1
Print"动态变量d="d,"静态变量s="s End Sub Private Sub Commandl Click Dim i As Integer For i=1 To 3 change"或 Call change End sub 实训 (3)在Foml的窗体模块的代码窗口的最顶部(通用声明段)声明模块级变量te,用 来存储字符串。代码如下 Private title as string'表明是使用通用过程还是函数 (4)定义Sub通用过程 My Sub,来进行乘法运算。其中的形参x、y按值传递,形参z 按地址传递。代码如下 Private Sub MySub( By Val x As Integer, By Val y As Integer, z As Integer) End Sub 定义函数过程 My Fun,来进行加法运算。其中的形参m和n是按值传递的。代码如下 Private Function My Fun(By Val m As Integer, By Val n As Integer)As Integer MyFun=m+n End function (5)添加窗体的事件过程 Form load,来做些初始化的工作,将文本框置空。代码如下: Private Sub Form Load Text2 Text Text3.Te End Sub 分别添加两个单选按钮的 Click事件过程,来设置框架 Frame和标签 Label的 Caption 属性。代码如下: Private Sub Option Click Frame. Caption="乘法积运算 Labell Caption="x End Su Private Sub Option2 Click Frame. Caption="加法运算 End sub 添加命令按钮的事件过程 Command 1_ Click。其中,关键字 statIc声明的静态变量i和j 用来储存运算的次数,关键字Dm声明的过程级局部变量a、b、c用来储存三个文本框里 的数值。代码如下 Private Sub Commandl Click c i As Integer, j As Integer a As Integer, b As Integer, c As Integer
17 Print "动态变量 d = ";d,"静态变量 s = ";s End Sub Private Sub Command1_Click() Dim i As Integer For i = 1 To 3 change '或 Call change Next i End Sub 实训 (3)在 Form1 的窗体模块的代码窗口的最顶部(通用声明段)声明模块级变量 title,用 来存储字符串。代码如下: Private title As String '表明是使用通用过程还是函数 (4)定义 Sub 通用过程 MySub,来进行乘法运算。其中的形参 x、y 按值传递,形参 z 按地址传递。代码如下: Private Sub MySub(ByVal x As Integer,ByVal y As Integer,z As Integer) Z = x * y End Sub 定义函数过程 MyFun,来进行加法运算。其中的形参 m 和 n 是按值传递的。代码如下: Private Function MyFun(ByVal m As Integer,ByVal n As Integer) As Integer MyFun=m+n End Function (5)添加窗体的事件过程 Form_Load,来做些初始化的工作,将文本框置空。代码如下: Private Sub Form_Load() Text1.Text = "" Text2.Text = "" Text3.Text = "" End Sub 分别添加两个单选按钮的 Click 事件过程,来设置框架 Frame1 和标签 Label 的 Caption 属性。代码如下: Private Sub Option1_Click() Frame1.Caption = "乘法积运算" Label1.Caption = "×" End Sub Private Sub Option2_Click() Frame1.Caption = "加法运算" Label1.Caption = "+" End Sub 添加命令按钮的事件过程 Command1_Click。其中,关键字 static 声明的静态变量 i 和 j 用来储存运算的次数,关键字 Dim 声明的过程级局部变量 a、b、c 用来储存三个文本框里 的数值。代码如下: Private Sub Command1_Click() Static i As Integer,j As Integer Dim a As Integer,b As Integer,c As Integer
A= Val(TextI Text If Option Value=True Then Te="用过程运算 Call MySub(a,b, c) Forml Caption="第"&i&"次"&tite Title="用函数运算 c=MYFun Forml Caption="第"&j&"次"&tle End If Text3 Text =c End Sub 第7章 课前体验 Private Sub Framel Mouse Down(Button As Integer, Shift As Integer, X As Single, Y As Single) Forml Caption="测试鼠标事件” Select case button 左键,或用常量 Visual BasicLeftButton FrameLeft Back Color Visual BasicRed Frame Right Back Color Visual Basic White Frame. ToolTipText="朋友,您刚才在这按了鼠标左键! Case2右键,或用常量Ⅴ sual BasicRight Button FrameRight Back Color=Visual BasicRed FrameLeft Back color visual basic white Framel ToolTipText="朋友,您刚才在这按了鼠标右 【例7-1】 Private Sub Form Mouse Down( Button As Integer, Shift As Integer, XAs Single, Y As Single) 触发了 MouseDown事件 End Sub Private Sub Form MouseUp(Button As Integer, Shift As Integer, X As Single, Y As single) Print"您又触发了 MouseUp事件! 【例7-2】 Private Sub Form MouseMove(Button As Integer, Shift As Integer X As Single, Y As Single) If shift=l And Button=I Then
18 A = Val(Text1.Text) B = Val(Text2.Text) If Option1.Value=True Then Title = "用过程运算" Call MySub(a,b,c) i=i+1 Form1.Caption = "第"& i &"次"& title Else Title = "用函数运算" c=MyFun(a, b) j = j+1 Form1.Caption = "第"& j &"次"& title End If Text3.Text = c End Sub 第 7 章 课前体验 Private Sub Frame1_MouseDown(Button As Integer, Shift As Integer, _ X As Single, Y As Single) Form1.Caption = "测试鼠标事件" Select Case Button Case 1 '左键,或用常量 Visual BasicLeftButton FrameLeft.BackColor = Visual BasicRed FrameRight.BackColor = Visual BasicWhite Frame1.ToolTipText = "朋友,您刚才在这按了鼠标左键!" Case 2 '右键 ,或用常量 Visual BasicRightButton FrameRight.BackColor = Visual BasicRed FrameLeft.BackColor = Visual BasicWhite Frame1.ToolTipText = "朋友,您刚才在这按了鼠标右键!" End Select End Sub 【例 7-1】 Private Sub Form_MouseDown(Button As Integer,Shift As Integer,_ X As Single,Y As Single) Print"您触发了 MouseDown 事件!" End Sub Private Sub Form_MouseUp(Button As Integer, Shift As Integer,_ X As Single,Y As Single) Print"您又触发了 MouseUp 事件!" End Sub 【例 7-2】 Private Sub Form_MouseMove(Button As Integer,Shift As Integer,_ X As Single,Y As Single) If Shift=1 And Button=1Then
Pn"鼠标指针当前横坐标Ⅹ="X Pint"鼠标指针当前纵坐标Y=";Y 【例7-3】 1)首先,在窗体模块的顶部声明一个逻辑变量 paint,如下 Private paint As Boolean (2)定义窗体上的按下鼠标按键的事件过程,使得变量 paint的值在按鼠标左键时为 true。再定义释放鼠标按键的事件过程,使得变量 paint的值为 false。代码如下: Private Sub Form Mouse Down(Button As Integer, Shift As Integer, XAs Single, Y As Single) If Button=I Then End Sub Private Sub Form MouseUp(Button As Integer, Shift As Integer XAs Single, Y As Single) End Sub (3)定义窗体上的鼠标移动事件过程。 Private Sub Form MouseMove(Button As Integer, Shift As Integer, XAs Single, Y As Single) If paint Then' paint为tue时,程序画出轨迹点 PSet(X,Y End Sub 【例7-4】 (2)定义窗体的 Form load事件过程,使列表框中添加几个选项。代码如下 Private Sub Form Load Listl AddItem"O-Default" Listl. addiltem2-Cross ListI AddItem"3-I-Beam' End Sub (3)定义列表框的单击事件过程 Private Sub List Click Forml. MousePointerListl ListIndex End sub 【例75】 Private Sub Form KeyPress(KeyAscii As Integer)
19 Cls Print"鼠标指针当前横坐标 X=";X Else Cls Print"鼠标指针当前纵坐标 Y=";Y End If End Sub 【例 7-3】 1)首先,在窗体模块的顶部声明一个逻辑变量 paint,如下: Private paint As Boolean (2)定义窗体上的按下鼠标按键的事件过程,使得变量 paint 的值在按鼠标左键时为 true。再定义释放鼠标按键的事件过程,使得变量 paint 的值为 false。代码如下: Private Sub Form_MouseDown(Button As Integer,Shift As Integer,_ X As Single,Y As Single) If Button=1 Then paint=True End If End Sub Private Sub Form_MouseUp(Button As Integer, Shift As Integer, _ X As Single, Y As Single) paint = False End Sub (3)定义窗体上的鼠标移动事件过程。 Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _ X As Single, Y As Single) If paint Then ’paint 为 true 时,程序画出轨迹点 PSet(X,Y) End If End Sub 【例 7-4】 (2)定义窗体的 Form_load 事件过程,使列表框中添加几个选项。代码如下: Private Sub Form_Load() List1.AddItem"0-Default" List1.AddItem"1-Arrow" List1.AddItem"2-Cross" List1.AddItem"3-I-Beam" End Sub (3)定义列表框的单击事件过程。 Private Sub List1_Click() Form1.MousePointer=List1.ListIndex End Sub 【例 7-5】 Private Sub Form_KeyPress(KeyAscii As Integer)
If Key Ascii >=97 And KeyAscii <=122 Then Print小写字母:"Chr( KeyAscii);", ASCII码:" Key Ascii If KeyAscii >=65 And KeyAsci<=90 Then Print大写字母:",Chr( KeyAscii,", AsCII码:" KeyAscii End if End Sub 【例7-6】 Private Sub Form Key Down(Key Code As Integer, Shift As Integer) Dim color As String olorChr(Key Code) Select Case color Labell BackColor Visual Basic White CaseR" Labell. Back ColorVisual asicRed CaseG Labell Back Color visual basicgI Case " B Labell. Back ColorVisual BasicBlue End Selec End Sub 【例7-7】 Private Sub Form Key Down( Key Code As Integer, Shift As Integer Select Case shift Labell Caption="你按了【 SHIFT】键!" Labell Caption="你按了【CTRL】键 Case4 Labell Caption="你按了【ALT】键 Case3 Labell Caption="你同时按了SHFT和【CTRL】键! Labell Caption="你同时按了SHFT和【ALT】键! Case Labell Caption="你同时按了CTRL和【ALT】键! Labell Caption="你同时按了 SHIFT、CTRL和【ALT】键 End Select End sub 【例79】 Private Sub Form Click
20 If KeyAscii >= 97 And KeyAscii <= 122 Then Print"小写字母:";Chr(KeyAscii);",ASCII 码:";KeyAscii End If If KeyAscii >= 65 And KeyAscii<=90 Then Print"大写字母:";Chr(KeyAscii);",ASCII 码:";KeyAscii End If End Sub 【例 7-6】 Private Sub Form_KeyDown(KeyCode As Integer,Shift As Integer) Dim color As String color=Chr(KeyCode) Select Case color Case"W" Label1.BackColor = Visual BasicWhite Case"R" Label1.BackColor=Visual BasicRed Case"G" Label1.BackColor=Visual BasicGreen Case "B" Label1.BackColor=Visual BasicBlue End Select End Sub 【例 7-7】 Private Sub Form_KeyDown(KeyCode As Integer,Shift As Integer) Select Case Shift Case1 Label1.Caption="你按了【SHIFT】键!" Case2 Label1.Caption="你按了【CTRL】键!" Case4 Label1.Caption="你按了 【ALT】键!" Case3 Label1.Caption="你同时按了 SHIFT 和【CTRL】键!" Case5 Label1.Caption="你同时按了 SHIFT 和【ALT】键!" Case6 Label1.Caption="你同时按了 CTRL 和【ALT】键!" Case7 Label1.Caption="你同时按了 SHIFT、CTRL 和【ALT】键!" End Select End Sub 【例 7-9】 Private Sub Form_Click()