vb.net2005 用API打开文件夹对话框和设定默认路径。
来源:互联网 发布:php javascript 编辑:程序博客网 时间:2024/06/04 18:41
新建一个窗体,添加一个textbox 和一个button,拷贝下面的代码。
Imports System.Text
Imports System.Runtime.InteropServices
Public Class OpenFolder_OK
Private Delegate Function fbCallBack(ByVal hWnd As Integer, ByVal uMsg As Integer, ByVal lParam As Integer, ByVal lpData As Integer) As Integer
Private initpath As String = "C:/"
Private Structure BROWSEINFO
Dim hOwner As Integer
Dim pidlRoot As Integer
Dim pszDisplayName As String
Dim lpszTitle As String
Dim ulFlags As Integer
Dim lpfn As fbCallBack
Dim lParam As Integer
Dim iImage As Integer
End Structure
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (ByVal lpBrowseInfo As IntPtr) As Integer
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pIdl As Integer, ByVal pszPath As StringBuilder) As Integer
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const WM_USER As Integer = &H400
Private Const BFFM_INITIALIZED As Integer = 1
Private Const BFFM_SELCHANGED As Integer = 2
'Private Const BIF_BROWSEINCLUDEFILES As Integer = &H4000
Private Const BIF_DONTGOBELOWDOMAIN As Integer = &H2
Private Const BFFM_SETSELECTIONA As Integer = (WM_USER + 102)
Private Const BFFM_SETSTATUSTEXT As Integer = &H464
Private Const BIF_RETURNONLYFSDIRS As Integer = &H1
Dim pnt As IntPtr
Dim BIptr As IntPtr
Dim pIdl As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
pnt = Nothing
BIptr = Nothing
pIdl = Nothing
If Not My.Computer.FileSystem.DirectoryExists(initpath) Then
MsgBox(initpath & " not exist")
Exit Try
End If
Dim BI As BROWSEINFO
Dim sPath As StringBuilder
Dim txtPath As String
With BI
.hOwner = Me.Handle
.pszDisplayName = Space(260)
.lpszTitle = "Test"
.ulFlags = BIF_RETURNONLYFSDIRS
.lpfn = AddressOf BrowseCallBackProc
.lParam = Marshal.StringToHGlobalAnsi(initpath)
End With
txtPath = ""
BIptr = Marshal.AllocHGlobal(Marshal.SizeOf(BI))
Marshal.StructureToPtr(BI, BIptr, False)
pIdl = SHBrowseForFolder(BIptr)
If pIdl = 0 Then Exit Try
sPath = New StringBuilder(255)
SHGetPathFromIDList(pIdl, sPath)
txtPath = sPath.ToString
TextBox1.Text = txtPath
initpath = txtPath
Marshal.FreeHGlobal(pIdl)
Catch ex As Exception
MsgBox(ex.ToString)
Finally
Marshal.FreeHGlobal(BIptr)
Marshal.FreeHGlobal(pnt)
End Try
End Sub
Public Function BrowseCallBackProc(ByVal hWnd As Integer, ByVal uMsg As Integer, ByVal lParam As Integer, ByVal lpData As Integer) As Integer
Try
Select Case uMsg
Case BFFM_INITIALIZED
Call SendMessage(hWnd, BFFM_SETSELECTIONA, &H1, lpData)
Case BFFM_SELCHANGED
SendMessage(hWnd, BFFM_SETSTATUSTEXT, 0, lpData)
End Select
Catch Ex As Exception
Throw Ex
End Try
End Function
End Class
- vb.net2005 用API打开文件夹对话框和设定默认路径。
- vb打开文件夹对话框,并可事先指定默认路径,打开指定目录的对话框
- MFC打开文件夹对话框初始化打开路径
- 打开文件对话框和文件夹对话框之MFC和API实现。
- Excel VBA 打开对话框,获取文件夹路径
- VB.NET中用API实现打开文件夹
- [VC]“打开文件夹”对话框并获得文件夹的全路径
- c# 疑难(一)之打开“OpenFileDialog”文本对话框后默认路径改变问题和解决方法
- 打开文件对话框和打开文件夹对话框代码
- MFC中打开文件对话框和打开文件夹对对话框
- C++学习之:打开文件对话框和打开文件夹对话框
- JS打开文件和文件夹对话框
- MFC打开对话框和浏览文件夹操作
- EXCEL-VBA:调用打开文件夹对话框,选择路径
- MFC实现打开、保存文件对话框和浏览文件夹对话框
- VC++:打开、保存文件对话框和浏览文件夹对话框
- VC++打开、保存文件对话框和浏览文件夹对话框
- MFC实现打开、保存文件对话框和浏览文件夹对话框
- 后缀树及后缀数组
- 多级右键菜单 JS
- チョッコウ表测试实践
- 转: 字符串匹配
- 关于sizeof()的一些思考
- vb.net2005 用API打开文件夹对话框和设定默认路径。
- debian下应用raid5提高数据安全性--安恒网管员手记
- 从今天开始学习PHP!
- 进程间通讯
- 什么是FLASHLite
- Spring配置DHCP数据源
- NetBeans与SCIM的冲突解决[00原创]
- mdadm: failed to RUN_ARRAY
- 用VB实现托盘图标+气泡提示的效果