VB第3讲ActiveX控件添加ActiveX控件:菜单工程-部件-控件3.1文档编辑器3.1.1通用对话框MSCommonDialogControl6.0核心方法ShowOpen显示“打开”对话框ShowSave显示“另存为”对话框ShowColor显示“颜色”对话框ShowFont显示“字体”对话框ShowPrinter显示“打印”对话框ShowHelp运行WINHLP32.EXE3.1.2高级的文档控件:MSRichTextBoxControl可以为文档各部分设置不同的格式。特色方法LoadFile显示文件SaveFile保存文件特色属性:SelFontName,SelFontSize,SelBold,SelItalic3.1.3简单的文档编辑器PrivateSubcmdOpen_Click()CommonDialog1.Flags=cdlOFNFileMustExistCommonDialog1.Filter=RichTextFormat(*.rtf)|*.rtfCommonDialog1.ShowOpenRichTextBox1.LoadFileCommonDialog1.FileName,rtfRTFEndSubPrivateSubcmdFormatFont_Click()CommonDialog1.Flags=cdlCFBoth'装入字体CommonDialog1.ShowFontWithRichTextBox1.SelFontName=CommonDialog1.FontName.SelFontSize=CommonDialog1.FontSize.SelBold=CommonDialog1.FontBold.SelItalic=CommonDialog1.FontItalicEndWithEndSubPrivateSubcmdFormatColor_Click()CommonDialog1.ShowColorRichTextBox1.SelColor=CommonDialog1.colorEndSubPrivateSubcmdSaveAs_Click()CommonDialog1.Flags=cdlOFNOverwritePromptCommonDialog1.Filter=RichTextFormat(*.rtf)|*.rtfCommonDialog1.ShowSaveRichTextBox1.SaveFileCommonDialog1.FileName,rtfRTFEndSub3.2MSWindowsCommonControls6.0MSCOMCTL.OCX3.2.1进度条控件ProgressBarPrivateSubForm_Load()ProgressBar1.Align=vbAlignBottom'向窗体底部对齐ProgressBar1.Visible=FalseEndSubPrivateSubCommand1_Click()DimWorks(500)AsString,iAsInteger,jAsIntegerProgressBar1.Min=LBound(Works)ProgressBar1.Max=UBound(Works)ProgressBar1.Value=ProgressBar1.MinProgressBar1.Visible=TrueFori=LBound(Works)ToUBound(Works)Forj=1To20000'模拟一个费时的计算Works(i)=Initialvalue&iNextjProgressBar1.Value=iNextiProgressBar1.Visible=FalseEndSub3.2.2树列表控件TreeView核心属性:Nodes3.2.2.1结点关系的描述'设置外观PrivateSubForm_Load()TreeView1.LineStyle=tvwRootLinesTreeView1.Style=tvwTreelinesPlusMinusTextEndSubPrivateSubcmdAppendRoot_Click()'添加根结点DimnodXAsNodeSetnodX=TreeView1.Nodes.Add(,,r1,Root1)SetnodX=TreeView1.Nodes.Add(,,r2,Root2)EndSubPrivateSubcmdAppendChild_Click()'添加子结点DimnodXAsNodeSetnodX=TreeView1.Nodes.Add(r1,tvwChild,r11,Child11)SetnodX=TreeView1.Nodes.Add(r1,tvwChild,r12,Child12)SetnodX=TreeView1.Nodes.Add(r2,tvwChild,r21,Child21)SetnodX=TreeView1.Nodes.Add(r1,tvwChild,r22,Child22)EndSubPrivateSubTreeView1_NodeClick(ByValNodeAsMSComctlLib.Node)Caption=Node.Text&,&Node.KeyEndSubAdd的语法Add(亲属的代号,亲戚关系,代号,文字,图像,选中时图像)Add(,,r1,Root1)'根结点r1Add(r1,tvwChild,r11,Child11)'r1的子结点r113.2.2.2示例:由父子关系定义树型结构'根据txt查找结点的KeyPrivateFunctionSearch(txtAsString)AsStringDimiAsIntegerFori=1ToTreeView1.Nodes.Count'所有node的个数IfTreeView1.Nodes(i)=txtThenExitForNextiIfi=TreeView1.Nodes.CountThenSearch=TreeView1.Nodes(i).KeyElseSearch=EndIfEndFunction约定:根结点的Key:r1,r2,r3,r3....子结点的Key:父结点key连接子结点的textDimnRootAsInteger'记录根结点的个数PrivateSubForm_Load()nRoot=0TreeView1.LineStyle=tvwRootLinesTreeView1.Style=tvwTreelinesPlusMinusTextEndSubPrivateSubcmdAppendNode_Click()DimnodXAsNode,ParentKeyAsString,ChildKeyAsStringIfTrim(txtParent.Text)=ThennRoot=nRoot+1'添加根结点SetnodX=TreeView1.Nodes.Add(,,r&nRoot,txtChild.Text)ElseParentKey=Search(txtParent.Text)IfParentKeyThen'在父结点下添加子结点ChildKey=ParentKey&Trim(txtChild.Text)SetnodX=TreeView1.Nodes.Add(ParentKey,tvwChild,ChildKey,txtChild.Text)EndIfEndIfEndSub3.2.2.3上机题3:练习、改进以上程序功能上、界面上改进。3.2.3图片库控件ImageList关键属性:ListImages图片集合手工添加图片在设计阶段,通过属性页,添加图片程序添加图片ImageList1.ListImages.Addindex,key,picture'利用ImageList,实现动画效果PrivateSubForm_Load()DimPathAsStringPath=C:\ProgramFiles\MicrosoftVisualStudio\Common\Graphics\Icons\Industry\ImageList1.ListImages.Add,,LoadPicture(Path&rocket.ico)ImageList1.ListImages.Add,,LoadPicture(Path&plane.ico)ImageList1.ListImages.Add,,LoadPicture(Path&cars.ico)Timer1.Enabled=TrueEndSubPrivateSubTimer1_Timer()StaticiAsIntegeri=(i+1)Mod3Pic.Picture=ImageList1.ListImages(i+1).PictureEndSub3.2.4工具条控件ToolBar3.2.4.1文字版的工具条在属性页中,手工定义按钮工具条的组成标题关键字按钮1sOpenOpen按钮2sSaveSavePrivateSubToolbar1_ButtonClick(ByValButtonAsMSComctlLib.Button)SelectCaseButton.KeyCaseOpen:CommonDialog1.ShowOpenCaseSave:CommonDialog1.ShowSaveEndSelectEndSub3.2.4.2图形版的工具条'需要ImageList配合使用,创建动态工具条PrivateSubForm_Load()DimPathAsStringPath=C:\ProgramFiles\MicrosoftVisualStudio\Common\Graphics\bitmaps\tlbr_w95\ImageList1.ListImages.Add,Open,LoadPicture(Path&open.bmp)ImageList1.ListImages.Add,Save,LoadPicture(Path&save.bmp)Toolbar1.ImageList=ImageList1Toolbar1.Buttons.Add1,Open,,tbrDefault,OpenToolbar1.Buttons.Add2,Save,,tbrDefault,SaveEndSubAdd的语法规则:Toolbar1.Buttons.Add(index,key,caption,style,image)3.2.4.3上机题4:工具条版的文本编辑器3.2.5StatusBar控件关键属性:面板集合Panels面板对象Panel的关键属性:面板类型Style'测试程序:定义了7个不同种类的面板对象PrivateSubCommand1_Click()Dimpanel1AsPanel,iAsIntegerStatusBar1.Panels.ClearFori=0To6Setpanel1=StatusBar1.Panels.Add(,P&i)panel1.Style=iNextiStatusBar1.Panels(P0).Text=文本信息EndSub3.3其他ActiveX控件(自学)MSTabbedDialogControl:界面组织的好帮手MSSysInfoControl:了解系统情况MSCalendarControl:日历MSMultimediaControl:多媒体控件MSADODataControl6.0(OLEDB):数据库控件MSDataGridControl6.0(OLEDB):数据库表格控件3.4创建简单ActiveX控件控件目标:一个文本框负责输入姓名,一个列表框保证姓名列表中无重名,双击可以删除列表项。3.4.1设计ActiveX控件工程设计界面UserControl1控件容器txtName文本框lstNames列表框设计代码OptionExplicitPrivateSublstNames_DblClick()lstNames.RemoveItemlstNames.ListIndexEndSubPr