📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
BorderStyle = 1 'Fixed Single
Caption = "文件夹浏览"
ClientHeight = 3195
ClientLeft = 45
ClientTop = 330
ClientWidth = 4590
Icon = "Form1.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 3195
ScaleWidth = 4590
StartUpPosition = 2 '屏幕中心
Begin VB.TextBox Text2
Height = 375
Left = 240
TabIndex = 4
Text = "Text2"
Top = 2160
Width = 4095
End
Begin VB.TextBox Text1
Height = 375
Left = 240
TabIndex = 3
Text = "Text1"
Top = 600
Width = 4095
End
Begin VB.CommandButton Command3
Caption = "Command3"
Height = 375
Left = 2280
TabIndex = 2
Top = 2640
Width = 2055
End
Begin VB.CommandButton Command2
Caption = "Command2"
Height = 375
Left = 2280
TabIndex = 1
Top = 1200
Width = 2055
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Left = 240
TabIndex = 0
Top = 1200
Width = 2055
End
Begin VB.Label Label2
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "你选择的文件夹"
Height = 180
Left = 240
TabIndex = 6
Top = 1800
Width = 1260
End
Begin VB.Label Label1
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "浏览文件夹:"
Height = 180
Left = 240
TabIndex = 5
Top = 240
Width = 1080
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Command1_Click()
Dim spath As String
Text2.Text = ""
spath = UnqualifyPath((Text1.Text))
'调用函数,返回选择的路径,如果没有选择,则返回空字符串
Text2.Text = BrowseForFolderByPath(spath)
End Sub
Private Sub Command2_Click()
Dim spath As String
Text2.Text = ""
spath = UnqualifyPath((Text1.Text))
'调用函数,返回选择的路径,如果没有选择,则返回空字符串
Text2.Text = BrowseForFolderByPIDL(spath)
End Sub
Private Sub Command3_Click()
Unload Me
End Sub
Private Sub Form_Load()
Command1.Caption = "使用文件夹名浏览"
Command2.Caption = "使用文件夹的PIDL浏览"
Command3.Caption = "结束"
End Sub
Public Function BrowseForFolderByPath(sSelPath As String) As String
Dim BI As BROWSEINFO
Dim pidl As Long
Dim lpSelPath As Long
Dim spath As String * MAX_PATH
With BI
.hOwner = Me.hWnd
.pidlRoot = 0
.lpszTitle = "使用文件夹的pidl 来预先选择文件夹"
.lpfn = FARPROC(AddressOf BrowseCallbackProcStr)
lpSelPath = LocalAlloc(LPTR, Len(sSelPath) + 1)
CopyMemory ByVal lpSelPath, ByVal sSelPath, Len(sSelPath) + 1
.lParam = lpSelPath
End With
pidl = SHBrowseForFolder(BI)
If pidl Then
If SHGetPathFromIDList(pidl, spath) Then
BrowseForFolderByPath = Left$(spath, InStr(spath, vbNullChar) - 1)
End If
Call CoTaskMemFree(pidl)
End If
Call LocalFree(lpSelPath)
End Function
Public Function BrowseForFolderByPIDL(sSelPath As String) As String
Dim BI As BROWSEINFO
Dim pidl As Long
Dim spath As String * MAX_PATH
With BI
.hOwner = Me.hWnd
.pidlRoot = 0
.lpszTitle = "使用文件夹的pidl 来预先选择文件夹."
.lpfn = FARPROC(AddressOf BrowseCallbackProc)
.lParam = GetPIDLFromPath(sSelPath)
End With
pidl = SHBrowseForFolder(BI)
If pidl Then
If SHGetPathFromIDList(pidl, spath) Then
BrowseForFolderByPIDL = Left$(spath, InStr(spath, vbNullChar) - 1)
End If
'释放SHBrowseForFolder返回的pidl
Call CoTaskMemFree(pidl)
End If
'释放GetPIDLFromPath调用中设置的pidl
Call CoTaskMemFree(BI.lParam)
End Function
Public Function GetPIDLFromPath(spath As String) As Long
If IsWinNT Then
GetPIDLFromPath = SHSimpleIDListFromPath(StrConv(spath, vbUnicode))
Else
GetPIDLFromPath = SHSimpleIDListFromPath(spath)
End If
End Function
Public Function IsWinNT() As Boolean
#If Win32 Then
Dim OSV As OSVERSIONINFO
OSV.OSVSize = Len(OSV)
'如果函数调用成功,则返回1
If GetVersionEx(OSV) = 1 Then
'PlatformId给出了OS的平台信息,因此,如果它是VER_PLATFORM_WIN32_NT,则返回True。
IsWinNT = OSV.PlatformID = VER_PLATFORM_WIN32_NT
End If
#End If
End Function
Public Function UnqualifyPath(spath As String) As String
If Len(spath) > 0 Then
If Right$(spath, 1) = "\" Then
UnqualifyPath = Left$(spath, Len(spath) - 1)
Exit Function
End If
End If
UnqualifyPath = spath
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -