CommonDialog 控件(“打开”、“另存为”对话框)
通过使用 CommonDialog 控件的 ShowOpen 和 ShowSave
方法可显示“打开”和“另存为”对话框。
两个对话框均可用以指定驱动器,目录,文件扩展名和文件名。除对话的标题不同外,另存为对话外观上与打开对话相似。
在运行时,当用户选择一个文件“关闭”对话框时,FileName 属性既为选定的文件名。
可以设置 Filter 属性,这样对话就只显示某种文件类型,如文本文件。Flags
属性可用来改变对话的元素,当诸如覆盖文件之类的动作发生时,还可用来提示用户。
CommonDialog
控件(“打开”、“另存为”对话框)示例
下例显示“打开”对话框然后在信息框中显示所选的文件名:
Private Sub Command1_Click()
' 设置“CancelError”为 True
CommonDialog1.CancelError = True
On Error GoTo ErrHandler
' 设置标志
CommonDialog1.Flags = cdlOFNHideReadOnly
' 设置过滤器
CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & _
"(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
' 指定缺省的过滤器
CommonDialog1.FilterIndex = 2
' 显示“打开”对话框
CommonDialog1.ShowOpen
' 显示选定文件的名字
MsgBox CommonDialog1.FileName
Exit Sub
ErrHandler:
' 用户按了“取消”按钮
Exit Sub
End Sub
你点“工程”、“部件”,然后在“部件”对话框中选择
“Microsoft Common Dialog Control 6.0”,将其添加到工具箱中
然后就如楼上的所说,可以使用
Commondialog1.action=1
或者commondialog1.showopen
把上面的代码写入你的单击事件中即可
Private Sub Command1_Click()
Dim filepath As String
filepath = openfile(Me)
If filepath <> "" Then
Call ShellExecute(Me.hwnd, "Open", filepath, "", App.Path, 1)
End If
End Sub
模块代码:
'**************选择文件声明
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" ( _
pOpenfilename As OPENFILENAME) As Long
Public Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
'*************************************打开文件声明
Public Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) _
As Long
'文件选择函数
Function openfile(ByVal Fname As Form) As String
Dim strFile As String
Dim OFName As OPENFILENAME
OFName.lStructSize = Len(OFName)
OFName.hwndOwner = Fname.hwnd
OFName.hInstance = App.hInstance
OFName.lpstrFilter = "所有文件(*.*)" & Chr$(0) & "*.*"
OFName.lpstrFile = Space$(254)
OFName.nMaxFile = 255
OFName.lpstrFileTitle = Space$(254)
OFName.nMaxFileTitle = 255
OFName.lpstrTitle = "指定珊瑚虫外挂主程序CoralQQ.exe"
OFName.flags = 0
If GetOpenFileName(OFName) Then
openfile = Trim(OFName.lpstrFile)
End If
End Function
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOPENFILENAME As OPENFILENAME) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_PATHMUSTEXIST = &H800
Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_SHAREAWARE = &H4000
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Sub Command1_Click()
Dim sFilter, sFilename As String
sFilter = "可执行文件(*.exe;*.com;*.bat;*.cmd)" & Chr$(0) & _
"*.exe;*.com;*.bat;*.cmd" & Chr$(0) & _
"所有文件(*.*)" & Chr$(0) & _
"*.*" & Chr$(0)
sFilename = browseFile(Me.hwnd, "选择程序", sFilter)
If sFilename = "" Then Exit Sub
ShellExecute hwnd, "open", sFilename, vbNullString, vbNullString, 1
End Sub
Private Function browseFile(hwnd As Long, strTitle As String, strFilter) As String
On Error GoTo mnuFileOpenDialog_Click_Error
Dim file As OPENFILENAME, sFile As String, lResult As Long, iDelim As Integer
file.lStructSize = Len(file)
file.hwndOwner = hwnd
file.flags = OFN_HIDEREADONLY + OFN_PATHMUSTEXIST + OFN_FILEMUSTEXIST + OFN_SHAREAWARE
file.lpstrFile = "" & String$(250, 0)
file.nMaxFile = 255
file.lpstrFileTitle = String$(255, 0)
file.nMaxFileTitle = 255
'file.lpstrInitialDir = Environ$("WinDir")
file.lpstrFilter = strFilter
file.nFilterIndex = 1
file.lpstrTitle = strTitle
lResult = GetOpenFileName(file)
If lResult <> 0 Then
iDelim = InStr(file.lpstrFile, Chr$(0))
If iDelim > 0 Then
sFile = Left$(file.lpstrFile, iDelim - 1)
End If
End If
browseFile = sFile
mnuFileOpenDialog_Click_Exit:
Exit Function
mnuFileOpenDialog_Click_Error:
MsgBox "Error: " & Format$(Err) & " " & Error$, , "mnuFileOpenDialog_Click"
Resume mnuFileOpenDialog_Click_Exit
End Function
comaialog1.showopen