VB中CommonDialog示例

前端之家收集整理的这篇文章主要介绍了VB中CommonDialog示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
VB中CommonDialog示例
从project->components,选中microsoft common dialog control 6.0,点击“确定”,会有Commondialog按钮出现在控件栏中,然后拖一个到窗体,用其默认名Commondialog1。

在本程序中实现如下功能: 打开Word/Text/其它文件到Text1控件,Commondialog1.action=1/Commondialog1.ShowOpen 保存Text1中的文本到另一个文件,Commondialog1.action=2/Commondialog1.ShowSave 设置Text1中的字体颜色,Commondialog1.action=3/Commondialog1.ShowColor 设置Text1中的字体属性,Commondialog1.action=4/Commondialog1.ShowFont 打印Text1中的文本,Commondialog1.action=5/Commondialog1.ShowPrint 打开Help文档,Commondialog1.action=6/Commondialog1.ShowHelp。 具体代码如下: Private Sub cmdColor_Click() CommonDialog1.ShowColor '或用CommonDialog1.Action=3 Text1.ForeColor = CommonDialog1.Color '设置文本框的前景色 End Sub Private Sub cmdFont_Click() With CommonDialog1 .Flags = cdlCfScreenFonts Or cdlCFEffects '使对话框只列出系统支持的屏幕字体,使对话框允许下划线、删除线、及颜色效果 .Max = 100 .Min = 1 .ShowFont '也可以用.Action=4代替 End With With Text1 .FontName = CommonDialog1.FontName .FontSize = CommonDialog1.FontSize .FontBold = CommonDialog1.FontBold .FontItalic = CommonDialog1.FontItalic .FontStrikethru = CommonDialog1.FontStrikethru .FontUnderline = CommonDialog1.FontUnderline End With End Sub Private Sub cmdHelp_Click() CommonDialog1.HelpCommand = cdlHelpContents CommonDialog1.HelpFile = "C:/Windows/help/notepad.hlp" CommonDialog1.ShowHelp '或使用CommonDialog1.Action=6 End Sub Private Sub cmdOpen_Click() Dim StrText As String On Error GoTo Note '当出现错误时,跳转到Note语句 With CommonDialog1 .DialogTitle = "打开对话框" .InitDir = "D:/" .Filter = "Word文档(*.doc) |*.doc|文本文件(*.txt) |*.txt|所有文件(*.*) |*.*" .FilterIndex = 1 Text1.Text = "" .ShowOpen '或使用CommonDialog1.Action=1 Open .FileName For Input As #1 End With If Err.Number = 0 Then '如果打开文件正确 Do While Not EOF(1) Line Input #1,StrText Text1 = Text1 + StrText + vbCrLf 'vbCrLf是系统常量,为回车换行符 'Text1=Text1+StrText+(Chr(13)+(Chr(10)) Loop Close #1 End If Note: End Sub Private Sub cmdPrinter_Click() Dim i As Integer CommonDialog1.ShowPrinter '或使用CommonDialog1.Action=5 For i = 1 To CommonDialog1.Copies Printer.Print Text1.Text Next i Printer.EndDoc '结束打印 End Sub Private Sub cmdSave_Click() With CommonDialog1 .DialogTitle = "另存为对话框" .InitDir = "C:/" .Filter = "Word文档(*.doc)|*.doc|文本文件(*.txt)|*.txt|所有文件(*.*)|*.*" .FilterIndex = 1 .DefaultExt = "*.Txt" .Action = 2 '或使用CommondDialog1.ShowSave Open .FileName For Output As #2 End With For i = 1 To Len(Text1) Print #2,Mid$(Text1,i,1); Next i Close #2 End Sub ,Private Sub Form_Load() ,Dim j As Integer ,For j = 0 To Screen.FontCount - 1 ,Combo1.AddItem Screen.Fonts(j) ,Next j ,End Sub

原文链接:https://www.f2er.com/vb/258304.html

猜你在找的VB相关文章