With The Case Of  Release The Power OF  Visual C++ !   HomeProducts | PurchaseSupport | Downloads  
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
ActiveX COM Products
Technical Support
  General Q & A
Discussion Board
Contact Us


Get Ready to Unleash the Power of UCanCode .NET

UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"

MFC Article: Keep an Window Always on Top with WS_EX_TOPMOST and OnWindowPosChanged


Code Snippet

It is not possible to set a style (like WS_EX_TOPMOST) on an MDI child window so it stays on top of other MDI child windows. Instead, we have to mark a window to be the top most, and then manually bring that window to the top when another window is activated.

Microsoft has provided a nice article called How To Keep an MDI Window Always on Top (Q108315). But the solution(s) in the article are not implemented using the MFC framework.

Implement a global variable to remember the MDI window with the top-most status:

Collapse Copy Code
HWND CChildFrame::m_TopWindow = NULL; // There can only be one top-most window

Handle the WM_WINDOWPOSCHANGED mesage:

The WM_WINDOWPOSCHANGED message is sent to a window, when it changes size, position, or z-order. It is only sent to the window being activated (not to the window losing focus).

Collapse Copy Code

void CChildFrame::OnWindowPosChanged(WINDOWPOS* lpwndpos)
 // This message-handler is sent to the window
 // being activated (Changing position, size or z-order)
 // Check if another window has the status of being top-most, instead of this one
 if (m_TopWindow!=NULL && m_TopWindow!=m_hWnd && ::IsWindow(m_TopWindow))
  ::SetWindowPos(m_TopWindow, HWND_TOP, 0, 0, 0, 0, 

Reset the global variable when the top-most window closes:

When the top-most window is closed, we must reset the global variable so other windows will not try to bring it up front.

Collapse Copy Code

void CChildFrame::OnClose() 
 // Check if the top-most window is closing
 if (m_hWnd==m_TopWindow)
  m_TopWindow = NULL;

Finally, we need to mark a window as being the top-most:

In this sample application, the newly opened window will get the status of being the top-most, and other existing windows will bring it to the front when they are activated.

Collapse Copy Code
BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs)
 // TODO: Modify the Window class or styles here by modifying
 //  the CREATESTRUCT cs
 if( !CMDIChildWnd::PreCreateWindow(cs) )
     return FALSE;
 // The newly opened window will get the status of being the top-most
 m_TopWindow = m_hWnd;
 return TRUE;




Copyright ?1998-2022 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to