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



Comments

  • Ing.

    Posted by koc on 03/04/2013 05:33am

    Nice the site still there. Helped me to hide the top line in the CFileDialog Box (ToolbarWindow32 control, Combobox an Text). The Toolbar control was trixi: // Deletes the header line in a CFileDialog Box !! //================================================ CWnd *wndDlg = GetParent(); // TolbarWindow32 control ID 0x440 (dlgs.h) CWnd *wndCtrl = wndDlg-GetDlgItem(0x440); wndCtrl-DestroyWindow(); wndCtrl-CloseWindow(); // Combobox ID 0x471 (dlgs.h) wndCtrl = wndDlg-GetDlgItem(0x471); wndCtrl-ShowWindow(FALSE); // Text ID 0x443 (dlgs.h) wndCtrl = wndDlg-GetDlgItem(0x443); wndCtrl-ShowWindow(FALSE);

    Reply
  • I want to provide my own handler for Open button

    Posted by Vivek Kumar on 10/06/2012 09:40am

    I have added a button to the CFileOpen dialog which selects all file and folder(if it is containing the desired file). So if this is the case, and user click on "Open" button, then instead of opening the selected folder which is the default implementation, I want to close the dialog passing IDOK

    Reply
  • VC70 - dosen't work !

    Posted by Legacy on 10/07/2003 12:00am

    Originally posted by: Svyatoslav

    Converted project dosen't work under VC70. When I select File Open the application(converted sample) disappeared(closed) without any message. Why?
    Best regards.

    • a solution!

      Posted by namhh on 02/19/2006 09:26am

      Microsoft changed the CFileDialog a bit in .NET, so the GetDlgItem(edt1) returns null. edt1 was replaced with a combobox, cmb13. Also, there a new checkbox, chx1, ("open as read-only") that has to be dealt with. Change const UINT cNum = 7; //Number of controls int Control[cNum] = { stc3, stc2, //The two label ctrls edt1, cmb1, //Edit and combo ctrls IDOK, IDCANCEL, //The dialog buttons lst1 }; //The Explorer window to const UINT cNum = 8; //Number of controls int Control[cNum] = { stc3, stc2, //The two label ctrls cmb13, chx1, cmb1, //Edit and combo ctrls IDOK, IDCANCEL, //The dialog buttons lst1 }; //The Explorer window

      Reply
    Reply
  • maximize dialog based on systemmetrics

    Posted by Legacy on 04/15/2003 12:00am

    Originally posted by: Koekke

    Is there any way to make a dialog automatically maximize based on systemmetrics?
    I need a dialog to fill my screen after some input-actions in my main dialog.
    it's a dialog based application.
    any suggestions would be nice!

    Reply
  • How to trap messages in a CFileDialog-derived dialog

    Posted by Legacy on 03/14/2003 12:00am

    Originally posted by: Chris

    I have a CFileDialog-derived dialog. I added OnMove, OnSize and OnWindowPosChanged message handlers in the dialog. However, the handlers are only called during the creation. Afterwards, I can't trap the messages anymore when I moved the dialog. Such problem doesn't exist with CDialog-derived class. Is any solution?

    Thanks

    • thx

      Posted by fantasywood on 05/05/2004 02:29am

      I've got useful info

      Reply
    Reply
  • Change font size of lst1

    Posted by Legacy on 01/24/2003 12:00am

    Originally posted by: Alain

    How could I change the text font size of the lst1 control??
    
    

    I tried
    GetParent()->GetDlgItem(lst1)->SetFont(&m_fontNormal);
    but it doesn't work...

    Please help me ;)


    Reply
  • Any code which will block / trap the download dialog box when required to save the file

    Posted by Legacy on 01/23/2003 12:00am

    Originally posted by: Anand Zadbuke

    I am working with an cyber cafe with dishnet I want to write a piece of code which block unwanted download at the cafe it should ask for password when I want to save the file
    

    Reply
  • Making the Open Dialog Smaller

    Posted by Legacy on 01/15/2003 12:00am

    Originally posted by: Thomas

    Hi;

    This is a very useful example, and I will probably use it. However, one behaviour of the base class is a problem which I haven't yet overcome. I am saving the sizing in the registry so it comes up as the user resized it from one run of the program to another. However, you cannot resize the File Dialog *smaller*, so though the user can resize it larger, they will never be able return it to a smaller size than it is initially displayed as if they wish. Clear as mud? =) This could get problematic for me. I would like them to be able to size it smaller, at least down to the default size of a standard windows CFileDialog.

    Any help would be greatly appreciated.

    Thanks,
    Thomas

    Reply
  • Cfiledialog customization

    Posted by Legacy on 01/02/2003 12:00am

    Originally posted by: sureshb

    hai,
    
    We customized the default open dialog by deriving the class from
    cfiledialog . These are the issues that we faced.
    Issues:
    1. How to handle the default messages in cfiledialog class?
    2. In Win'2000 & above, we tried to change the listview style to
    LVS_REPORT using Modifystyle method The style is changed, but the files and
    column headers are not displayed.
    3. In Win'98, how to remove the separator in toolbarwindow32.

    Please give me the solution as early as possible.

    sureshb

    Reply
  • help me to customize colors, please

    Posted by Legacy on 12/10/2002 12:00am

    Originally posted by: Uticus

    I try to customize colors in CFileDialog.
    I can't change colors of 3 cotrols :
    ListBox (lst1)( shows files in directory )
    ComboBox (cmb2)( shows current directory )
    and list view of previous ComboBox (id is 1000).

    i have alredy tried to intercept CTL_COLOR, but
    i couldn't catch notification from lst1
    and any changes when notification coming from cmb2
    and it's view has no effects

    i tried to subclass lst1, but it doesn't work.

    Anybody! Help me please!

    Uticus.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date