You are currently offline, waiting for your internet to reconnect

How To Keep an MDI Window Always on Top

This article was previously published under Q108315
SUMMARY
When creating a multiple document interface (MDI) window, there are nostyles available to have the new window stay on top of the other MDIwindows. Alternatively, two methods are available to achieve thisfunctionality:
  • 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 forthe new MDI window. Extended styles, such as WS_EX_TOPMOST, are notavailable in MDI windows. This field of MDICREATESTRUCT is passed toCreateWindowEx() in the dwStyle parameter. The dwExStyle field is set to0L. The two methods shown below cannot be used at the same time in the sameapplication.

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 ofthe 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:
66943Determining the Topmost Pop-Up Window
Properties

Article ID: 108315 - Last Review: 07/11/2005 19:07:00 - Revision: 1.3

  • Microsoft Platform Software Development Kit-January 2000 Edition
  • Microsoft Windows Software Development Kit 3.1
  • kbhowto kbwndw kbmdi KB108315
Feedback
tml>> ="text/javascript" src="https://c.microsoft.com/ms.js" '="">js" '="">