Costumizing CFileDialog

The Demo
The following code will make the file-open dialog 150 pixels higher, than it use to be. This is a very simple example of the things that can be done, to make the common dialogs smarter and more useable.

cmdlg.gif (10566 bytes)

What To Consider
Eventhough it has become easier to costumize the common dialogs with MFC, there still is some tricky parts.

  • If you want to change the size or appearence of some of the controls in the dialog (or the dialog it self, for that matter) you cannot just use "GetDlgItem()", because  the controls belongs to the parent of the dialog you have subclassed.
  • The id's of the controls is defined in the header file <dlgs.h> as     

            stc3, stc2                     The two label controls ("File name" and "Files of type")
            edt1, cmb1                  The edit control and the drop-down box.
            IDOK, IDCANCEL The OK and Cancel button.
            lst1                              The window that is used to browse the namespace.
            If you using a dialog with "Help" button and "Open as read-only" check button,
            there will also be ids for these controls. They are all defined in <dlgs.h>.

The sample code
The class CMyFileDialog is derived from CFileDialog using classwizard. A handler for WM_INITDIALOG has been added and that is where everything is done.

////////////////////////////////////////////////////////////////
// This is where the real action is going on
// Remember #include <dlgs.h>
BOOL CMyFileDialog::OnInitDialog() // Override
{
    // This variable should be changed acording to your wishes
    // about the size of the finished dialog
    const UINT iExtraSize = 150;
    // Number of controls in the File Dialog
    const UINT nControls = 7;    

    // Get a pointer to the original dialog box.
    CWnd *wndDlg = GetParent();

    RECT Rect;
    wndDlg->GetWindowRect(&Rect);
    // Change the size
    wndDlg->SetWindowPos(NULL, 0, 0, 
                        Rect.right - Rect.left, 
                        Rect.bottom - Rect.top + iExtraSize, 
                        SWP_NOMOVE);

    // Control ID's - defined in <dlgs.h>
    UINT Controls[nControls] = {stc3, stc2, // The two label controls
                                edt1, cmb1, // The eidt control and the drop-down box
                                IDOK, IDCANCEL, 
                                lst1}; // The Explorer window

    // Go through each of the controls in the dialog box, and move them to a new position
    for (int i=0 ; i<nControls ; i++)
    {
        CWnd *wndCtrl = wndDlg->GetDlgItem(Controls[i]);
        wndCtrl->GetWindowRect(&Rect);
        wndDlg->ScreenToClient(&Rect); // Remember it is child controls

        // Move all the controls according to the new size of the dialog.
        if (Controls[i] != lst1)
            wndCtrl->SetWindowPos(NULL, 
                            Rect.left, Rect.top + iExtraSize,
                            0, 0, SWP_NOSIZE);
        else // This is the explorer like window. It should be sized - not moved.
            wndCtrl->SetWindowPos(NULL, 0, 0,
                                Rect.right - Rect.left, 
                                Rect.bottom - Rect.top + iExtraSize, 
                                SWP_NOMOVE);
    }

    // Remember to call the baseclass.
    return CFileDialog::OnInitDialog();
}

Download the sample project/executable cmdlg.zip (31 kb)

Last updated: 11 April 1998