11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7. Common Dialog Boxes<br />

To decide if this notification is CDN_SELCHANGE or not, first we need to cast lParam parameter to an<br />

OFNOTIFY type pointer. The following is the format of structure OFNOTIFY:<br />

typedef struct _OFNOTIFY {<br />

NMHDR hdr;<br />

LPOPENFILENAME lpOFN;<br />

LPTSTR pszFile;<br />

} OFNOTIFY, FAR *LPOFNOTIFY;<br />

The first member of OFNOTIFY is an NMHDR structure:<br />

typedef struct tagNMHDR {<br />

HWND hwndFrom;<br />

UINT idFrom;<br />

UINT code;<br />

} NMHDR;<br />

From its member code, we can judge if the current notification is CDN_SELCHANGE or not.<br />

Sample<br />

Sample 7.4\CDB demonstrates how to add a file preview window to Explorer-style file dialog box. If<br />

the user selects a file with “*.cpp” extension after the file dialog is activated, the contents of the file will be<br />

displayed in the preview window before it is opened.<br />

In the sample, a new command File Dialog Box | Custom File Dlg is added to the application, whose<br />

command ID is ID_FILEDIALOGBOX_CUSTOMFILEDLG. Also, a WM_COMMAND message handler is added for this<br />

command through using Class Wizard, whose correspondung member function is CCDBDoc::<br />

OnFiledialogboxCustomfiledlg().<br />

A new dialog template IDD_COMDLG32 is also added, it contains a static text control stc32, and an edit<br />

box control IDC_EDIT (Figure 7-4). This edit box has “Disabled” and “Multiline” styles. This will<br />

implement a read only edit box that can display multiple lines of text. A new class MCCusFileDialog is<br />

derived from FileDialog. In this class, function OnNotity(…) is overridden.<br />

The following is the definition of class MCCusFileDialog:<br />

class MCCusFileDialog : public CFileDialog<br />

{<br />

DECLARE_DYNAMIC(MCCusFileDialog)<br />

public:<br />

MCCusFileDialog(BOOL bOpenFileDialog,<br />

LPCTSTR lpszDefExt = NULL,<br />

LPCTSTR lpszFileName = NULL,<br />

DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,<br />

LPCTSTR lpszFilter = NULL,<br />

CWnd* pParentWnd = NULL);<br />

protected:<br />

//{{AFX_MSG(MCCusFileDialog)<br />

//}}AFX_MSG<br />

virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);<br />

DECLARE_MESSAGE_MAP()<br />

};<br />

182

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!