Microsoft & .NETVisual C#Making a Window "Always On Top"

Making a Window “Always On Top”

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

History

Date Posted: August 09, 2000

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories