|
■No55896 (やんまー さん) に返信
>>SelectionChanged イベントで代用できませんか?
> そもそもこれはどうやって書いたらいいのでしょうか。
レイトバインドだと面倒そうなので、タイプライブラリを用いる方法でのサンプルです。
参照設定で、COM の [Microsoft Shell Controls And Automation] を加えておいてください。
Imports System.Runtime.InteropServices
Public Class Form1
'WithEvents にはしない
Private FolderView As Shell32.ShellFolderView
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
ListBox2.Items.Clear()
WebBrowser1.Navigate(TextBox1.Text)
End Sub
Private Sub FolderView_SelectionChanged()
Dim fld As Shell32.Folder = FolderView.Folder
'ListBox1 に「FocusedItem」を取得
ListBox1.BeginUpdate()
ListBox1.Items.Clear()
Dim focusedItem As Shell32.FolderItem = FolderView.FocusedItem()
ListBox1.Items.Add(focusedItem.Path)
For column As Integer = 0 To 63
Dim title As String = fld.GetDetailsOf(fld, column)
If String.IsNullOrEmpty(title) Then
Continue For
End If
ListBox1.Items.Add(" " & title)
ListBox1.Items.Add(" " & fld.GetDetailsOf(focusedItem, column))
Next
Marshal.FinalReleaseComObject(focusedItem)
ListBox1.EndUpdate()
'ListBox2 に「SelectedItems」を取得
ListBox2.BeginUpdate()
ListBox2.Items.Clear()
Dim selectedItems As Shell32.FolderItems = FolderView.SelectedItems()
ListBox2.Items.Add(String.Format("{0:#,0}個のアイテムが選択されています。", selectedItems.Count))
For n As Integer = 0 To selectedItems.Count - 1
Dim fi As Shell32.FolderItem = selectedItems.Item(n)
ListBox2.Items.Add(fi.Path)
Marshal.FinalReleaseComObject(fi)
Next
Marshal.ReleaseComObject(selectedItems)
ListBox2.EndUpdate()
Marshal.ReleaseComObject(fld)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.AllowWebBrowserDrop = False
WebBrowser1.AllowNavigation = True
ListBox1.Items.Add("フォーカスのあるアイテム")
ListBox2.Items.Add("選択されているアイテム")
TextBox1.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
Button1.Text = "移動"
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If FolderView IsNot Nothing Then
RemoveHandler FolderView.SelectionChanged, AddressOf FolderView_SelectionChanged
If Marshal.IsComObject(FolderView) Then
Marshal.ReleaseComObject(FolderView)
End If
FolderView = Nothing
End If
Dim doc As Object = Nothing
Try
doc = WebBrowser1.ActiveXInstance.Document
FolderView = TryCast(doc, Shell32.ShellFolderView)
AddHandler FolderView.SelectionChanged, AddressOf FolderView_SelectionChanged
Catch ex As Exception
MsgBox(ex.Message)
Finally
If doc IsNot Nothing AndAlso FolderView Is Nothing Then
If Marshal.IsComObject(doc) Then
Marshal.ReleaseComObject(doc)
End If
End If
End Try
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, _
ByVal e As FormClosingEventArgs) Handles Me.FormClosing
If FolderView IsNot Nothing Then
If Marshal.IsComObject(FolderView) Then
Marshal.ReleaseComObject(FolderView)
End If
End If
End Sub
End Class
|