Retrieve a List of All Top-Level Windows

Problem

You know you can determine if specific applications are currently running (as shown in Section 11.8.2 ), but now you’d like to obtain a list of all the running applications. That way, you could decide, as part of your application, what to present to your users. Is there a way to walk through all the open main windows and build up a list?

Solution

Windows includes API functions that allow you to walk down and around the tree of open windows, starting with the main desktop window. This solution provides a function that will do that for you, filling an array with information on each top-level window. You can then use that array to list applications, switch to them, or close them (see Section 11.10.2 for information on closing other windows).

Load and run frmListWindows from 11-09.MDB. This sample form fills a list box with all the top-level windows and provides a button that uses the VBA AppActivate command to display the selected window. In addition, the “Show visible windows only” checkbox allows you to add invisible windows to the list. Of course, attempting to use AppActivate to switch to an invisible window will fail. Figure 11-11 shows the sample form in action.

frmListWindows allows you to select and display any of the top-level windows

Figure 11-11. frmListWindows allows you to select and display any of the top-level windows

To include this functionality in your own applications, follow these steps:

  1. Import ...

Get Access Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.