文件选择对话框的实现

Imports System.Runtime.InteropServices
Imports System.Text
'''
Public Class OpenDirectoryDialog
    '''
    Private _DialogTitle As String = ""
    Private _DirectoryName As String = ""
    Private Const MAX_PATH As Integer = 260
    '''
    Private Structure BROWSEINFO
        Public hWndOwner As IntPtr
        Public pIDLRoot As Integer
        Public pszDisplayName As String
        Public lpszTitle As String
        Public ulFlags As Integer
        Public lpfnCallback As Integer
        Public lParam As Integer
        Public iImage As Integer
    End Structure
    '''
    Private Declare Function CoTaskMemFree Lib "ole32.dll" (ByVal hMem As IntPtr) As Integer
    Private Declare Function lstrcat Lib "kernel32.dll" (ByVal lpString1 As String, ByVal lpString2 As String) As IntPtr
    Private Declare Function SHBrowseForFolder Lib "shell32.dll" (ByRef lpbi As BROWSEINFO) As IntPtr
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" (ByVal pidList As IntPtr, ByVal lpBuffer As StringBuilder) As Integer
    '''
    Public Function ShowDialog(ByVal owner As IWin32Window) As DialogResult
        Dim _handle As IntPtr
        Dim udtBI As New BROWSEINFO()
        Dim lpIDList As IntPtr
        Dim hTitle As GCHandle
        Dim buffer As New StringBuilder(MAX_PATH)
        Dim path As New StringBuilder(MAX_PATH)
        Try
            hTitle = GCHandle.Alloc(_DialogTitle, GCHandleType.Pinned)
            If Not owner Is Nothing Then
                _handle = owner.Handle
            Else
                _handle = IntPtr.Zero
            End If
            udtBI.hWndOwner = _handle
            udtBI.lpszTitle = _DialogTitle
            udtBI.ulFlags = CType(&H1, Integer)
            buffer.Length = MAX_PATH
            udtBI.pszDisplayName = buffer.ToString()
            lpIDList = SHBrowseForFolder(udtBI)
            hTitle.Free()
            If lpIDList.ToInt64() <> 0 Then
                SHGetPathFromIDList(lpIDList, path)
                _DirectoryName = path.ToString()
                CoTaskMemFree(lpIDList)
                Return DialogResult.OK
            Else
                Return DialogResult.Cancel
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    '''
    Public Property DialogTitle() As String
        Get
            Return _DialogTitle
        End Get
        Set(ByVal value As String)
            _DialogTitle = value
        End Set
    End Property
    '''
    Public ReadOnly Property DirectoryName() As String
        Get
            Return _DirectoryName
        End Get
    End Property
    '''
End Class

转载于:https://www.cnblogs.com/xioxu/archive/2008/05/06/1185506.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值