Keeping a Window on Top of All Other Windows

Last reviewed: January 5, 1995
Article ID: Q71573
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.0 and 3.1

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.

NOTE: In Windows version 3.1, if you have a non-child window then the extended window style WS_EX_TOPMOST is a better method for keeping it topmost. CreateWindowEx() can create a window with the extended style and SetWindowPos() can set or clear the style for a already created window.

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.


Additional reference words: 3.00 3.10
KBCategory: kbprg
KBSubcategory: UsrTim


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 5, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.