The following example demonstrates how to use Akemi Spy
Library to loop through all the open windows on the system.
To create this example from scratch, create a standard exe
project with one form. Open
zip file
The form has the following controls (not including the various
labels):
lstmWindows |
ListBox. Used to display the captions of all the open
windows on the system. |
txtmClassName |
TextBox. Used to display the Class Name of a selected
window. |
btnmRefresh |
CommandButton. Clicking on this button refreshes the
displayed list. |
btnmClose |
Closes the dialog. |
The code for this form is below:
Option Explicit
Private Sub btnmClose_Click()
Unload Me
End Sub
Private Sub btnmRefresh_Click()
FillListBox
End Sub
Private Sub Form_Load()
FillListBox
End Sub
Private Sub FillListBox()
'Fill lstmWindows with the captions of all the windows
'in objmWindows which are visible and have a caption.
Dim objWindow As Window
'Clear any entries from previous calls to FillListBox
lstmWindows.Clear
'Iterate through each window in the collection
For Each objWindow
In GetTopLevelWindows
'Check whether the window is even visible
If objWindow.Visible
Then
'Check whether the window even has a caption
If objWindow.Caption <> "" Then
lstmWindows.AddItem objWindow.Caption
'Set the item data for the newly added item to be the
'window's hWnd.
lstmWindows.ItemData(lstmWindows.NewIndex) = objWindow.hWnd
End If
End If
Next objWindow
End Sub
Private Sub lstmWindows_Click()
'Update txtmClassName with the class name of the currently
'selected window.
Dim objWindow As Window
Dim lFindHwnd As Long
'Get the hWnd of the selected window from the
'List Box's item data
lFindHwnd = lstmWindows.ItemData(lstmWindows.ListIndex)
Set objWindow = GetTopLevelWindows.FindWindowByHwnd(lFindHwnd)
'If the window wasn't found, then it must have been closed,
'since the last call to FillListBox.
If objWindow Is Nothing Then
txtmClassName.Text = "* Window was closed. *"
Else
'We have a winner.
txtmClassName.Text = objWindow.ClassName
End If
End Sub
|
See also: GetTopLevelWindows,
GetChildWindows, Windows
Property
|