Online Book Reader

Home Category

Access Cookbook - Ken Getz [238]

By Root 2161 0
top-level window. You can then use that array to list applications, switch to them, or close them (see the Solution in Recipe 11.10 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.

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:

Import the module basWindowList from 11-09.MDB. This module includes the API declarations, constants, and wrapper functions that you'll need to list and select top-level windows.

In your code, declare an array of type acb_tagWindowInfo to hold the list of open windows, like this:

Dim atypWindowList( ) As acb_tagWindowInfo

Call acbWindowList, passing the array to be filled in and a Boolean value indicating whether to show visible windows only. The function returns the number of windows it finds. After the function call, your array will have intCount rows, with each row containing information about a specific top-level window. For example, this call will fill the array with information about all the visible top-level windows:

intCount = acbWindowList(atypWindowList( ), True)

In your application, decide which (if any) window you'd like to display, perhaps by looping through all the elements of the array. Use the AppActivate command, along with the window name, to activate the selected window:

AppActivate atypWindowList(intI).strCaption

Discussion


This example uses several functions for navigating through the hierarchy of windows. Table 11-11 describes the functions.

Table 11-11. Windows API navigation functions

Function

Purpose

GetDesktopHWnd

Retrieve the window handle for the main desktop window. All applications are children of this window.

GetWindow

Find a window in a specified relation to a specified window. In this case, you'll be looking for the first child window of the desktop window.

GetWindowLong

Retrieve one of the 32-bit pieces of information stored with a window's structure in memory. You'll need to retrieve the style information (using the GWL_STYLE constant) so you can tell whether a window is visible.

GetClassName

Retrieve the window class name for the specified window.

The acbWindowList function first retrieves a handle to the main desktop window, using GetDesktopHWnd. Once it knows that, it can find the handle for the desktop's first child window, using GetWindow. From then on, as long as the handle for the current window isn't 0, the code loops, filling in the array with information about the current window and then moving on to the next window with the GetWindow function. You'll note that the loop skips windows without captions (of which there are quite a few). Windows maintains a number of top-level hidden windows without captions for its own use. In addition, by specifying the blnVisibleOnly parameter for acbWindowList, you can include or exclude invisible windows. Windows sets up a number of invisible windows, and you probably won't want them to show up in your list. If you're interested, however, pass in False for this parameter to add all the hidden windows to your list. The code for the acbWindowList function is as follows:

Type acb_tagWindowInfo

strCaption As String

hWnd As Long

strClass As String

End Type

Public Function acbWindowList(aWI( ) As acb_tagWindowInfo, _

ByVal blnVisibleOnly As Boolean) As Integer

' Fill an array with a list of all the currently

' open top-level windows.

Dim hWnd As Long

Dim strCaption As String

Dim intCount As Integer

Dim lngStyle As Long

' Get the desktop window and, from there, the first

' top-level window.

hWnd = acb_apiGetDesktopWindow(

Return Main Page Previous Page Next Page

®Online Book Reader