INF: Keeping a Window on Top of All Other Windows

ID Number: Q71573

3.00

MS-DOS

Summary:

By using a timer, a pop-up window can be made to behave like a

"topmost" window, a window that remains on top of all other windows

in the system. Such a window is brought to the top of all other

windows whenever a WM_TIMER message is sent to its window function.

More Information:

To create a topmost window, first register a new window class. Using

that new class, create a pop-up window without a parent. The window is

created without a parent so that it is independent of the main

overlapped window created for each application in the system. This

will ensure that the topmost window remains open on the desktop even

if the main window is minimized.

After creating this pop-up window, create a timer with the SetTimer()

function and associate it with the window. Set the elapsed time to 100

milliseconds. The window function for the topmost window will process

the WM_TIMER message and call SetWindowPos() to place the topmost

window on top of all other windows. The code that processes the timer

message may be similar to the following:

case WM_TIMER:

GetWindowRect(hWnd, &rect);

SetWindowPos(hWnd,

NULL,

rect.left,

rect.top,

0,

0,

SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);

break;

The window coordinates returned by GetWindowRect() are used in the

call to SetWindowPos(). Doing this allows the user to move the topmost

window (if it has a caption bar) and it will remain on top.

The wFlags parameter for SetWindowPos() also contains the

SWP_NOACTIVATE flag. This is required so that the window can remain on

top without moving the focus to that window. Failing to do this will

disable all other applications in the system.

When the application that installed the topmost window is terminated,

the timer should be removed using the KillTimer() function. This call

can be placed in the processing of the WM_DESTROY message in the

topmost window's window procedure.

With this implementation, when a menu is activated, the screen will

flash as the menu is painted and then the topmost window is painted

over the menu. Menu selections can be made normally. However,

depending upon the topmost window's position, not all menu items will

be properly visible.