如何打开一个路径,而不是打开一个文件?(vb)
如何打开一个路径,而不是打开一个文件?(vb)
我用common dialog ,方法,然后再进行处理,不好,有没有更好的方法。
问题点数:100、回复次数:7Top
1 楼gpo2002(永吹不休)回复于 2003-08-02 10:55:36 得分 30
有!
Option Explicit
Private Type BROWSEINFOTYPE
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
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBROWSEINFOTYPE As BROWSEINFOTYPE) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
Private Declare Function LocalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal uBytes As Long) As Long
Private Declare Function LocalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Const WM_USER = &H400
Private Const BFFM_SETSELECTIONA As Long = (WM_USER + 102)
Private Const BFFM_SETSELECTIONW As Long = (WM_USER + 103)
Private Const LPTR = (&H0 Or &H40)
Private Function BrowseCallbackProcStr(ByVal hWnd As Long, ByVal uMsg As Long, ByVal lParam As Long, ByVal lpData As Long) As Long
If uMsg = 1 Then
Call SendMessage(hWnd, BFFM_SETSELECTIONA, True, ByVal lpData)
End If
End Function
Private Function FunctionPointer(FunctionAddress As Long) As Long
FunctionPointer = FunctionAddress
End Function
Public Function BrowseForFolder(ByVal hWnd As Long, ByVal Title As String, Optional selectedPath As String = " ") As String
Dim Browse_for_folder As BROWSEINFOTYPE
Dim itemID As Long
Dim selectedPathPointer As Long
Dim tmpPath As String * 256
With Browse_for_folder
.hOwner = hWnd ' Window Handle
.lpszTitle = Title
.lpfn = FunctionPointer(AddressOf BrowseCallbackProcStr) ' Dialog callback function that preselectes the folder specified
selectedPathPointer = LocalAlloc(LPTR, Len(selectedPath) + 1) ' Allocate a string
CopyMemory ByVal selectedPathPointer, ByVal selectedPath, Len(selectedPath) + 1 ' Copy the path to the string
.lParam = selectedPathPointer ' The folder to preselect
End With
itemID = SHBrowseForFolder(Browse_for_folder) ' Execute the BrowseForFolder API
If itemID Then
If SHGetPathFromIDList(itemID, tmpPath) Then ' Get the path for the selected folder in the dialog
BrowseForFolder = Left$(tmpPath, InStr(tmpPath, vbNullChar) - 1) ' Take only the path without the nulls
End If
Call CoTaskMemFree(itemID) ' Free the itemID
End If
Call LocalFree(selectedPathPointer) ' Free the string from the memory
End Function
---------------------
Label1 = BrowseForFolder(Me.hWnd, "select a path ")
Top
2 楼3661512(.Net)回复于 2003-08-02 10:59:49 得分 10
窗体申明部分
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
点击CommandButton事件中
Private Sub Command1_Click()
ShellExecute hwnd, "open", "c:\winnt", "", "", 5 '打开c:\winnt目录
End SubTop
3 楼pcwak()回复于 2003-08-02 12:46:43 得分 10
就用楼上所说的ShellExecuteTop
4 楼since1990(level)回复于 2003-08-02 13:07:50 得分 0
快
帮楼主upTop
5 楼neo40(企鹅)回复于 2003-08-02 13:23:37 得分 0
^-^,学习中Top
6 楼junwhj(http://www.grid2000.com/cn)回复于 2003-08-02 15:37:41 得分 30
Private Declare Function SHBrowseForFolder _
Lib "shell32.dll" Alias "SHBrowseForFolderA" ( _
lpBrowseInfo 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
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
Private Sub Command1_Click()
Dim BI As BROWSEINFO
Dim lngIDList As Long
Dim strPath As String * 255
BI.lpszTitle = "请选择一个文件夹"
lngIDList = SHBrowseForFolder(BI)
If lngIDList = 0 Then
Exit Sub
End If
Call SHGetPathFromIDList(ByVal lngIDList, ByVal strPath)
MsgBox Left(strPath, InStr(strPath, Chr(0)) - 1)
End Sub
Top
7 楼jjkk168(老加班的人--好好学习,天天吃饭)回复于 2003-08-02 15:44:12 得分 20
如果是想在内部打开,那用filesystemobject进行处理吧
如果是想在外部打开,用shellexecute打开IE浏览器Top




