Making a Window "Always On Top"

Wednesday Aug 9th 2000 by Christopher Tan

Shows how to set a windows properties such that it is always shown on top of other windows

Making a window always visible, 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.

Sample Image

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 find one, so after an hour of research I came up with this code; I hope you'll find it as useful as I did.

It's 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


Date Posted: August 09, 2000

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved