MFC supplies several classes derived from CDialog that encapsulate commonly used dialog boxes. The dialog boxes encapsulated are called the "common dialog boxes" and are part of the Windows common dialog library (COMMDLG.DLL). The dialog-template resources and code for these classes are provided in the Windows common dialog boxes that are part of Windows versions 3.1 and later. An example is available here demonstrating many of the common controls.
The CColorDialog class allows you to incorporate a color-selection dialog box into your application. A CColorDialog object is a dialog box with a list of colors that are defined for the display system.
Color Dialog Examples
// Show the Color dialog with all the default settings. CColorDialog dlg; dlg.DoModal(); // Show the fully opened Color dialog with red as the selected color. CColorDialog dlg(RGB(255, 0, 0), CC_FULLOPEN); dlg.DoModal();
// Get the selected color from the CColorDialog.
CColorDialog dlg;
if (dlg.DoModal() == IDOK)
{
COLORREF color = dlg.GetColor();
TRACE("RGB value of the selected color - red = %u,
green = %u, blue = %u\n",
GetRValue(color), GetGValue(color), GetBValue(color));
}
// Get a pointer to an array of 16 RGB color values that stores
// custom colors created by the user from CColorDialog.
CColorDialog dlg;
if (dlg.DoModal() == IDOK)
{
COLORREF* ccolor = dlg.GetSavedCustomColors();
for (int i=0; i < 16; i++)
{
TRACE("RGB value of the selected color - red = %u,
green = %u, blue = %u\n",
GetRValue(ccolor[i]),
GetGValue(ccolor[i]),
GetBValue(ccolor[i]));
}
}
The CFileDialog class encapsulates the Windows common file dialog box. Common file dialog boxes provide an easy
way to implement File Open and File Save As dialog boxes (as well as other file-selection dialog boxes) in a manner
consistent with Windows standards.
File Dialog Methods
CString GetFileExt( ) const; |
If the name of the file entered is DATA.TXT, GetFileExt returns "TXT". |
CString GetFileName( ) const; |
GetFileName will return "TEXT.DAT" for the file C:\FILES\TEXT.DAT. |
CString GetFileTitle( ) const; |
GetFileTitle will return "TEXT" for the file C:\FILES\TEXT.DAT. |
CString GetPathName( ) const; |
GetPathName will return "C:\FILES\TEXT.DAT" for the file C:\FILES\TEXT.DAT. |
File Dialog Examples
// Create an instance
CFileDialog fileDlg( TRUE, NULL, NULL, OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY, "All Files (*.*)|*.*||", this);
// Initializes m_ofn structure
fileDlg.m_ofn.lpstrTitle = "My File Dialog";
// Call DoModal
if ( fileDlg.DoModal() == IDOK)
{
CString szlstfile = fileDlg.GetPathName(); // This is your selected file name with path
AfxMessageBox("Your file name is :" +szlstfile );
}
What if you want to browse only certain types of files. Say bmp only.
CFileDialog bitmapDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_FILEMUSTEXIST, "Bitmap Files(*.bmp)|*.bmp||",this);
The CFindReplaceDialog class allows you to implement standard string Find/Replace dialog boxes in your application. Unlike the other Windows common dialog boxes, CFindReplaceDialog objects are modeless, allowing users to interact with other windows while they are on screen.
A description of it's use is here, with an example here.
The CFontDialog class allows you to incorporate a font-selection dialog box into your application. A CFontDialog object is a dialog box with a list of fonts that are currently installed in the system. The user can select a particular font from the list, and this selection is then reported back to the application.
Example
// Get the characteristics of the currently selected font, if any.
CFontDialog dlg;
if (dlg.DoModal() == IDOK)
{
LOGFONT lf;
dlg.GetCurrentFont(&lf);
TRACE("Face name of the selected font = %s\n", lf.lfFaceName);
CString facename = dlg.GetFaceName();
TRACE("Face name of the selected font = %s\n", facename);
CString stylename = dlg.GetStyleName();
TRACE("Style name of the selected font = %s\n", stylename);
int size = dlg.GetSize();
TRACE("The size of the selected font = %d\n", size);
COLORREF color = dlg.GetColor();
TRACE("Color of the selected font = %8x\n", color);
int weight = dlg.GetWeight();
TRACE("Weight of the selected font = %d\n", weight);
BOOL strikeout = dlg.IsStrikeOut();
TRACE("Is the selected font displayed with strikeout? %d\n", strikeout);
BOOL underline = dlg.IsUnderline();
TRACE("Is the selected font underlined? %d\n", underline);
BOOL bold = dlg.IsBold();
TRACE("Is the selected font bold? %d\n", bold);
BOOL italic = dlg.IsItalic();
TRACE("Is the selected font italic? %d\n", italic);
}
The CPrintDialog class encapsulates the services provided by the Windows common dialog box for printing. Common
print dialog boxes provide an easy way to implement Print and Print Setup dialog boxes in a manner consistent with
Windows standards.
Example
// Display the Windows Print dialog box with "All" radio button
// initially selected. All other radio buttons are disabled.
CPrintDialog dlg(FALSE);
if (dlg.DoModal() == IDOK)
{
// Create a printer device context (DC) based on the information
// selected from the Print dialog.
HDC hdc = dlg.CreatePrinterDC();
ASSERT(hdc);
int copies = dlg.GetCopies();
TRACE("Number of copies selected? %d\n", copies);
CString strDescription;
strDescription.Format(_T("Your default printer is %s on %s using %s.\n"),
(LPCTSTR) dlg.GetDeviceName(),
(LPCTSTR) dlg.GetPortName(),
(LPCTSTR) dlg.GetDriverName());
TRACE(strDescription);
TRACE("Driver name = %s\n",dlg.GetDriverName( ));
TRACE("Print from page %d to page %d\n",dlg.GetFromPage(),dlg.GetToPage( ));
// Get a handle to the printer device context (DC). HDC hdc = dlg.GetPrinterDC(); ASSERT(hdc); // Do something with the HDC... // Clean up. CDC::FromHandle(hdc)->DeleteDC(); }
Getting the device context of the printer:
// Get a handle to the printer device context (DC). HDC hdc = dlg.GetPrinterDC(); ASSERT(hdc); // Do something with the HDC... // Clean up. CDC::FromHandle(hdc)->DeleteDC();