How to Get Windows Master List (Task List) Using Visual Basic

ID Number: Q78001

1.00

WINDOWS

Summary:

By calling the Windows API functions GetWindow, GetWindowText, and

GetWindowTextLength, you can get the window titles of all windows

(visible and invisible) loaded under Windows. The list of all of the

window titles under Windows 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 know as

the task list.

Further below is a sample program that demonstrates how to AppActivate

an application that is available from a list of top-level windows.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

The task list is generally a subset of the master list. The Windows

API functions only 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. The reason for this is that it is

possible for an application to remove itself from the task list, but

it will be included as 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 with 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.

Below are the steps necessary to construct a sample program that

demonstrates how to load the task list into a Visual Basic combo

box:

1. Run Visual Basic, or choose New Project from the File menu (ALT, F,

N) if Visual Basic is already running. Form1 will be created by

default.

2. Change the caption property of Form1 to AppActivate.

3. Add the following controls to Form1 and change the CtlNames as

indicated in the chart below:

Control Default Name CtlName

------- ------------ -------

Label Control Label1 Label1

Combo Box Combo1 Combo_ListItem

Command Button Command1 Command_Ok

4. Change the Caption properties of the controls as follows:

Control CtlName Caption

------- ------- ---------

Label Control Label1 Application to AppActivate:

Command Button Command_OK OK

5. Add the following code to the general declarations section of

Form1:

DefInt A-Z

'* Windows API function declarations

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

Const False = 0

Const True = Not False

'* 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

6. Add the following code in the Form_Load event procedure of Form1:

Sub Form_Load ()

Call LoadTaskList

'* Check to see if any items are in the task list, if

'* not 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

7. Enter the following code in 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

'* Simply resume if an "Illegal function call" occurs on the

'* AppActivate statement

On Local Error Resume Next

AppActivate f$

End Sub

8. Enter the following code under 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 the task name identified by

'* CurrWnd in the list

Length = GetWindowTextLength(CurrWnd)

'* Get the task name of the task in the master list

ListItem$ = Space$(Length + 1)

Length = GetWindowText(CurrWnd, ListItem$, Length + 1)

'* If there is an actual 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

9. From the Run menu, choose Start (ALT, R, S) to run the program.

From the combo box, select the window title of an application

currently running under Windows. Choose the OK button to activate the

application.

Reference(s):

"Programming Windows: the Microsoft Guide to Writing Applications

for Windows 3," Charles Petzold, Microsoft Press, 1990

"Microsoft Windows Software Development Kit: Reference Volume 1,"

version 3.0

WINSDK.HLP file shipped with Microsoft Windows 3.0 Software

Development Kit

Additional reference words: 1.00