Online Book Reader

Home Category

Access Cookbook - Ken Getz [239]

By Root 1793 0
)

hWnd = acb_apiGetWindow(hWnd, GW_CHILD)

' Loop through all the top-level windows.

Do While hWnd <> 0

strCaption = acbGetCaption(hWnd)

If Len(strCaption) > 0 Then

' If you got a caption, add one element to the output

' array, and fill in the information (name and hWnd).

lngStyle = acb_apiGetWindowLong(hWnd, GWL_STYLE)

' The Imp operator (Implies) returns True unless

' the first condition is True and the second is False,

' so this condition will be true unless you're

' showing visible only and the window is not visible.

If blnVisibleOnly Imp (WS_VISIBLE And lngStyle) Then

ReDim Preserve aWI(0 To intCount)

aWI(intCount).strCaption = strCaption

aWI(intCount).hWnd = hWnd

aWI(intCount).strClass = CalcClassName(hWnd)

intCount = intCount + 1

End If

End If

' Move to the next top-level window.

hWnd = acb_apiGetWindow(hWnd, GW_HWNDNEXT)

Loop

' Return the number of windows.

acbWindowList = intCount

End Function

You may find it instructive to study the code in the sample form's module. It calls acbWindowList and then uses a list-filling callback function to fill the list box on the form with window captions, classes, and handles. This is a perfect example of when you'd use such a function: you need to fill a control with data from an array that can't be gathered until the application is running, and the array might be too large to fit within the character limit imposed when you call the control's AddItem method.

Some of the windows on the list exist at the time the form is filling its list, but are not available (the Access Immediate window, for example). You can attempt to switch to them, but the attempt will fail. The code attached to the checkmark button's Click event disregards errors, so it just keeps going if an error occurs when it tries to switch the active window. See the Solution in Recipe 11.10 for information on deleting windows in this list.

11.10. Close a Running Windows Application


Problem


As part of some of your large Access applications, you often allow users to start other Windows tools (Notepad, Calculator, Calendar, etc.); once those tools are open, your application doesn't touch them. Some users have complained about all the "junk" left over once your application closes. Is there some way you can close another window from your Access application? That way, on the way out you can close any tools your application has opened.

Solution


The Solution in Recipe 11.9 demonstrated the retrieval of a list of all the running Windows applications' captions, class names, and window handles. Once you know that information, it's easy to close an application: given a window handle, simply tell it to close. Using the Windows API PostMessage function, you can close any window at any time. Of course, some applications (those that support Automation; see Chapter 12 for more information) allow themselves to be closed programmatically without using the Windows API. Other applications that don't support Automation will require either the API method described here, or SendKeys, which is unreliable at best.

Load and run frmListWindows from 11-10.MDB. This form, shown in Figure 11-12, is similar to the sample form in the Solution in Recipe 11.9 with the addition of the Stop App button, which lets you close the selected window. Try a few; you can even close Access this way, if you want.

Figure 11-12. frmListWindows includes a Stop App button

WARNING

Some top-level windows shouldn't be closed—you should never include a form like this as part of an end-user application. On the other hand, given an array of window captions and handles, you could programmatically decide which window to close and close it yourself from within your application. This form is a demonstration of the power of the method, not a tool you'd actually use.

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

Import the modules basWindowList (if you haven't already for the Solution in Recipe 11.9) and basCloseWindows.

Follow the steps listed in the Solution in Recipe 11.9 to create and fill

Return Main Page Previous Page Next Page

®Online Book Reader