2009/04/18(Sat) 18:47:47 編集(投稿者)
■No35067 (W さん) に返信
> たとえばButton1を押した後にInternetExplorerを強制終了(タスクマネージャで言うプロセスの終了)をさせたいのですが
フォームに、ListBox と Button を貼っておいてください。
Option Strict On
Imports Item = System.Collections.Generic.KeyValuePair(Of Object, String)
Imports System.Runtime.InteropServices
Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If ListBox1.SelectedIndex > 0 Then
CloseIE(ListBox1.SelectedIndex)
End If
End Sub
Private Sub CloseIE(ByVal index As Integer)
Dim item As Item = DirectCast(ListBox1.Items(index), Item)
Dim internetExplorer As Object = item.Key
Dim LocationURL As String = CallByName(internetExplorer, "LocationURL", vbGet).ToString()
Dim locationName As String = item.Value
Dim message As String = "ブラウザを閉じますか?" & vbCrLf & locationName & vbCrLf & LocationURL
If MessageBox.Show(message, "IE終了", MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
= System.Windows.Forms.DialogResult.Yes Then
ListBox1.Items.RemoveAt(index)
CallByName(internetExplorer, "Quit", vbMethod)
Marshal.ReleaseComObject(internetExplorer)
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim shellWindows As Object = Activator.CreateInstance( _
Type.GetTypeFromCLSID(New Guid("{9BA05972-F6A8-11CF-A442-00A0C90A8F39}")))
Dim cnt As Integer = CInt(CallByName(shellWindows, "Count", vbGet))
ListBox1.BeginUpdate()
For c As Integer = 0 To cnt - 1
Dim internetExplorer As Object = CallByName(shellWindows, "Item", vbMethod, c)
Dim doc As Object = CallByName(internetExplorer, "Document", vbGet)
Dim docType As String = TypeName(doc)
Marshal.ReleaseComObject(doc)
If docType Like "*HTMLDocument*" Then
Dim locationName As String = CallByName(internetExplorer, "LocationName", vbGet).ToString()
ListBox1.Items.Add(New Item(internetExplorer, locationName))
Else
Marshal.ReleaseComObject(internetExplorer)
End If
Next
Marshal.ReleaseComObject(shellWindows)
ListBox1.DisplayMember = "Value"
ListBox1.EndUpdate()
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
For Each listItem As Item In ListBox1.Items
Marshal.ReleaseComObject(listItem.Key)
Next
ListBox1.Items.Clear()
End Sub
End Class