Making a Window "Always On Top"

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
      ,      // 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.

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.


Download demo project - 12 Kb


  • the way to implement but do not need to get the current window size and position

    Posted by darena on 05/15/2006 11:01pm

    ::SetWindowPos(m_hWnd ,       // handle to window
                    HWND_TOPMOST,  // placement-order handle
                    0,     // horizontal position
                    0,      // vertical position
                    0,  // width
                    0, // height
                    SWP_NOMOVE|SWP_NOSIZE // window-positioning options);

  • Good !

    Posted by kapildev on 04/24/2006 12:08am

    Nice finding !

  • How can i....

    Posted by jimmymjoy on 03/11/2004 12:26am

    Is it possible to create a window which stays always on the top and it should be actve always. In this case when i click some other applications which is already opened, the focus of the window is lost. ie no other application should not work while this active window is there including the windows key. thankx jim

  • Can this be restricted to a application ?

    Posted by Legacy on 01/22/2004 12:00am

    Originally posted by: Ajit

    Is there any way to restrict a dialog box to be "Always on Top" only for its parent application and not for whole system ? It will be something like "property pages". But I want it for the dialog box.

  • good!

    Posted by Legacy on 10/11/2003 12:00am

    Originally posted by: LinGuang

    thank you!

  • It wont work >.< (im newb)

    Posted by Legacy on 10/02/2003 12:00am

    Originally posted by: Brandon

    hey ya that code dont work for me i dont know why (as i just started taking a class few weaks ago, only in highschool) but at home i like to mes around, and like i was wondering like wats the script to make my dialog alwas on top (without having to click a button). Also the name of my dialog is: DisableClose so like if u can replace the name of like the C(wateveryourprogramis) cuz im newb >.<

  • Always on top with Flash?

    Posted by Legacy on 06/18/2003 12:00am

    Originally posted by: Claudia Azor�n

    Hi, how can I do an always on top window with Flash (Remoting MX or ColdFusion), it's possible?
    Or with JSP? I have to do that the content of a tutorial runs in an always on top window, like in the book serie Dr. Max? Do you know it?

    Thank's you very much! :)

  • ok so how do i...

    Posted by Legacy on 09/09/2002 12:00am

    Originally posted by: david rush

    ...create an app/dialog which is never on top?

  • Where can get the rsource.rc file ?

    Posted by Legacy on 07/16/2002 12:00am

    Originally posted by: jacky.xue

    Sorry i just can't find this file .Would you show me how to get this file ,or create my myself ?

  • Always On Top

    Posted by Legacy on 03/21/2002 12:00am

    Originally posted by: joby

    how to do this one in SDK..

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

  • As everyone scrambles to protect customers and consumers from the Heartbleed virus, there will be a variety of mitigating solutions offered up to address this pesky bug. There are a variety of points within the data path where solutions could be put into place to mitigate this (and similar) vulnerabilities and customers must choose the most strategic point in the network at which to deploy their selected mitigation. Read this white paper to learn the ins and outs of mitigating the risk of Heartbleed and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds