How to Keep an MDI Window Always on Top

Last reviewed: November 2, 1995
Article ID: Q108315
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

When creating a multiple document interface (MDI) window, there are no styles available to have the new window stay on top of the other MDI windows. Alternatively, two methods are available to achieve this functionality:

  • Process the WM_WINDOWPOSCHANGED message and call SetWindowPos() to change the Z-order of the window.
  • Install a timer for the MDI windows and reset the Z-order of the window when processing the WM_TIMER message.

MORE INFORMATION

MDICREATESTRUCT has the field "style", which can be set with the styles for the new MDI window. Extended styles, such as WS_EX_TOPMOST, are not available in MDI windows. This field of MDICREATESTRUCT is passed to CreateWindowEx() in the dwStyle parameter. The dwExStyle field is set to 0L. The two methods shown below cannot be used at the same time in the same application.

Method 1: Process the WM_WINDOWPOSCHANGED message and call SetWindowPos() to change the Z-order of the window.

Sample Code

LRESULT CALLBACK MdiWndProc (HWND hWnd, UINT message, WPARAM wParam,

                    LPARAM lParam)
{
    static HWND hWndAlwaysOnTop = 0;
    switch (message)
     {
     case WM_CREATE :
         if (!hWndAlwaysOnTop)
          {
          SetWindowText (hWnd, "Always On Top Window");
          hWndAlwaysOnTop = hWnd;
          }
            break;
     case WM_WINDOWPOSCHANGED :
         if (hWndAlwaysOnTop)
          {
          WINDOWPOS FAR* pWP = (WINDOWPOS FAR*)lParam;
          if (pWP->hwnd != hWndAlwaysOnTop)
              SetWindowPos (hWndAlwaysOnTop, HWND_TOP, 0, 0, 0, 0,
                      SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
          }
         break;
     //
     // Other Messages to process here.
     //
     case WM_CLOSE :
         if (hWndAlwaysOnTop == hWnd)
          hWndAlwaysOnTop = NULL;
     default :
         return DefMDIChildProc (hWnd, message, wParam, lParam);
     }
    return 0L;
}

Method 2: Install a timer for the MDI windows and reset the Z-order of the window when processing the WM_TIMER message.

Sample Code

LRESULT CALLBACK MdiWndProc (HWND hWnd, UINT message, WPARAM wParam,

                    LPARAM lParam)
{
    static HWND hWndAlwaysOnTop = 0;
    switch (message)
     {
     case WM_CREATE :
         SetTimer (hWnd, 1, 200, NULL);
         if (!hWndAlwaysOnTop)
          {
          SetWindowText (hWnd, "Always On Top Window");
          hWndAlwaysOnTop = hWnd;
          }
            break;
     case WM_TIMER :
         if (hWndAlwaysOnTop)
          {
          SetWindowPos (hWndAlwaysOnTop, HWND_TOP, 0, 0, 0, 0,
                     SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
          }
         break;
     case WM_DESTROY:
         KillTimer (hWnd, 1) ;
         break;
     //
     // Other Messages to process here.
     //
     case WM_CLOSE :
         if (hWndAlwaysOnTop == hWnd)
          hWndAlwaysOnTop = NULL;
     default :
         return DefMDIChildProc (hWnd, message, wParam, lParam);
     }
    return 0L;
}

For additional information on changing the Z-order of child pop-up windows, please see the following article(s) in the Microsoft Knowledge Base:

   ARTICLE-ID: Q66943
   TITLE     : Determining the Topmost Pop-Up Window


Additional reference words: 3.10 3.50 3.51 4.00 95
KBCategory: kbui
KBSubcategory: UsrWndw


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: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.