In your form:
Option Strict Off
Option Explicit On
'
'
************************************************** **************************
**************************
' Created by Julian Milano
' JDMils Software Development Pty Ltd
'
jd****@datafast.net.au
'
ju****@jdmils.com
'
' Date: 19 Jul 2005
'
' This application cycles thru all active Windows sessions looking for a
' particular window which it will then attempt to kill (close).
'
' The application code demonstrates the use of:
' 1....Enumerating Windows sessions by API
' 2....Kill (Close) Windows sessions by API
' 3....Get a window session's title-bar text by API
' 4....Call back functions
' 5....Raising events
'
' The code in this application is copywrite, but you can use it simply by
emailing me.
'
************************************************** **************************
**************************
'
Friend Class frmAskWinCaption
Inherits System.Windows.Forms.Form
#Region "Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
If Disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(Disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Public ToolTip1 As System.Windows.Forms.ToolTip
Public WithEvents Command1 As System.Windows.Forms.Button
Public WithEvents Label1 As System.Windows.Forms.Label
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents cbCaseSensitive As System.Windows.Forms.CheckBox
Public WithEvents tbWindowText As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(frmAskWin Caption))
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.Command1 = New System.Windows.Forms.Button
Me.tbWindowText = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.cbCaseSensitive = New System.Windows.Forms.CheckBox
Me.SuspendLayout()
'
'Command1
'
Me.Command1.BackColor = System.Drawing.SystemColors.Control
Me.Command1.Cursor = System.Windows.Forms.Cursors.Default
Me.Command1.Font = New System.Drawing.Font("Arial", 8.0!,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.Command1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command1.Location = New System.Drawing.Point(8, 56)
Me.Command1.Name = "Command1"
Me.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Command1.Size = New System.Drawing.Size(120, 33)
Me.Command1.TabIndex = 1
Me.Command1.Text = "Kill It"
'
'tbWindowText
'
Me.tbWindowText.AcceptsReturn = True
Me.tbWindowText.AutoSize = False
Me.tbWindowText.BackColor = System.Drawing.SystemColors.Window
Me.tbWindowText.Cursor = System.Windows.Forms.Cursors.IBeam
Me.tbWindowText.Font = New System.Drawing.Font("Arial", 8.0!,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.tbWindowText.ForeColor = System.Drawing.SystemColors.WindowText
Me.tbWindowText.Location = New System.Drawing.Point(8, 24)
Me.tbWindowText.MaxLength = 0
Me.tbWindowText.Name = "tbWindowText"
Me.tbWindowText.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.tbWindowText.Size = New System.Drawing.Size(264, 25)
Me.tbWindowText.TabIndex = 0
Me.tbWindowText.Text = "Enter Window Session Name"
'
'Label1
'
Me.Label1.BackColor = System.Drawing.SystemColors.Control
Me.Label1.Cursor = System.Windows.Forms.Cursors.Default
Me.Label1.Font = New System.Drawing.Font("Arial", 8.0!,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.Label1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Label1.Size = New System.Drawing.Size(233, 17)
Me.Label1.TabIndex = 2
Me.Label1.Text = "Enter the partial name of the window to close:"
'
'cbCaseSensitive
'
Me.cbCaseSensitive.Checked = True
Me.cbCaseSensitive.CheckState =
System.Windows.Forms.CheckState.Checked
Me.cbCaseSensitive.Location = New System.Drawing.Point(136, 64)
Me.cbCaseSensitive.Name = "cbCaseSensitive"
Me.cbCaseSensitive.Size = New System.Drawing.Size(136, 16)
Me.cbCaseSensitive.TabIndex = 3
Me.cbCaseSensitive.Text = "Case Sensitive Search"
'
'frmAskWinCaption
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.SystemColors.Control
Me.ClientSize = New System.Drawing.Size(280, 95)
Me.Controls.Add(Me.cbCaseSensitive)
Me.Controls.Add(Me.Command1)
Me.Controls.Add(Me.tbWindowText)
Me.Controls.Add(Me.Label1)
Me.Cursor = System.Windows.Forms.Cursors.Default
Me.Font = New System.Drawing.Font("Arial", 8.0!,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.Icon = CType(resources.GetObject("$this.Icon"),
System.Drawing.Icon)
Me.Location = New System.Drawing.Point(4, 22)
Me.Name = "frmAskWinCaption"
Me.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScree n
Me.Text = "Find & Kill Window Sessions"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles Command1.Click
Dim KillWindow As Object
KillWindow = FindWindowWild("*" & tbWindowText.Text & "*",
cbCaseSensitive.Checked)
If KillWindow = 0 Then
tbWindowText.Text = "Sorry. [" & tbWindowText.Text & "] was not
found."
Else
SendMessage(KillWindow, WM_CLOSE, 0, 0)
tbWindowText.Text = "[" & tbWindowText.Text & "] was killed."
End If
End Sub
End Class
In a module:
Option Strict Off
Option Explicit On
Module mAPICode
#Region "API Declares"
Private Delegate Function CallBack(ByVal hwnd As Integer, ByVal lParam
As Integer) As Boolean
Private Declare Function EnumWindows Lib "User32" (ByVal lpEnumFunc As
CallBack, ByVal lParam As Integer) As Integer
Private Declare Function GetWindowText Lib "user32" Alias
"GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch
As Integer) As Integer
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As
Integer) As Boolean
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Integer)
As Integer
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA"
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer,
ByVal lParam As Integer) As Integer
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal
hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef
lParam As Integer) As Integer
Public Const WM_CLOSE As Short = &H10S
#End Region
#Region "Events"
'
'
************************************************** **************************
**************************
' Raise an event to signify a window was found- any window.
'
************************************************** **************************
**************************
'
Public Event WndH(ByVal hWnd As Integer)
#End Region
#Region "Called Back Function"
'
'
************************************************** **************************
**************************
' This is the called function. hwnd will be the window being queried.
'
************************************************** **************************
**************************
'
Private Function retHndW(ByVal hwnd As Integer, ByVal lParam As Integer)
As Boolean
' Raise an event signifying a window session was found.
RaiseEvent WndH(hwnd)
Dim k As Long, sName As String
' Check if the window session is visible- Windows has hidden window
sessions
' running all the time.
If IsWindowVisible(hwnd) Then
' Prepare the string in which the window's title-bar caption
will be returned.
sName = Space$(128)
' Get the title-bar text of this window.
k = GetWindowText(hwnd, sName, 128)
' Check length of title-bar text.
If k > 0 Then
sName = Left$(sName, k)
' If Case is not sensitive, then convert title-bar text to
upper-case.
If Not mMatchCase Then sName = sName.ToUpper()
' Match it to the serach string
If sName Like sPattern Then
' Match found.
hFind = hwnd
Return False
Exit Function
End If
End If
End If
' Return True so that this call is passed onto the next Windows
process.
Return True
End Function
#End Region
#Region "Module Level Variables"
Dim sPattern As String
Dim hFind As Integer
Dim mMatchCase As Boolean = True
#End Region
#Region "Window Session Functions"
'
'
************************************************** **************************
**************************
' This function closes the selected window session.
'
************************************************** **************************
**************************
'
Public Function CloseWindowByhWnd(ByRef hwnd As Integer) As Integer
On Error GoTo ErrorCloseWindowByhWnd
CloseWindowByhWnd = PostMessage(hwnd, WM_CLOSE, 0, 0)
Exit Function
ErrorCloseWindowByhWnd:
MessageBox.Show(Err.Number & ":Error in CloseWindowByhWnd. Error
Message: " & _
Err.Description, MsgBoxStyle.Critical, "Warning")
End Function
'
'
************************************************** **************************
**************************
' This sub sets up the call back function. Windows will call retHndW
when a window is found.
'
************************************************** **************************
**************************
'
Public Sub GetWindows()
Call EnumWindows(AddressOf retHndW, 0)
End Sub
#End Region
#Region "API Interface Function"
'
'
************************************************** **************************
**************************
' This function is the interface to the outside world.
'
************************************************** **************************
**************************
'
Public Function FindWindowWild(ByVal sWild As String, Optional ByVal
bMatchCase As Boolean = True) As Long
mMatchCase = bMatchCase
On Error Resume Next
sPattern = sWild
hFind = 0
' If Case is not sensitive, then convert title-bar text to
upper-case.
If Not bMatchCase Then sPattern = UCase(sPattern)
' Cycle thru all windows.
Call GetWindows()
' Return the hwnd of the window session being sought.
Return hFind
End Function
#End Region
End Module
You need to search for the window with the particular text in the title and
kill it when your app is closing down.
--
|
+-- Thief_
|
"Michael D. Reed" <ch******@online.nospam> wrote in message
news:9B**********************************@microsof t.com...
I am using the help class to display a simple help file. I generated the
help file using Word and saving it as a single page Web page (.mht
extension). I show the help file with the following statement.
Help.ShowHelp(Parent:=Me, url:=Me.HELP_URL_PRE & Me.myWorker.HelpFile)
How do I get it to go away when the program exits? Now when I quit the
program that I called it form the help file is sill displayed. Is there a
way to get a handle to the instance of the IE browser that the help file
is in and then using this handle to kill it?
--
Mike Reed