How to restrict Window Movement?

This is beginner level post. How we can make a Window immovable?

Roughly we’ve two methods to do this.

Method #1. Handle the NCHITTEST message and ignore while user click on the caption area (Titlebar). This method doesn’t work well in Windows 7/Vista if Aero is enabled. This method works with any type of Window having titlebar. The disadvantage of this method is , the user will still able to move using keyboard, if system menu is available.

 

LRESULT CMoveWindowSampleDlg::OnNcHitTest(CPoint point)
{
    UINT nHitTest = CDialogEx::OnNcHitTest(point);

    if( HTCAPTION == nHitTest )
        nHitTest = HTNOWHERE;

    return nHitTest;
}

Method #2 – Remove the Move command from System Menu (Works with windows having system menu stle (WS_SYSMENU) ). The the Move command in the system menu will be removed in this case. Windows will internally disable the movement of this kind of Windows.

BOOL CMoveWindowSampleDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    CMenu* pSysMenu = GetSystemMenu(FALSE);

    if (pSysMenu != NULL)
    {
        pSysMenu->RemoveMenu( SC_MOVE, MF_BYCOMMAND );
       }
...

       return TRUE;
}

PS: Using method 2, this is the same method we use to disable the close button of a Window. For disabling a menu/titlebar button. Call EnableMenu function with relevant parameters.

How to remove the Document name from the SDI/MDI Window titlebar?

When you create a simple SDI Application, you can see that the Title of the Window is displayed as “Document Name – Application name” like you’re seeing below.

image

How we can remove this? It’s simple, just remove the FWS_ADDTOTITLE style from the frame window. You’ve to speicify this before the window being created. You can write the code in PreCreateWindow function.

See the snippet below

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
 {
 	cs.style &= ~ FWS_ADDTOTITLE;	// Remove the style
 	if( !CFrameWndEx::PreCreateWindow(cs) )
 		return FALSE;
 	return TRUE;
}

After that you can see the Window title like this!

image