【例8-7】
(2)编写命令按钮Command1(设置颜色)的单击事件过程:
Private Sub Command1_Click()
CommonDialog1.ShowColor '或CommonDialog1.Action=3 Text1.ForeColor=CommonDialog1.Color End Sub
(3)编写命令按钮Command2(设置字体)的单击事件过程: Private Sub Command2_Click()
CommonDialog1.Action = 4 '或CommonDialog1.ShowFont Text1.FontName = CommonDialog1.FontName Text1.FontSize = CommonDialog1.FontSize Text1.FontBold = CommonDialog1.FontBold Text1.FontItalic = CommonDialog1.FontItalic
Text1.FontUnderline = CommonDialog1.FontUnderline End Sub
【例8-8】
(2)编写程序代码如下:
Private Sub Form_Load()
StatusBar1.Panels(1).Text=Date End Sub
该事件过程使得程序启动后,状态栏窗格1上显示当前的日期。 Private Sub Text1_KeyPress(KeyAscii As Integer) StatusBar1.Panels(2).Text = \文本框1上输入\End Sub
当在文本框Text1上输入时,调用该事件过程,使得状态栏窗格2上显示“文本框1上输入”。
Private Sub Text2_KeyPress(KeyAscii As Integer) StatusBar1.Panels(2).Text = \文本框2上输入\End Sub
【例8-10】
(3)编写3个复选框的事件过程: Private Sub Check1_Click()
Text1.FontBold = Not Text1.FontBold End Sub
Private Sub Check2_Click()
Text1.FontItalic = Not Text1.FontItalic End Sub
Private Sub Check3_Click()
Text1.FontUnderline = Not Text1.FontUnderline End Sub
(4)编写命令按钮的事件过程:
26
Private Sub Command1_Click() CommonDialog1.ShowColor
Text1.ForeColor = CommonDialog1.Color End Sub 【例8-11】
Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button) Select Case Button.Index Case 1
Text1.FontBold = Button.Value Case 2
Text1.FontItalic = Button.Value Case 3
Text1.FontUnderline = Button.Value Case 5
CommonDialog1.ShowColor
Text1.ForeColor = CommonDialog1.Color End Select End Sub 【例8-12】
(3)在MDIForm1的代码窗口中,编写代码。
在顶部通用声明段,声明变量n来储存子窗体编号,代码如下: Private n As Integer
编写“文件”菜单中各菜单项的事件过程,代码如下: Private Sub menuNew_Click() N ﹦ n + 1
Dim Fm As New Form1 '定义窗体 Load Fm '装载窗体 Fm.Caption = \新文档\End Sub
该过程,定义一个子窗体对象,然后加载显示在父窗体中,并设置了窗体标题。Private Sub menuExit_Click() End End Sub
该过程关闭应用程序。
编写“窗口排列”菜单中各菜单项的事件过程,代码如下: Private Sub menuCascade_Click() MDIForm1.Arrange0 End Sub
Private Sub menuH_Click() MDIForm1.Arrange1 End Sub
Private Sub menuV_Click() MDIForm1.Arrange2
27
End Sub 实训
(3)编写窗体模块Form1里的代码
①在代码窗口顶部声明一个公用的模块级变量user,用来保存用户名。代码如下: Public user As String'用户名变量 ②编写命令按钮的单击事件过程: Private Sub Command1_Click() If Option1.Value = True Then User = Text1.Text & \先生\ Else
User = Text1.Text & \女士\ End If
Form1.Hide Form2.Show End Sub
调用该过程,变量user根据用户对单选按钮的选择来保存“某某先生”或“某某女士”,然后隐藏登陆界面(窗体Form1),显示主界面(窗体Form2)。
Private Sub Command2_Click() End End Sub
该过程关闭应用程序。
(4)编写窗体模块Form2里的代码
①在代码窗口顶部声明一个模块级变量clip,当成剪贴板来保存字符串。代码如下: Private clip As String
②窗体加载的事件过程如下: Private Sub Form_Load()
StatusBar1.Panels(1).Text = Form1.user End Sub
该过程使状态栏的第一个窗格里显示用户名。 ③高级文本框的change事件过程如下: Private Sub RichTextBox1_Change()
StatusBar1.Panels(2).Text = \正在输入...\End Sub
该过程使状态栏的第二个窗格里显示输入状态。 ④计时器的Timer事件过程如下: Private Sub Timer1_Timer()
StatusBar1.Panels(3).Text = \时钟:\End Sub
该过程使状态栏的第3个窗格里显示当前时间。 ⑤工具栏的按钮单击事件过程如下:
Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button) Select Case Button.Index
28
Case 1
RichTextBox1.Text = \ Case 2
CommonDialog1.ShowPrinter Case 3
RichTextBox1.SelBold = Not RichTextBox1.SelBold Case 4
RichTextBox1.SelItalic = Not RichTextBox1.SelItalic Case 5
RichTextBox1.SelUnderline = Not RichTextBox1.SelUnderline Case 6
CommonDialog1.ShowColor
RichTextBox1.SelColor = CommonDialog1.Color Case 8
MsgBox \可用菜单或工具栏来操作\关于文本编辑器\ End Select End Sub
⑥为高级文本框RichTextBox1编写鼠标MouseDown事件过程,如下: Private Sub RichTextBox1_MouseDown(Button As Integer,Shift As Integer,_ x As Single,y As Single) If Button = 2 Then
If RichTextBox1.SelText<>\ menuCut.Enabled = True menuCopy.Enabled = True End If
PopupMenu Edit End If End Sub
⑦为“编辑”菜单中的各菜单项编写单击事件过程 Private Sub menuCut_Click()
Clip = RichTextBox1.SelText
RichTextBox1.SelText = \ '将选定的字符清除 menuCut.Enabled = False
menuCopy.Enabled = False '将\剪切\与\复制\菜单项设为无效 menuPaste.Enabled = True End Sub
Private Sub menuCopy_Click() clip = RichTextBox1.SelText menuCut.Enabled = False menuCopy.Enabled = False menuPaste.Enabled = True End Sub
29
Private Sub menuPaste_Click()
RichTextBox1.SelText = clip '将变量clip中的内容粘贴到光标所在处 End Sub
上述3个菜单项的事件过程分别实现了RichTextBox1上的复制、剪切和粘贴的功能。 Private Sub menuSelAll_Click() RichTextBox1.SelStart = 0
RichTextBox1.SelLength = Len(RichTextBox1.Text) End Sub
第9章 课前体验
(3)双击窗体,在窗体的Load事件中编写如下程序:
Dim Snow(1000, 2), Amounty As Integer Private Sub Form_Load()
Form1.Show DoEvents Randomize
Amounty = 325
For J = 1 To Amounty
Snow(J,0) = Int(Rnd * Form1.Width) Snow(J,1) = Int(Rnd * Form1.Height) Snow(J,2) = 10 + (Rnd * 20) Next J
Do While Not (DoEvents = 0) For LS = 1 To 10 For I = 1 To Amounty
OldX = Snow(I, 0): OldY = Snow(I, 1) Snow(I, 1) = Snow(I, 1) + Snow(I, 2) If Snow(I, 1) > Form1.Height Then
Snow(I, 1) = 0: Snow(I, 2) = 5 + (Rnd * 30) Snow(I, 0) = Int(Rnd * Form1.Width) OldX = 0: OldY = 0 End If
Coloury = 8 * (Snow(I, 2) - 10): Coloury = 60 + Coloury PSet (OldX, OldY), QBColor(0)
PSet (Snow(I, 0), Snow(I, 1)), RGB(Coloury, Coloury, Coloury) Next I Next LS Loop End End Sub
(4)编写窗体的鼠标按下代码,在窗体的MouseDown事件中编程:
Private Sub Form_MouseDown(Button As Integer,Shift As Integer, X As Single,
30