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

  • This paper introduces IBM Java on the IBM PowerLinux 7R2 server and describes IBM's implementation of the Java platform, which includes IBM's Java Virtual Machine and development toolkit.

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds