Simulate Window's Alt-Tab List and
Bring a Window to the Foreground

Learn how to identify the applications that Windows displays when you press the ALT-TAB keys. The applications listed are the ones that appear on the Windows Taskbar. Then you can select an application and click the Switch To button to make it the foreground application. Okay, if you want it to look like the Window's version too, see my Extract Icons program.

Simulate Window's Alt-Tab list and bring a window to the foreground
Download Source Code

Determine the Tasks Currently Running

To list the applications (or tasks) running we need to examine all the windows in the system and extract task information from them since there is no way to simply identify all the tasks themselves. Well, that is almost true. On 9x and 2000 machines you can use the CreateToolHelpSnapshot API but the method described here is the more generic approach. This example is only concerned with the applications that appear on the TaskBar. My Enumerate Processes example identifies all tasks currently running.

The EnumWindows API function is used to enumerate all of the existing windows. When you issue EnumWindows you pass it the address of a public function defined in your code using the AddressOf operator. The callback function, which I named fEnumWindowsCallBack, must reside in a .bas module. The operating system calls your function, fEnumWindowsCallBack in this case, for each window and passes it the handle of the identified window. EnumWindows continues until the last top-level window is enumerated or the callback function returns False.

To build our task list, my fEnumWindowsCallBack function examines each window it is passed and filters out the unwanted ones. The filtering criteria is as follows. The window must be visible as determined by the IsWindowVisible API. The window must not have any parent windows. To check this condition, a call to the GetParent API is made. If GetParent returns zero, this condition is met. Next, we want un-owned windows. A call to GetWindow using the GW_OWNER flag checks for this. Again, if a zero is returned the condition is satisfied.

Here is where it gets a little tricky. All Visual Basic applications really have an invisible window as the parent window for the application. We do not want to retrieve this window. If we do and then try to bring that window to the foreground, we will be unsuccessful. Using GetWindowLong with the GWL_EXSTYLE flag we can get extended style information about a window. Examining this information we can fine tune our selection criteria by testing whether the window is a tool tip window (the style will equal WS_EX_TOOLWINDOW) or an app window (the style will equal WS_EX_APPWINDOW).

The final test to see if we should include the window in our list is if the window has a caption or not. The GetWindowText function tells us this,

This program, by the way, is a low-fat version of my Enumerate Windows program which lists all windows in the system.

Bringing a Window to the Foreground

Making a window the foreground window requires more than just calling the SetForegroundWindow API. You must first determine the foreground thread and attach it to your window, using AttachThreadInput, then call SetForegroundWindow. That way they can share input states.

First I call GetForegroundWindow to get the handle of the current foreground window. Then a few calls to GetWindowThreadProcessId retrieve the threads associated with the current foreground window and the window I want to bring to the foreground. If these threads are the same a simple call to SetForegroundWindow is all that is necessary. Otherwise, the foreground thread is attached to the window that I am bringing to the front and detached from what was the current foreground window. The AttachThreadInput API handles this.

Once this is accomplished, the state of the window is determined. If IsIconic says that the window is minimized, I restore it by issuing ShowWindow with the SW_RESTORE flag. Otherwise ShowWindow is called with the SW_SHOW flag.

Applies To

This code uses the AddressOf operator in the EnumWindows API. As a result, it will only work with Visual Basic 5.0 or later.

Instructions

Download this project and run it to view your running applications. Start another application then hit the Refresh button to update the list. Select any application in the list and click on Switch To to bring that application to the foreground.




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page