`

VB 在浏览目录时指定初始目录

    博客分类:
  • vb
VB 
阅读更多
'VB也可以使用CallBack,下面是一个例子:
'先把下面的代码放入BAS模块:
Option Explicit

'common to both methods
Public 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

Public Declare Function SHBrowseForFolder Lib _
"shell32.dll" Alias "SHBrowseForFolderA" _
(lpBrowseInfo
As BROWSEINFO) As Long

Public Declare Function
SHGetPathFromIDList Lib _
"shell32.dll" Alias "SHGetPathFromIDListA" _
(
ByVal pidl As Long, _
ByVal pszPath As String) As Long

Public Declare Sub
CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)

Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam
As Any) As Long

Public Declare Sub
MoveMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDest
As Any, _
pSource
As Any, _
ByVal dwLength As Long)

Public Const MAX_PATH = 260
Public Const WM_USER = &H400
Public Const BFFM_INITIALIZED = 1

'Constants ending in 'A' are for Win95 ANSI
'calls; those ending in 'W' are the wide Unicode
'calls for NT.

'Sets the status text to the null-terminated
'string specified by the lParam parameter.
'wParam is ignored and should be set to 0.
Public Const BFFM_SETSTATUSTEXTA As Long = (WM_USER + 100)
Public Const BFFM_SETSTATUSTEXTW As Long = (WM_USER + 104)

'If the lParam parameter is non-zero, enables the
'OK button, or disables it if lParam is zero.
'(docs erroneously said wParam!)
'wParam is ignored and should be set to 0.
Public Const BFFM_ENABLEOK As Long = (WM_USER + 101)

'Selects the specified folder. If the wParam
'parameter is FALSE, the lParam parameter is the
'PIDL of the folder to select , or it is the path
'of the folder if wParam is the C value TRUE (or 1).
'Note that after this message is sent, the browse
'dialog receives a subsequent BFFM_SelectIONCHANGED
'message.
Public Const BFFM_SETSelectIONA As Long = (WM_USER + 102)
Public Const BFFM_SETSelectIONW As Long = (WM_USER + 103)


'specific to the PIDL method
'Undocumented call for the example. IShellFolder's
'ParseDisplayName member function should be used instead.
Public Declare Function SHSimpleIDListFromPath Lib _
"shell32" Alias "#162" _
(
ByVal szPath As String) As Long


'specific to the STRING method
Public Declare Function LocalAlloc Lib "kernel32" _
(
ByVal uFlags As Long, _
ByVal uBytes As Long) As Long

Public Declare Function
LocalFree Lib "kernel32" _
(
ByVal hMem As Long) As Long

Public Declare Function
lstrcpyA Lib "kernel32" _
(lpString1
As Any, lpString2 As Any) As Long

Public Declare Function
lstrlenA Lib "kernel32" _
(lpString
As Any) As Long

Public Const
LMEM_FIXED = &H0
Public Const LMEM_ZEROINIT = &H40
Public Const LPTR = (LMEM_FIXED Or LMEM_ZEROINIT)


Public Function BrowseCallbackProcStr(ByVal hWnd As Long, _
ByVal uMsg As Long, _
ByVal lParam As Long, _
ByVal lpData As Long) As Long

'Callback for the Browse STRING method.

'On initialization, set the dialog's
'pre-selected folder from the pointer
'to the path allocated as bi.lParam,
'passed back to the callback as lpData param.

Select Case uMsg
Case BFFM_INITIALIZED

Call SendMessage(hWnd, BFFM_SETSelectIONA, _
True, ByVal StrFromPtrA(lpData))

Case Else:

End Select

End Function


Public Function
BrowseCallbackProc(ByVal hWnd As Long, _
ByVal uMsg As Long, _
ByVal lParam As Long, _
ByVal lpData As Long) As Long

'Callback for the Browse PIDL method.

'On initialization, set the dialog's
'pre-selected folder using the pidl
'set as the bi.lParam, and passed back
'to the callback as lpData param.

Select Case uMsg
Case BFFM_INITIALIZED

Call SendMessage(hWnd, BFFM_SETSelectIONA, _
False, ByVal lpData)

Case Else:

End Select

End Function


Public Function
FARPROC(pfn As Long) As Long

'A dummy procedure that receives and returns
'the value of the AddressOf operator.

'Obtain and set the address of the callback
'This workaround is needed as you can't assign
'AddressOf directly to a member of a user-
'defined type, but you can assign it to another
'long and use that (as returned here)

FARPROC = pfn

End Function


Public Function
StrFromPtrA(lpszA As Long) As String

'Returns an ANSI string from a pointer to an ANSI string.

Dim sRtn As String
sRtn = String$(lstrlenA(ByVal lpszA), 0)
Call lstrcpyA(ByVal sRtn, ByVal lpszA)
StrFromPtrA = sRtn

End Function

'--end block--'

 

'-----------------------------------------------------------------------------------------
'将下面代码加入窗体。窗体上还应放置三个按钮和两个TextBox。
'-----------------------------------------------------------------------------------------
Option Explicit

Private Sub cmdString_Click()
Text2 =
""
Text2 = BrowseForFolderByPath((Text1))
End Sub


Private Sub
cmdPIDL_Click()
Text2 =
""
Text2 = BrowseForFolderByPIDL((Text1))
End Sub


Private Sub
cmdEnd_Click()
Unload
Me
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 = "Pre-selecting the folder using the folder's string."
.lpfn = FARPROC(AddressOf BrowseCallbackProcStr)

lpSelPath = LocalAlloc(LPTR, Len(sSelPath))
MoveMemory
ByVal lpSelPath, ByVal sSelPath, Len(sSelPath)
.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 = "Pre-selecting a folder using the folder's pidl."
.lpfn = FARPROC(AddressOf BrowseCallbackProc)
.lParam = SHSimpleIDListFromPath(sSelPath)
End With

pidl = SHBrowseForFolder(BI)

If pidl Then
If
SHGetPathFromIDList(pidl, sPath) Then
BrowseForFolderByPIDL = Left$(sPath, InStr(sPath, vbNullChar) - 1)
End If
Call
CoTaskMemFree(pidl)
End If

Call
CoTaskMemFree(BI.lParam)
End Function

 

分享到:
评论

相关推荐

    VB的TreeView展示目录树

    总之,VB的`TreeView`控件结合适当的编程逻辑,可以实现动态展示指定路径下文件系统的目录树结构,这在许多需要直观展现文件组织的应用场景中都非常实用。通过理解`TreeNode`的使用、目录遍历以及事件处理,开发者...

    VB打开浏览文件夹对话框

    "VB打开浏览文件夹对话框"就是一个典型的用户交互功能,它允许用户通过图形界面选择指定的文件夹,从而实现文件或数据的读取、保存等操作。在VB中,我们通常使用`FolderBrowserDialog`组件来实现这一功能。 `...

    ftp的vb实现

    FTP(File Transfer Protocol)是一种用于在互联网上传输文件的标准协议,而VB(Visual Basic)是Microsoft开发的一种面向对象的编程语言。"FTP的VB实现"指的是使用VB来编写程序,实现对FTP服务器的连接、登录、文件...

    VB6 webbrowser加载Word 附送VB基础入门教程

    WebBrowser控件是VB6中的一个强大组件,它允许开发者在应用程序中嵌入一个Internet Explorer实例,从而实现浏览网页的功能。这个"VB6 Webbrowser加载Word"的主题涉及到如何利用WebBrowser控件来打开和显示Microsoft ...

    VB DirBrowse获取路径值

    在VB(Visual Basic)编程中,`DirBrowse`是一个非标准的控件或函数,它不是VB的标准库中的一部分,但常常被开发者用于实现文件和目录的浏览功能。`DirBrowse`通常指的是一个自定义控件或者用户界面元素,允许用户在...

    VB 实现 图片浏览器

    【VB 实现 图片浏览器】是一个使用Visual Basic(VB)编程语言开发的应用程序,它能够帮助用户方便地浏览指定文件夹中的所有图片。这个应用程序的核心功能是提供一个友好的图形用户界面(GUI),使得用户无需打开多...

    在VB6.0打开任意的Access数据库

    - `GetOpenFileName`函数的参数是一个`OPENFILENAME`结构体,其中包含了许多与文件对话框相关的属性设置,例如过滤器、初始目录等。 - 示例代码中定义了一个名为`ofn`的`OPENFILENAME`结构体变量,并对其进行了...

    显示文件目录选择显示文件目录选择显示文件目录选择

    在 VB 中,用户可以通过调用系统提供的 API 函数来实现文件目录的选择功能。这一功能在开发文件管理器、应用程序配置等场景中非常实用。 #### 知识点二:`SHBrowseForFolder` 函数详解 - **函数定义**: ```vb ...

    VB 打开对话框的实例

    3. **FolderBrowserDialog**:允许用户浏览并选择文件夹,适用于需要指定文件保存位置或操作特定目录的应用。 4. **ColorDialog** 和 **FontDialog**:分别用于选择颜色和字体,常用于图形界面设计或文本编辑应用。...

    VB 弹出文件夹对话框

    这个功能在许多应用程序中非常常见,例如保存或打开文件时,或者需要指定某个目录进行操作时。下面将详细解释如何在VB中实现这一功能,并探讨相关知识点。 首先,VB提供了`FolderBrowserDialog`控件,这是一个...

    VB编写日志记录,简单的

    在VB(Visual Basic)编程中,日志记录是一项重要的任务,它可以帮助开发者追踪程序运行时的状态,便于调试和问题排查。本主题将详细介绍如何使用VB来编写简单的日志记录功能,特别是结合ListView控件实现这一功能。...

    简单浏览器VB.NET做

    4. **控件的使用**:在VB.NET中,需要将WebBrowser控件拖放到窗体上,然后可以通过属性窗口设置其属性,如`Url`属性可以指定初始加载的网页地址。 5. **事件处理**:通过编写代码响应控件的事件,例如,可以监听`...

    vb.net OpenFileDialog与saveFileDialog

    10. InitialDirectory 属性:文件对话框显示的初始目录。 二、SaveFileDialog 组件 SaveFileDialog 组件也是 .NET 预设的有模式对话框之一,显示的是系统的“另存为”对话框。我们可以通过它来快速开发一个能让...

    VB 调用TTS语音控件实例.rar

    在VB(Visual Basic)编程中,开发者经常需要与各种外部组件和控件进行交互以增强应用程序的功能。在这个实例中,我们关注的是“VB调用TTS语音控件”,这是一个能够实现文本转语音(Text To Speech,简称TTS)功能的...

    vb6.0 编写 浏览器代码

    在VB6.0中编写浏览器代码是一项常见的编程任务,它涉及到使用Visual Basic 6.0的内置功能来创建一个能够浏览网页的应用程序。VB6.0是一个古老的但仍然被一些开发者使用的集成开发环境(IDE),它支持事件驱动的编程...

    如何用VB在网页中获取图片超链

    在VB(Visual Basic)编程环境中,我们可以利用内置的WEBBROWSER1控件来与网页进行交互,从而实现从网页中获取图片的超链接。这个过程涉及到网页元素的定位、属性的读取等多个知识点,下面将详细讲解。 首先,我们...

    OPCUA客户端vb.net编写

    在VB.NET环境中开发OPC UA客户端,可以实现与OPC服务器的交互,读取或写入不同类型的设备数据。VS2012(Visual Studio 2012)是微软提供的一个强大的集成开发环境,支持多种编程语言,包括VB.NET。 在VB.NET中创建...

    VB6.0结合web控件编写网络浏览器

    在"工程1.vbp"文件中,保存了整个项目的元数据,包括工程的名称、引用的库、窗体列表以及程序启动时的初始窗体等信息。对于这个简单的浏览器项目,VBP文件确保了Form1在运行时作为主窗口出现。 编写网络浏览器的...

    Complex VB OPC Automation Example

    1. **初始化OPC客户端**:在VB代码中,首先实例化OPC客户端对象,设置服务器地址和连接参数。 2. **连接OPC服务器**:调用连接方法连接到指定的OPC服务器,如Siemens的PLC(可编程逻辑控制器)或其他自动化设备。 3....

    vb.net代码 如何实现简单的图片浏览器

    在VB.NET中,创建一个简单的图片浏览器涉及到多个关键知识点,包括控件的使用、事件处理、文件操作以及图像处理。以下是对这些知识点的详细说明: 1. **控件使用**: - `PictureBox`: 用于显示图片的主要控件,...

Global site tag (gtag.js) - Google Analytics