Private Sub Command1_Click()
CommonDialog1.InitDir = "c:\"
CommonDialog1.FileName = ""
CommonDialog1.ShowOpen
Me.Caption = CommonDialog1.FileName
End Sub
編譯后運行,結果,不行
然后改代碼
Private Sub Command1_Click()
CommonDialog1.InitDir = app.path & "\"
CommonDialog1.FileName = ""
CommonDialog1.ShowOpen
Me.Caption = CommonDialog1.FileName
End Sub
Private Sub Command1_Click()
CommonDialog1.InitDir = "c:\"
CommonDialog1.FileName = ""
CommonDialog1.ShowOpen
Me.Caption = CommonDialog1.FileName
End Sub
編譯后運行,結果,不行
然后改代碼
Private Sub Command1_Click()
CommonDialog1.InitDir = app.path & "\"
CommonDialog1.FileName = ""
CommonDialog1.ShowOpen
Me.Caption = CommonDialog1.FileName
End Sub
' -= 檔案對話框 =-
Private Declare Function GetOpenFileName Lib "Comdlg32" Alias "GetOpenFileNameW" ( _
ByRef pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "Comdlg32" Alias "GetSaveFileNameW" ( _
ByRef pOpenfilename As OPENFILENAME) As Long
' * * * 打開/保存 檔案API用資料型別 * * *
Private Type OPENFILENAME
lStructSize As Long
hWndOwner As Long
hInstance As Long
lpstrFilter As Long
lpstrCustFilter As Long
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As Long
nMaxFile As Long
lpstrFileTitle As Long
nMaxFileTitle As Long
lpstrInitialDir As Long
lpstrTitle As Long
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As Long
End Type
Private Function ShowOpen(ByVal Title As String, ByVal Filter As String, _
ByRef FileOpen As String) As Long
Dim OpenFN As OPENFILENAME
Dim strFilt As String
Dim strPath As String
Dim strName As String
Dim strTemp As String
strName = String$(264&, 0)
strTemp = strName
strPath = App.Path & "\" & vbNullChar
strFilt = Replace(Replace(Filter & vbNullChar, "|", vbNullChar), ",", vbNullChar)
OpenFN.lStructSize = 76& ' LenB(OpenFN)
OpenFN.hWndOwner = Me.hWnd
OpenFN.hInstance = App.hInstance
OpenFN.lpstrTitle = StrPtr(Title)
OpenFN.lpstrFilter = StrPtr(strFilt)
OpenFN.nFilterIndex = 0&
OpenFN.lpstrDefExt = 5& + StrPtr(strFilt) + InStrB(1&, Filter, "|") ' 第一種型別的擴展名
OpenFN.lpstrInitialDir = StrPtr(strPath)
OpenFN.lpstrFile = StrPtr(strName)
OpenFN.nMaxFile = 256&
OpenFN.lpstrFileTitle = StrPtr(strTemp)
OpenFN.nMaxFileTitle = 256&
OpenFN.Flags = &H180C&
If (GetOpenFileName(OpenFN)) Then
Dim p As Long
p = InStr(1&, strName, vbNullChar)
FileOpen = Left$(strName, p - 1&)
ShowOpen = vbFalse
Else
ShowOpen = vbTrue
End If
End Function
Private Sub Command1_Click()
Dim strFile As String
If (ShowOpen("打開檔案", "所有檔案(*.*)|*.*,", strFile)) Then Exit Sub
Call MsgBox(strFile, 64&)
End Sub
uj5u.com熱心網友回復:
想貼個模塊,居然超過發帖限制
uj5u.com熱心網友回復:
分兩截貼吧
Option Explicit
Private Declare Sub CopyMem Lib "KERNEL32" Alias "RtlMoveMemory" (lpTo As Any, lpFrom As Any, ByVal lLen As Long)
Private Declare Sub CopyMemByPtr Lib "KERNEL32" Alias "RtlMoveMemory" (ByVal lpTo As Long, ByVal lpFrom As Long, ByVal lLen As Long)
Private Declare Function lstrcpy Lib "KERNEL32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Any) As Long
Private Declare Function lstrlen Lib "KERNEL32" Alias "lstrlenA" (ByVal lpString As Any) As Long
'**********************************************************
''定義一個全域變數,用于保存字體的各種屬性
Public Type SmFontAttr
FontName As String '字體名
FontSize As Integer '字體大小
FontBod As Boolean '是否黑體
FontItalic As Boolean '是否斜體
FontUnderLine As Boolean '是否下劃線
FontStrikeou As Boolean
FontColor As Long
WinHwnd As Long
End Type
Dim M_GetFont As SmFontAttr
'/------------------------------------------
Private Type CHOOSECOLOR
lStructSize As Long
hwndOwner As Long
hInstance As Long
rgbResult As Long
lpCustColors As String
flags As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
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 Type LOGFONT
lfHeight As Long
lfWidth As Long
lfEscapement As Long
lfOrientation As Long
lfWeight As Long
lfItalic As Byte
lfUnderline As Byte
lfStrikeOut As Byte
lfCharSet As Byte
lfOutPrecision As Byte
lfClipPrecision As Byte
lfQuality As Byte
lfPitchAndFamily As Byte
lfFaceName As String * LF_FACESIZE
End Type
Private Type CHOOSEFONT
lStructSize As Long
hwndOwner As Long
hdc As Long
lpLogFont As Long
iPointSize As Long
flags As Long
rgbColors As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As String
hInstance As Long
lpszStyle As String
nFontType As Integer
MISSING_ALIGNMENT As Integer
nSizeMin As Long
nSizeMax As Long
End Type
'/--------------
Private Type SHITEMID
cb As Long
abID() As Byte
End Type
Private Type ITEMIDLIST
mkid As SHITEMID
End Type
'/------------------------------------------
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
(ByVal Pidl As Long, ByVal pszPath As String) As Long
Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _
(ByVal hwndOwner As Long, ByVal nFolder As Long, _
Pidl As ITEMIDLIST) As Long
'/------------------------------------------
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function CHOOSECOLOR Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long
Private Declare Function WNetConnectionDialog Lib "mpr.dll" (ByVal hWnd As Long, ByVal dwType As Long) As Long
Private Declare Function CHOOSEFONT Lib "comdlg32.dll" Alias "ChooseFontA" (pChooseFont As CHOOSEFONT) As Long
'/=======顯示斷開網路資源對話框============
Private Declare Function WNetDisconnectDialog Lib "mpr.dll" _
(ByVal hWnd As Long, ByVal dwType As Long) As Long
'/================================================================================
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" _
(lpBrowseInfo As BROWSEINFO) As Long
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
'/結構說明: _
hOwner 呼叫這個對話框的視窗的句柄 _
pidlRoot 指向你希望瀏覽的最上面的檔案夾的符串列 _
pszDisplayName 用于保存用戶所選擇的檔案夾的顯示名的緩沖區 _
lpszTitle 瀏覽對話框的標題 _
ulFlags 決定瀏覽什么的標志(見下) _
lpfn 當事件發生時對話框呼叫的回呼函式的地址.可將它設定為NULL _
lparam 若定義了回呼函式,則為傳遞給回呼函式的值 _
iImage As Long 保存所選檔案夾映像索引的緩沖區 _
ulFlags引數(見下:)
Private Const BIF_RETURNONLYFSDIRS = &H1 '僅允許瀏覽檔案系統檔案夾
Private Const BIF_DONTGOBELOWDOMAIN = &H2 '利用這個值強制用戶儀在網上鄰居的域級別中
Private Const BIF_STATUSTEXT = &H4 '在選擇對話中顯示狀態欄
Private Const BIF_RETURNFSANCESTORS = &H8 '回傳檔案系統祖先
Private Const BIF_BROWSEFORCOMPUTER = &H1000 '允許瀏覽計算機
Private Const BIF_BROWSEFORPRINTER = &H2000 '允許游覽列印機檔案夾
'/--------------------------------------------------------------------------------
Private Function GetFolderValue(wIdx As Integer) As Long
If wIdx < 2 Then
GetFolderValue = 0
ElseIf wIdx < 12 Then
GetFolderValue = wIdx
Else
GetFolderValue = wIdx + 4
End If
End Function
uj5u.com熱心網友回復:
第二截,其中有一個函式被我整體注釋掉了,應該是當時有點啥問題,因為沒用到于是懶得查問題直接注釋掉。
'
'檔案夾選擇對話框
'函式:SaveFile
'引數:Title 設定對話框的標簽.
' hWnd 呼叫此函式的HWND
' FolderID SmBrowFolder列舉(默認:我的電腦).
'回傳值:String 檔案夾路徑.
'例子:
'Public Function GetFolder(Optional Title As String, _
' Optional hWnd As Long, _
' Optional FolderID As SmBrowFolder = MyComputer) As String
' Dim Bi As BROWSEINFO
' Dim Pidl As Long
' Dim Folder As String
' Dim IDL As ITEMIDLIST
' Dim nFolder As Long
' Dim ReturnFol As String
' Dim Fid As Integer
'
' Fid = FolderID
' Folder = String$(255, Chr$(0))
' With Bi
' .hOwner = hWnd
' nFolder = GetFolderValue(Fid)
' If SHGetSpecialFolderLocation(ByVal hWnd, ByVal nFolder, IDL) = NoError Then
' .pidlRoot = IDL.mkid.cb
' End If
' .pszDisplayName = String$(MAX_PATH, Fid)
'
' If Len(Title) > 0 Then
' .lpszTitle = Title & Chr$(0)
' Else
' .lpszTitle = "請選擇檔案夾:" & Chr$(0)
' End If
'
' .ulFlags = GetReturnType()
' End With
'
' Pidl = SHBrowseForFolder(Bi)
' '/回傳所選的檔案夾路徑
' If SHGetPathFromIDList(ByVal Pidl, ByVal Folder) Then
' ReturnFol = Left$(Folder, InStr(Folder, Chr$(0)) - 1)
' If Right$(Trim$(ReturnFol), 1) <> "/" Then ReturnFol = ReturnFol & "/"
' GetFolder = ReturnFol
' Else
' GetFolder = ""
' End If
'End Function
'
'檔案保存對話框
'函式:SaveFile
'引數:WinHwnd 呼叫此函式的HWND
' BoxLabel 設定對話框的標簽.
' StartPath 設定初始化路徑.
' FilterStr 檔案過濾.
' Flag 標志.(參考MSDN)
'回傳值:String 檔案名.
'例子:
Public Function SaveFile(WinHwnd As Long, _
Optional BoxLabel As String = "", _
Optional StartPath As String = "", _
Optional FilterStr = "*.*|*.*", _
Optional Flag As Variant = &H4 Or &H200000) As String
Dim rc As Long
Dim pOpenfilename As OPENFILENAME
Dim Fstr1() As String
Dim Fstr As String
Dim i As Long
Const MAX_Buffer_LENGTH = 256
On Error Resume Next
If Len(Trim$(StartPath)) > 0 Then
If Right$(StartPath, 1) <> "/" Then StartPath = StartPath & "/"
If Dir$(StartPath, vbDirectory + vbHidden) = "" Then
StartPath = App.Path
End If
Else
StartPath = App.Path
End If
If Len(Trim$(FilterStr)) = 0 Then
Fstr = "*.*|*.*"
End If
Fstr1 = Split(FilterStr, "|")
For i = 0 To UBound(Fstr1)
Fstr = Fstr & Fstr1(i) & vbNullChar
Next
'/--------------------------------------------------
With pOpenfilename
.hwndOwner = WinHwnd
.hInstance = App.hInstance
.lpstrTitle = BoxLabel
.lpstrInitialDir = StartPath
.lpstrFilter = Fstr
.nFilterIndex = 1
.lpstrDefExt = vbNullChar & vbNullChar
.lpstrFile = String(MAX_Buffer_LENGTH, 0)
.nMaxFile = MAX_Buffer_LENGTH - 1
.lpstrFileTitle = .lpstrFile
.nMaxFileTitle = MAX_Buffer_LENGTH
.lStructSize = Len(pOpenfilename)
.flags = Flag
End With
rc = GetSaveFileName(pOpenfilename)
If rc Then
SaveFile = Left$(pOpenfilename.lpstrFile, pOpenfilename.nMaxFile)
Else
SaveFile = ""
End If
End Function
'
'檔案打開對話框
'函式:OpenFile
'引數:WinHwnd 呼叫此函式的HWND
' BoxLabel 設定對話框的標簽.
' StartPath 設定初始化路徑.
' FilterStr 檔案過濾.
' Flag 標志.(參考MSDN)
'回傳值:String 檔案名.
'例子:
Public Function MyOpenFile(WinHwnd As Long, _
Optional BoxLabel As String = "", _
Optional StartPath As String = "", _
Optional FilterStr = "*.*|*.*", _
Optional Flag As Variant = &H8 Or &H200000) As String
Dim rc As Long
Dim pOpenfilename As OPENFILENAME
Dim Fstr1() As String
Dim Fstr As String
Dim i As Long
Const MAX_Buffer_LENGTH = 256
On Error Resume Next
If Len(Trim$(StartPath)) > 0 Then
If Right$(StartPath, 1) <> "/" Then StartPath = StartPath & "/"
If Dir$(StartPath, vbDirectory + vbHidden) = "" Then
StartPath = App.Path
End If
Else
StartPath = App.Path
End If
If Len(Trim$(FilterStr)) = 0 Then
Fstr = "*.*|*.*"
End If
Fstr = ""
Fstr1 = Split(FilterStr, "|")
For i = 0 To UBound(Fstr1)
Fstr = Fstr & Fstr1(i) & vbNullChar
Next
With pOpenfilename
.hwndOwner = WinHwnd
.hInstance = App.hInstance
.lpstrTitle = BoxLabel
.lpstrInitialDir = StartPath
.lpstrFilter = Fstr
.nFilterIndex = 1
.lpstrDefExt = vbNullChar & vbNullChar
.lpstrFile = String(MAX_Buffer_LENGTH, 0)
.nMaxFile = MAX_Buffer_LENGTH - 1
.lpstrFileTitle = .lpstrFile
.nMaxFileTitle = MAX_Buffer_LENGTH
.lStructSize = Len(pOpenfilename)
.flags = Flag
End With
rc = GetOpenFileName(pOpenfilename)
If rc Then
MyOpenFile = Left$(pOpenfilename.lpstrFile, pOpenfilename.nMaxFile)
Else
MyOpenFile = ""
End If
End Function
'
'顏色對話框
'函式:GetColor
'引數:
'回傳值:Long,用戶所選擇的顏色.
'例子:
Public Function GetColor() As Long
Dim rc As Long
Dim pChoosecolor As CHOOSECOLOR
Dim CustomColor() As Byte
With pChoosecolor
.hwndOwner = 0
.hInstance = App.hInstance
.lpCustColors = StrConv(CustomColor, vbUnicode)
.flags = 0
.lStructSize = Len(pChoosecolor)
End With
rc = CHOOSECOLOR(pChoosecolor)
If rc Then
GetColor = pChoosecolor.rgbResult
Else
GetColor = -1
End If
End Function
uj5u.com熱心網友回復:
第二截,其中有一個函式被我整體注釋掉了,應該是當時有點啥問題,因為沒用到于是懶得查問題直接注釋掉。
'
'檔案夾選擇對話框
'函式:SaveFile
'引數:Title 設定對話框的標簽.
' hWnd 呼叫此函式的HWND
' FolderID SmBrowFolder列舉(默認:我的電腦).
'回傳值:String 檔案夾路徑.
'例子:
'Public Function GetFolder(Optional Title As String, _
' Optional hWnd As Long, _
' Optional FolderID As SmBrowFolder = MyComputer) As String
' Dim Bi As BROWSEINFO
' . . . . . . . . . . . .
'
'
'End Function
'
' . . . . . . . . . . . . .