打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
commondialog1 属性及用法

''把输出的文件存放到这个文件夹

Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _

"SHGetPathFromIDListA" (ByVal pIdl As Long, ByVal pszPath As String) As Long

Private Type BrowseInfo

hwndOwner As Long

piDLroot As Long

pszdisplayName As String

lpsztitle As String

ulFlags As Long

lpfncallback As Long

lParam As Long

iImage As Long

End Type

Private Sub Command3_Click()

Dim bi As BrowseInfo

Dim folderid As Long

Dim pb As String

With bi

      .hwndOwner = Me.hWnd

      .lpsztitle = "把输出的文件存放到这个文件夹:"

      .ulFlags = 3

End With

folderid = SHBrowseForFolder(bi)

If folderid = 0 Then Exit Sub

     pb = String$(260, 0)

     SHGetPathFromIDList folderid, pb

     pb = Left$(pb, InStr(pb, vbNullChar) - 1)

     Text5.Text = pb

End Sub

----------------------------------------------------------------------------------------------------------

操作步骤如下:

图10-8 在当前窗体内插入的控件

(1)在当前窗体上插入通用对话框控件,其Name属性值为默认值(CommonDialog1),然后插入两个按钮:Command1(其Caption值为“打开”)和Command2(其Caption属性值为“保存”)如下图10-8所示;

(2)为“打开”和“保存”按钮编写的程序代码如下

“打开”的代码为:

Private Sub Command1_Click()

CommonDialog1.FileName = ""

CommonDialog1.Flags = cdlOFNFileMustExist

CommonDialog1.Filter = "All Files|*.*|(*.exe)|*.exe|(*.doc)|*.doc|(*.txt)|*.txt"

CommonDialog1.FilterIndex = 4

CommonDialog1.DialogTitle = "打开文件(*.txt)"

CommonDialog1.Action = 1

If CommonDialog1.FileName = "" Then

MsgBox "没有选定文件", 37, "出错提示"

Else

Open CommonDialog1.FileName For Input As #1

Do Until Not EOF(1)

Input #1, a$

Print a$

Loop

End If

End Sub

此事件过程用于建立一个“打开”对话框,程序运行时在这个对话框内选择要打开的文件,双击此文件(或选“打开”按钮),所选择的文件名即为该对话框的FileName属性的值。但此时并没有实际执行打开一个文件的操作。如果要打开这个文件,还应该编写相应的程序代码来处理,如可用下面的语句来完成。

Open CommonDialog1.FileName For Input As #1

Do Until Not EOF(1)

Input #1, a$

Print a$

Loop

即选定被打开的文件后,从该文件中逐个记录读入,并在窗体上显示出来。

图10-9 “打开”文件对话框

“打开”对话框的执行结果如下图10-9所示

“保存”的代码为:

Private Sub Command2_Click()

CommonDialog1.CancelError = False

CommonDialog1.DefaultExt = "DOC"

CommonDialog1.FileName = "example.doc"

CommonDialog1.Filter = "Document Files(*.doc)|*.doc|All Files(*.*)|*.*|Text Files(*.txt)|*.txt"

CommonDialog1.FilterIndex = 1

CommonDialog1.DialogTitle = "保存文件"

CommonDialog1.Flags = 2050

CommonDialog1.Action = 2

End Sub

与“打开”对话框一样,“保存文件”对话框也没有实际执行保存文件的功能,用户应该自己编写用来实现保存文件操作的程序代码,在实际应用中,可根据实际情况进行相关的操作。

Private Sub Command1_Click()

CommonDialog1.CancelError = False

CommonDialog1.DefaultExt = "txt"

CommonDialog1.FileName = "文档.txt"

CommonDialog1.Filter = "文本文档(*.txt)|*.txt|所有文件(*.*)|*.*"

CommonDialog1.FilterIndex = 1

CommonDialog1.DialogTitle = "导出"

CommonDialog1.Flags = 2050

CommonDialog1.Action = 2

Open CommonDialog1.FileName For Output As #1

Write #1, Text1.Text

Close #1

End Sub

==========================================

CommonDialog 控件提供诸如打开和保存文件、设置打印选项、选择颜色和字体等操作的一组标准对话框。运行 Windows 帮助引擎时,控件还能够显示帮助。

CommonDialog 控件在 Visual Basic 和 Microsoft Windows 动态连接库Commdlg.dll 例程之间提供了接口。为了用该控件创建对话框,必须要求Commdlg.dll 在 Microsoft Windows \System 目录下。

为了在应用程序中使用 CommonDialog 控件,应将其添加到窗体上并设置属性。控件显示的对话由控件的方法决定。运行时,调用相应方法后将显示对话框或执行帮助引擎;设计时在窗体上将 CommonDialog 控件显示成一个图标。此图标的大小不能改变。

CommonDialog 控件可以显示如下常用对话框:

“打开”

“另存为”

“颜色”

“字体”

“打印”

要使用 CommonDialog 控件

1. 若未添加 CommonDialog 控件,则应从“工程”菜单中选定“部件”,将控件添加到工具箱中。在标记对话的“控件”中找到并选定控件,然后单击“确定”按钮。

2. 单击工具箱中的“CommonDialog”控件并在窗体上绘制该控件。在窗体上绘制 CommonDialog 控件时,控件将自动调整大小。象 Timer控件一样,CommonDialog 控件在运行时不可见。

3. 运行时,请适当使用下表所列方法显示需要的对话。

显示“打开”和“另存为”对话框

有了“打开”对话框就可指定驱动器、目录、文件扩展名和文件名。“另存为”对话框在外观上与“打开”对话框相同,只是对话框的标题和文件名是暗淡的。运行时选定文件并关闭对话框后,可用 FileName 属性获取选定的文件名。

要显示“打开”对话框

1. 指定在“文件类型”列表框中显示的文件过滤器列表。

可用下列格式设置 Filter 属性:

description1 | filter1 | description2 | filter2...

Description 是列表框中显示的字符串——例如,"Text Files (*.txt)"。Filter 是实际的文件过滤器——例如,"*.txt"。每个description | filter 设置间必须用管道符号分隔 (|)。

2. 用 ShowOpen 方法显示对话框。

选定文件后可用 FileName 属性获取选定文件的名称。

对所有公共对话框,当 CancelError 属性为 True,而且用户单击了对话框的“取消”按钮时将生成一个错误。在显示对话框时捕获错误,以此检测是否按了“取消”按钮。

下列代码显示“打开”对话框并以选定的文件名为打开文件过程的参数:

Private Sub mnuFileOpen_Click ()

'CancelError 为 True。

On Error GoTo ErrHandler

'设置过滤器。

CommonDialog1.Filter = "All Files (*.*)|*.*|Text _

Files (*.txt)|*.txt|Batch Files (*.bat)|*.bat"

'指定缺省过滤器。

CommonDialog1.FilterIndex = 2

'显示“打开”对话框。

CommonDialog1.ShowOpen

'调用打开文件的过程。

OpenFile (CommonDialog1.FileName)

Exit Sub

ErrHandler:

'用户按“取消”按钮。

Exit Sub

End Sub

使用“颜色”对话框

可用“颜色”对话框在调色板中选择颜色,或者创建并选定自定义颜色。运行时,选定颜色并关闭对话框后可用 Color 属性获取选定的颜色。

要显示“颜色”对话框

1. 将CommonDialog控件的Flags属性设置成Visual Basic常数cdlCCRGBInit。

2. 用 ShowColor 方法显示对话框。

可用 Color 属性获取选定颜色的 RGB 值。单击“Command1”命令按钮时,下列代码将显示“颜色”对话框:

Private Sub Command1_Click ()

'将 Cancel 设置成 True。

CommonDialog1.CancelError = True

On Error GoTo ErrHandler

'设置 Flags 属性。

CommonDialog1.Flags = cdlCCRGBInit

'显示“颜色”对话框。

CommonDialog1.ShowColor

'将窗体的背景颜色设置成选定的'颜色。

Form1.BackColor = CommonDialog1.Color

Exit Sub

ErrHandler:

'用户按了“取消”按钮。

Exit Sub

End Sub

使用“字体”对话框

“字体”对话框根据大小、颜色、样式选择字体。用户一旦在“字体”对话框中选定字体后,下列属性就会包含有关用户选项的信息。

要显示“字体”对话框

1. 将 Flags 属性设置为下述 Visual Basic 常数之一:

cdlCFScreenFonts(屏幕字体)

cdlCFPrinterFonts(打印机字体)

cdlCFBoth(既可以是屏幕字体又可以是打印机字体)

警告 在显示“字体”对话框之前必须将 Flags 属性设置为这些数值之一,否则将发生字体不存在错误。

2. 用 ShowFont 方法显示对话框。

下列代码根据用户在“字体”对话框中的选择来设置文本框的字体属性:

Private Sub Command1_Click ()

'将 Cancel 设置成 True。

CommonDialog1.CancelError = True

On Error GoTo ErrHandler

'设置 Flags 属性。

CommonDialog1.Flags = cdlCFBoth Or cdlCFEffects

'显示“字体”对话框。

CommonDialog1.ShowFont

'根据用户的选择来设置文本属性。

Text1.Font.Name = CommonDialog1.FontName

Text1.Font.Size = CommonDialog1.FontSize

Text1.Font.Bold = CommonDialog1.FontBold

Text1.Font.Italic = CommonDialog1.FontItalic

Text1.Font.Underline = CommonDialog1.FontUnderline

Text1.FontStrikethru = CommonDialog1.FontStrikethru

Text1.ForeColor = CommonDialog1.Color

Exit Sub

ErrHandler:

'用户按了“取消”按钮。

Exit Sub

End Sub

如何查找一个文件是否存在

Private Declare Function SearchTreeForFile Lib "imagehlp" (ByVal RootPath As String, ByVal InputPathName As String, ByVal OutputPathBuffer As String) As Long

Private Const MAX_PATH = 260

Private Sub Form_Load()

Dim tempStr As String, Ret As Long

'create a buffer string

tempStr = String(MAX_PATH, 0)

'returns 1 when successfull, 0 when failed

Ret = SearchTreeForFile("c:\", "notepad.exe", tempStr)

If Ret <> 0 Then

MsgBox "Located file at " + Left$(tempStr, InStr(1, tempStr, Chr$(0)) - 1)

Else

MsgBox "File not found!"

End If

End Sub  

在编程时经常会用到判断文件是否存在,比如对文件做读写操作前,或是判断密钥文件是否存在等。判断的方法有很多,有些方法虽很实用,但有点繁琐。其实还可以有更简单的方法,就是使用vb 6.0提供的filesystemobject对象。

  filesystemobject对象不是vb内置对象,使用前必须首先选择[工程]→[引用],在出现的窗口中选择“microsoft scripting runtime”,然后利用filesystemobject的fileexists方法来判断文件是否存在。示例程序代码如下:

  private sub command1_click()

   ′引用filesystemobject对象

   dim fs as new filesystemobject

   ′利用filesystemobject对象的fileexists

   ′方法判断文件是否存在

   if fs.fileexists(″文件路径和文件名″) then

   msgbox ″文件存在″

   else

   msgbox ″文件不存在″

   end if

  end sub

  灵活运用filesystemobject对象可以解决与文件操作有关的大部分问题

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
谈vb目录文件操作的三种方法
VB— CommonDialog 控件的使用
公用对话框CommonDialog的 用法!
DriveListBox、DirListBox、FileListBox(三个列表框)和CommonDialog(公共对话框控件)
《Visual Basic程序设计(第4版)》第10章 对话框设计
计算机技术基础(第十章 界面设计 )
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服