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

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); }ReplyGood !
Posted by kapildev on 04/24/2006 12:08amNice finding !
ReplyHow can i....
Posted by jimmymjoy on 03/11/2004 12:26amIs 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
ReplyCan this be restricted to a application ?
Posted by Legacy on 01/22/2004 12:00amOriginally 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.
Replygood!
Posted by Legacy on 10/11/2003 12:00amOriginally posted by: LinGuang
thank you!
ReplyIt wont work >.< (im newb)
Posted by Legacy on 10/02/2003 12:00amOriginally 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 >.<
ReplyAlways on top with Flash?
Posted by Legacy on 06/18/2003 12:00amOriginally 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! :)
Replyok so how do i...
Posted by Legacy on 09/09/2002 12:00amOriginally posted by: david rush
...create an app/dialog which is never on top?
ReplyWhere can get the rsource.rc file ?
Posted by Legacy on 07/16/2002 12:00amOriginally 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 ?
ReplyAlways On Top
Posted by Legacy on 03/21/2002 12:00amOriginally posted by: joby
how to do this one in SDK..
ReplyLoading, Please Wait ...