The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for
Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0
SUMMARY
By calling the Windows API functions GetWindow, GetWindowText, and
GetWindowTextLength, you can get the window titles of all windows (visible
and invisible) currently loaded.
The list of all of the window titles is known as the master list. The
Windows Task Manager contains a list of the window titles for each of the
top-level windows (normally one per application). This list is known as the
task list.
The sample program listed below demonstrates how to activate an application
by using a list of the top-level windows (a task list).
MORE INFORMATION
This information is included with the Help file provided with Microsoft
Professional Toolkit for Visual Basic version 1.0, Microsoft Visual Basic
version 2.0, and Microsoft Visual Basic version 3.0.
The task list is generally a subset of the master list. The Windows API
functions support methods of getting the master list, not the task list.
However, from the master list you can get a list of all top-level windows
closely resembling the task list. The only difference is that the list
containing the top-level windows may have more entries than the task list
because it is possible for an application to be removed from the task list
even though it is part of the master list.
The example below demonstrates how to get the names of all top-level
windows. The names of child windows can also be obtained by calling the
GetWindow API function using the GW_CHILD constant. Although the code
example only provides an example of using the constants GW_HWNDFIRST and
GW_HWNDNEXT as arguments to GetWindow, the value of the other constants
such as GW_CHILD are provided in the code.
Here are the steps necessary to construct a sample program that
demonstrates how to load the task list into a Visual Basic combo box:
- Start Visual Basic or choose New Project from the File menu (ALT+F, N)
if Visual Basic is already running. Form1 is created by default.
- Change the caption property of Form1 to AppActivate.
- Add the following controls to Form1, and change the Name property as
indicated:
Control Default Name Name
---------------------------------------------------------
Label Control Label1 Label1
Combo Box Combo1 Combo_ListItem
Command Button Command1 Command_Ok
- Change the Caption properties of the controls as follows:
Control Name Caption
-----------------------------------------------------------
Label Control Label1 Application to AppActivate:
Command Button Command_OK OK
- Add the following code to the general declarations section of Form1:
DefInt A-Z
'Windows API function declarations:
'Enter each entire Declare statement on one, single line:
Declare Function GetWindow Lib "user" (ByVal hWnd%, ByVal wCmd%)
As Integer
Declare Function GetWindowText Lib "user" (ByVal hWnd%, ByVal lpSting$,
ByVal nMaxCount%) As Integer
Declare Function GetWindowTextLength Lib "user" (ByVal hWnd%) As Integer
'Declare constants used by GetWindow.
Const GW_CHILD = 5
Const GW_HWNDFIRST = 0
Const GW_HWNDLAST = 1
Const GW_HWNDNEXT = 2
Const GW_HWNDPREV = 3
Const GW_OWNER = 4
- Add the following code to the Form_Load event procedure of Form1:
Sub Form_Load ()
Call LoadTaskList
'If no items are in the task list, end the program.
If Combo_ListItem.ListCount > 0 Then
Combo_ListItem.Text = Combo_ListItem.List(0)
Else
MsgBox "Nothing found in task list", 16, "AppActivate"
Unload Form1
End If
End Sub
- Add the following code to the Click event procedure of the Command_Ok
button:
Sub Command_Ok_Click ()
'Get the item selected from the text portion of the combo box.
f$ = Combo_ListItem.Text
'Resume if "Illegal function call" occurs on AppActivate statement.
On Local Error Resume Next
AppActivate f$
End Sub
- Add the following code to the general declarations section of Form1:
Sub LoadTaskList ()
'Get the hWnd of the first item in the master list
'so we can process the task list entries (top-level only).
CurrWnd = GetWindow(Form1.hWnd, GW_HWNDFIRST)
'Loop while the hWnd returned by GetWindow is valid.
While CurrWnd <> 0
'Get the length of task name identified by CurrWnd in the list.
Length = GetWindowTextLength(CurrWnd)
'Get task name of the task in the master list.
ListItem$ = Space$(Length + 1)
Length = GetWindowText(CurrWnd, ListItem$, Length + 1)
'If there is a task name in the list, add the item to the list.
If Length > 0 Then
Combo_ListItem.AddItem ListItem$
End If
'Get the next task list item in the master list.
CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
'Process Windows events.
x = DoEvents()
Wend
End Sub
- From the Run menu, choose Start (ALT+R, S) to run the program.
10.From the combo box, select the window title of an application currently
running in Windows. Choose the OK button to activate the application.