Hiding a Dialog Initially


Click here to change the theme.

Occasionally a dialog needs to be hidden initially. For modal dialogs using MFC there are not any obvious solutions. The following code can be put in OnInitDialog to move the dialog off-screen. You will need to implement a method for moving it back on-screen when appropriate. Note that this is not necessary for modeless dialogs. For modeless dialogs you can either turn off the Visible style in the dialog's properties or you can use ShowWindow(SW_HIDE), whatever is appropriate for your requirements.

CRect DialogRect;
GetWindowRect(&DialogRect);
int DialogWidth = DialogRect.Width();
int DialogHeight = DialogRect.Height();
MoveWindow(0-DialogWidth, 0-DialogHeight, DialogWidth, DialogHeight);