Public Sub New(ByVal pOwner As IntPtr) m_Owner = pOwner End Sub Protected Overrides Sub Finalize() 'm_BInfo = Nothing MyBase.Finalize() End Sub Public Function ShowDirDialog() As String Dim sRet As String = "" Dim pID As IntPtr With m_BInfo .hWndOwner = m_Owner .pidlRoot = IntPtr.Zero .lpszTitle = m_Description .ulFlags = BIF_NEWDIALOGSTYLE Or BIF_EDITBOX 'EditBox表示 .lpfn = AddressOf BrowseHookProc 'コールバック関数のアドレス End With '「フォルダの参照」ダイアログの呼び出し pID = SHBrowseForFolder(m_BInfo) If Not pID = IntPtr.Zero Then sRet = New String(vbNullChar, MAX_PATH) 'SHBrowseForFolderで得られた値からフォルダのパス名を取得 SHGetPathFromIDList(pID, sRet) '割り当てられたメモリを開放 CoTaskMemFree(pID) End If Return sRet End Function 'コールバック Private Function BrowseHookProc(ByVal hwnd As IntPtr, ByVal uMsg As Integer, _ ByVal lParam As IntPtr, ByVal lpData As IntPtr) As Integer Select Case uMsg Case BFFM_INITIALIZED 'ダイアログ初期化 Dim ret As Integer = SendMessage(hwnd, BFFM_SETSELECTION, 1, _ m_InitDirectory) '初期フォルダ指定 'Console.WriteLine(ret.ToString()) Return True End Select Return False End Function '初期フォルダを指定する Public Property InitDirectory() As String Get Return m_InitDirectory End Get Set(ByVal value As String) m_InitDirectory = value & vbNullChar End Set End Property 'Descriptionの設定 Public Property Title() As String Get Return m_Description End Get Set(ByVal value As String) m_Description = value End Set End Property End Class ////////////使い方/////////// Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim clsBrowse As New SHBrowseForFolderClass(Me.Handle) Dim sRet As String With clsBrowse .InitDirectory = "E:\VS2008" .Title = "フォルダを選択して下さい。" sRet = .ShowDirDialog() End With If sRet <> "" Then MessageBox.Show(sRet) End If End Sub End Class