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

  • monster beats auriculares de excelente calidad que superó

    Posted by cheneason on 06/04/2013 11:11am

    [url=http://www.beatsbydrebaratos.webgarden.com/]beats by dr dre barato[/url] DRE beat, begrebet glatte jazz, klassiske elementer blandet rock, elektronisk musik, skaber fantastiske beats by dre unik lyd. Den rytmen omdefinere personlig lydoplevelse, skabt af musikere headset. Shooting hver eneste tone, aldrig hver eneste nuance i hørt før realisme og iøjnefaldende design ørepropper trompet mundstykke, som en passende vidne føreren af fartøjet. Det er ikke kun de bedste hovedtelefoner til jazz. Uanset om du lytter, vil rytmen være et nyt højdepunkt for al din musik. [url=http://www.comprarbeatsbaratos.weebly.com/]Comprar beats baratos[/url] Et emne BIII booo kunne udrette ved hjælp af din adeptness sammen med aktivitet indenfor en ny og mere omfattende midler sammen med komplet inkluderet tilsætning i fald rigdom elektrisk strøm. Dit hygge del i de sande hovedtelefoner er dybest set vidunderligt. Du vil se disse slags Nederlag ved blot Dre nederlag opnå Nederlag ved blot at Dre Dojo hele headsets ved blot Dre krydret øretelefoner kompetente via flere superstjerner. Din BIII inkluderer en oprydning håndklæde til at foretage visse de er på jagt meget god. For nylig observerede jeg en gennemsnitlig nær ven går hele motorvejen sætte på BIII Boooplainly ved blot Doctor. Dre dojo headset af denne grund Min ægtefælle og jeg trang vil blive spurgte din ex den måde, de gjorde en udseende. [url=http://www.beat-by-dre.blinkweb.com/]Beats by dre[/url] Men hvis du ønsker at lytte til musik via en Bluetooth audio support uden at lukke dit problem stadig et kabel. Via USB giver dig de Beats by Dr. Dre Wireless på kort tid til 10 timers lytteglæde. Bare nyde i stil og trådløst fra din yndlingsmusik med Beats by Dr. Dre Wireless.The Beats Wireless leverer ligesom alle Beats By Dr. Dre produkter krystal klar diskant, naturlig mellemtone og en kraftfuld stram bas. Til også via bluetooth bedste præstation at levere den Beats Wireless med de nyeste Bluetooth-teknologi og er egnet til at modtage AAC og apt-X signaler. Selvfølgelig er dette Beats med ControlTalk.

    Reply
  • 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
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • New IT trends to support worker mobility — such as VDI and BYOD — are quickly gaining interest and adoption. But just as with any new trend, there are concerns and pitfalls to avoid.  Download this paper to learn the most important considerations to keep in mind for your VDI project.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds