Common Dialog Message | CDialog Function | Description |
CDM_GETFILEPATH | GetPathName | Retrieves the path and file name of the selected file; for example, "c:\mumble\bletch\foo.txt". |
CDM_GETSPEC | GetFileName | Retrieves the file name (not including the path) of the file currently selected in the dialog box; for example, "foo.txt". |
<none> | GetFileExt | Gets the file name extension of the file currently selected in the dialog; for example, "txt". |
CDM_GETFOLDERPATH | GetFolderPath | Retrieves the path of the current folder or directory; for example, "c:\mumble\bletch". |
CDM_HIDECONTROL | HideControl | Hides a control. |
CDM_SETCONTROLTEXT | SetControlText | Sets the text of a control. |
CDM_SETDEFEXT | SetDefExt | Sets the default file name extension. |
CDM_GETFOLDERIDLIST | <none> | Retrieves the address of the item identifier list corresponding to the currently open folder. |
Common Dialog Notification | CDialog Virtual Function | Description |
CDN_FILEOK | OnFileNameOK | The user clicked the OK button; the dialog box is about to close. |
CDN_FOLDERCHANGE | OnFolderChange | The user opened a new folder or directory. |
CDN_HELP | <none> | The user clicked the Help button. MFC routes this through its normal help mechanism by converting it to WM_COMMAND with ID_HELP as the ID and, if that fails, WM_COMMANDHELP. |
CDN_INITDONE | OnInitDone | The system has finished initializing the dialog box, and the dialog box has finished processing the WM_INITDIALOG message. Also, the system has finished arranging controls in the common dialog box to make room for the controls of the child dialog box (if any). |
CDN_SELCHANGE | OnFileNameChange | The user selected a new file or folder from the file list. |
CDN_SHAREVIOLATION | OnShareViolation | The common dialog box encountered a sharing violation on the file about to be returned. |
CDN_TYPECHANGE | OnTypeChange | The user selected a new file type from the list of file types. |
Figure 7 MyDlg
MyDlg.h
////////////////////////////////////////////////////////////////
// 1997 Microsoft Systems Journal.
// If this program works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//////////////////
// My open dialog
//
class CMyOpenDlg : public CFileDialog {
protected:
CEdit m_edit; // edit control to display info
void AddText(LPCTSTR lpText); // helper adds text to edit control
public:
DECLARE_DYNAMIC(CMyOpenDlg)
CMyOpenDlg();
virtual BOOL OnInitDialog();
virtual int DoModal();
// called for both Explorer and old-style Open dialogs
virtual BOOL OnFileNameOK();
virtual void OnLBSelChangedNotify(UINT nIDBox, UINT iCurSel, UINT nCode);
// only called if OFN_EXPLORER is set
virtual void OnInitDone();
virtual void OnFileNameChange();
virtual void OnFolderChange();
virtual void OnTypeChange();
DECLARE_MESSAGE_MAP()
afx_msg void OnPressMe();
};
MyDlg.cpp
////////////////////////////////////////////////////////////////
// OPENDLG 1997 Microsoft Systems Journal.
// If this program works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
#include "StdAfx.h"
#include "MyDlg.h"
#include "Resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// IDs of controls in Explorer-style dialog, from Spy
#define ID_FILESOFTYPE 0x0441
#define ID_FILENAME 0x0442
#define ID_LOOKIN 0x0443
////////////////////////////////////////////////////////////////
// CMyOpenFileDlg
//
IMPLEMENT_DYNAMIC(CMyOpenDlg, CFileDialog)
BEGIN_MESSAGE_MAP(CMyOpenDlg, CFileDialog)
ON_COMMAND(ID_PRESS_ME, OnPressMe)
END_MESSAGE_MAP()
CMyOpenDlg::CMyOpenDlg() : CFileDialog(TRUE,
NULL, // no default extension
NULL, // ..or file name
OFN_HIDEREADONLY, // no read-only checkbox
_T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"))
{
// custom title
m_ofn.lpstrTitle = _T("Like, pick a file, dude");
}
//////////////////
// Helper adds text to an edit control that I've added to the Open dialog
//
void CMyOpenDlg::AddText(LPCTSTR lpText)
{
if (m_edit) {
// Convert \n to \n\r for Windows brain-damaged edit control
// It's 1998, and I'm still writing code like this!
//
LPCTSTR src = lpText;
TCHAR buf[1024];
TCHAR* dst = buf;
TCHAR* endbuf = buf + sizeof(buf) - 1;
while (*src && dst < endbuf) {
if (*src == '\n')
*dst++ = '\r';
*dst++ = *src++;
}
*dst = 0;
m_edit.SetSel(-1, -1); // end of edit text
m_edit.ReplaceSel(buf); // append string..
m_edit.SendMessage(EM_SCROLLCARET); // ..and show caret
} else {
TRACE(lpText);
}
}
//////////////////
// Initialize dialog.
// For Explorer, this is actually a child of the main dialog.
//
BOOL CMyOpenDlg::OnInitDialog()
{
if (m_ofn.Flags & OFN_EXPLORER) {
SetControlText(IDOK, _T("Do it, Man!"));
SetControlText(IDCANCEL, _T("Not!"));
SetControlText(ID_LOOKIN, _T("Lookey here:"));
SetControlText(ID_FILENAME, _T("Which file:"));
SetControlText(ID_FILESOFTYPE,_T("Pick a type:"));
m_edit.SubclassDlgItem(IDC_MYDLGINFO, this);
AddText(_T("Look here as you use the controls\n"));
} else {
SetDlgItemText(IDOK, _T("Do it, Man!"));
SetDlgItemText(IDCANCEL, _T("Not!"));
}
return CFileDialog::OnInitDialog();
}
//////////////////
//
int CMyOpenDlg::DoModal()
{
if (m_ofn.Flags & OFN_EXPLORER) {
// Add my custom dialog to bottom
m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_MYOPENDLG);
m_ofn.Flags |= OFN_ENABLETEMPLATE;
}
return CFileDialog::DoModal();
}
//////////////////
// Silly button handler
//
void CMyOpenDlg::OnPressMe()
{
MessageBox(_T("Why?"), _T("OpenDlg"), MB_OK);
}
////////////////////////////////////////////////////////////////
// Below are handlers for the Explorer-style CDN_ notifications.
// MFC converts the Se to virtual functions, NOT message handlers!
//
// My overrides just add a message to the info window
//
// For both Explorer-style and old-style dialogs: User wants to open file
//
BOOL CMyOpenDlg::OnFileNameOK()
{
CString s;
s.Format(_T("CMyOpenDlg::OnFileNameOK(%s)\n"), (LPCTSTR)GetPathName());
AddText(s);
return 0; // let Explorer do it.
}
// For old-style dialogs: User changed one of the listboxes
//
void CMyOpenDlg::OnLBSelChangedNotify(UINT nIDBox, UINT iCurSel, UINT nCode)
{
CString s;
s.Format(_T("OnLBSelChangedNotify(nIDBox=%d, iCurSel=%d, nCode=%d)\n"),
nIDBox, iCurSel, nCode);
AddText(s);
}
// For Explorer-style dialogs: initialization is done
//
void CMyOpenDlg::OnInitDone()
{
AddText(_T("OnInitDone\n"));
}
// For Explorer-style dialogs: user selected a new file
//
void CMyOpenDlg::OnFileNameChange()
{
CString s;
s.Format(_T("OnFileNameChange: %s\n"), (LPCTSTR)GetFileName());
AddText(s);
}
// For Explorer-style dialogs: user selected a new folder
//
void CMyOpenDlg::OnFolderChange()
{
CString s;
s.Format(_T("OnFolderChange: %s\n"), (LPCTSTR)GetFolderPath());
AddText(s);
}
// For Explorer-style dialogs: user selected a new file type (from drop-down)
//
void CMyOpenDlg::OnTypeChange()
{
CString s;
s.Format(_T("OnTypeChange: nFilterIndex = %d\n"), m_ofn.nFilterIndex);
AddText(s);
}
OpenDLG.cpp
////////////////////////////////////////////////////////////////
// OPENDLG Copyright 1997 Microsoft Systems Journal.
// If this program works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// OPENDLG shows how to customize the MFC CFileDialog in Windows 95
// Compiles with Visual C++ 5.0 or greater.
//
#include "StdAfx.h"
•
•
•
CApp::CApp()
{
// default: use Explorer-style opendialog
m_bExplorerStyleOpenDialog = TRUE;
}
////////////////
// Run special File Open dialog
//
void CApp::OnFileOpen()
{
CMyOpenDlg dlg;
// do Explorer-style or old style depending on m_bExplorerStyleOpenDialog
if (!m_bExplorerStyleOpenDialog)
dlg.m_ofn.Flags &= ~OFN_EXPLORER;
dlg.DoModal();
}
//////////////////
// Use Explorer-style open dialog
//
void CApp::OnViewExplorer()
{
m_bExplorerStyleOpenDialog = TRUE;
}
void CApp::OnUpdateViewExplorer(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio(m_bExplorerStyleOpenDialog);
}
//////////////////
// Use old-style Open dialog
//
void CApp::OnViewOlddlg()
{
m_bExplorerStyleOpenDialog = FALSE;
}
void CApp::OnUpdateViewOlddlg(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio(!m_bExplorerStyleOpenDialog);
}