Costumizing CFileDialog
Posted
by Christian Skovdal Andersen
on August 7th, 1998
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.

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:33amNice 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);
ReplyI want to provide my own handler for Open button
Posted by Vivek Kumar on 10/06/2012 09:40amI 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
ReplyVC70 - dosen't work !
Posted by Legacy on 10/07/2003 12:00amOriginally 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.
-
Replya solution!
Posted by namhh on 02/19/2006 09:26amMicrosoft 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
Replymaximize dialog based on systemmetrics
Posted by Legacy on 04/15/2003 12:00amOriginally posted by: Koekke
Is there any way to make a dialog automatically maximize based on systemmetrics?
ReplyI 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!
How to trap messages in a CFileDialog-derived dialog
Posted by Legacy on 03/14/2003 12:00amOriginally 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
-
Replythx
Posted by fantasywood on 05/05/2004 02:29amI've got useful info
ReplyChange font size of lst1
Posted by Legacy on 01/24/2003 12:00amOriginally posted by: Alain
ReplyAny code which will block / trap the download dialog box when required to save the file
Posted by Legacy on 01/23/2003 12:00amOriginally posted by: Anand Zadbuke
ReplyMaking the Open Dialog Smaller
Posted by Legacy on 01/15/2003 12:00amOriginally 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,
ReplyThomas
Cfiledialog customization
Posted by Legacy on 01/02/2003 12:00amOriginally posted by: sureshb
Replyhelp me to customize colors, please
Posted by Legacy on 12/10/2002 12:00amOriginally 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.
ReplyLoading, Please Wait ...