Making a Window "Always On Top" | CodeGuru

Making a Window “Always On Top”

Making a window visible always even if it’s not the active window is very easy all you have to do is to enable “system modal” style in the window properties. But making a Dialog, SDI/MDI base application visible always at runtime requires more than just enabling some properties. This article is for developers who want […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 10, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Sample Image

Making a window visible always even if it’s not the active window is very easy all you
have to do is to enable “system modal” style in the window properties. But making a Dialog,
SDI/MDI base application visible always at runtime requires more than just enabling some properties.

This article is for developers who want their Dialog, SDI/ MDI base application to be always be visible
even if it is not the active window.

I’ve been working on an application, which can make itself always visible, as desired by the user . I
tried to find an article in codeguru that will solve the problem but I didn’t found one.
so after an hour of research I’ve came up with this code hope you’ll find it useful as I did.

Its very easy to do, for dialog base applications all you have to do is to add this method
to your dialog:

void CTopDlgDemoDlg::StayOnTop() const
{
 CRect rect;

 // get the current window size and position
 GetWindowRect( rect );

 // now change the size, position, and Z order 
 // of the window.
 ::SetWindowPos(m_hWnd ,       // handle to window
                HWND_TOPMOST,  // placement-order handle
                rect.left,     // horizontal position
                rect.top,      // vertical position
                rect.Width(),  // width
                rect.Height(), // height
                SWP_SHOWWINDOW // window-positioning options);
}

// add a control button and command handler for the button to your dialog.
// then invoke StayOnTop().
void CTopDlgDemoDlg::OnBtnClick()
{
 // make this dialog always on top.
 StayOnTop();
}

You can also use this method in SDI/MDI base applications; just add the same method
in your CMainFrame class and invoke it!

void CMainFrame::OnViewMaketop()
{
 // make this window always on top.
 StayOnTop();
}

Downloads

Download demo project – 12 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.