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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read