Creating an unmovable dialog

Someone recently asked in the MSDN forums how to create an unmovable dialog using MFC. The simplest way to do this is to remove SC_MOVE from the dialog’s system menu. This can be easily done in OnInitDialog :-

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
    pSysMenu->RemoveMenu(SC_MOVE, MF_BYCOMMAND);
}

Obviously, you need to think of why you’d want to do soemthing like this. I personally would find it very annoying if some app I installed prevented me from moving its main dialog. Though perhaps it would be okay if there was a way to minimize the app. An alternate way would be to prevent the dialog from moving via the mouse, but to still enable keyboard-based moving. To do that, we handle WM_NCHITTEST and do something like this :-

LRESULT CUnmovableDlg::OnNcHitTest(CPoint point)
{
    LRESULT result = CDialog::OnNcHitTest(point);
    return result == HTCAPTION ? HTCLIENT : result;
}

We treat HTCAPTION like HTCLIENT, so now the user cannot drag/move using the title bar. He can still move it via the keyboard (assuming you have not removed SC_MOVE). Once again, unless you have a very good reason, this is not something I would recommend at all.

4 thoughts on “Creating an unmovable dialog

  1. Move ur cursor on a title bar of some dialog box, and click the right button of ur mouse, press “Move”.. then press etc… U can move ur dialog using ur keyboard..HaHa

  2. Oh!! I cannot write some special characters. –;;
    I didn’t know that…
    and.. again…

    Move ur cursor on a title bar of some dialog box, and click the right button of ur mouse, press “Move”.. then press arrow buttons … U can move ur dialog using ur keyboard..HaHa

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s