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
                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


Comments

  • 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);
    }

    Reply
  • Good !

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

    Nice finding !

    Reply
  • 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

    Reply
  • 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.

    Reply
  • good!

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

    Originally posted by: LinGuang

    thank you!

    Reply
  • 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 >.<

    Reply
  • 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! :)

    Reply
  • 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?

    Reply
  • 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 ?

    Reply
  • Always On Top

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

    Originally posted by: joby

    how to do this one in SDK..

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Information is data with context. The era of Big Data has begun demonstrating to information security that there is more that can, and must, be done to identify threats, reduce risk, address fraud, and improve compliance monitoring activities by bringing better context to data and thereby creating information for actionable intelligence. This analyst report sets the stage and provides insights into IT and information security practitioners' perceptions of the impediments to, and the solutions necessary for, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds