Home | API | MFC | C++ | C | Previous | Next

Programming Windows with MFC

Common Dialog Window

Common dialogs are a set of predefined, standard dialogs that can be used in an application to carry out common tasks such as file selection, font selection, or color selection. All the common dialog classes are derived from a common base class, CCommonDialog.

The behavior and appearance of a common dialogue by set or altered changing the parameters supplied to the dialog’s constructor or by setting various flags. All the common dialogs require the inclusion of the commdlg.h header file.

MFC CCommonDialog Classes are shown in the following table.

Class Purpose
CColorDialog Allows the user to select or create a color
CFileDialog Allows the user to open or save a file
CFindReplaceDialog Allows the user to substitute one string for another
CFontDialog Allows the user to select a font from a list of available fonts
COleDialog Useful for inserting OLE objects
CPageSetupDialog Allows the user to set page measurement parameters
CPrintDialog Allows the user to set up the printer and print a document
CPrintDialogEx Printing and Print Preview for Windows 2000

For further details reading
https://docs.microsoft.com/en-us/cpp/mfc/reference/ccommondialog-class?view=vs-2019

The following short program illustrates the use of the CColourDialog by changing the background colour of the windows to match that selected from the colour dialog box


#include <afxdlgs.h>
#define BUTTON 1000

class CSimpleApp : public CWinApp
{
public:
BOOL InitInstance();
};

class CMainFrame : public CFrameWnd
{
public:
void refreshwindows(WPARAM, LPARAM );
CMainFrame();
afx_msg void modelessButtonClick();
afx_msg void modalButtonClick();
DECLARE_MESSAGE_MAP()
CButton Button;
afx_msg void OnPaint();
COLORREF wcolor;
};


BOOL CSimpleApp::InitInstance(){
m_pMainWnd = new CMainFrame();
m_pMainWnd->ShowWindow(m_nCmdShow);
return true;
}


CMainFrame::CMainFrame()
{
Create(NULL, "MFC Colour dialog",WS_OVERLAPPEDWINDOW ,CRect(25,25,410,200));
Button.Create ("Colour dialog",BS_DEFPUSHBUTTON| WS_CHILD | WS_VISIBLE , CRect(12,12,150,62), this,BUTTON);
wcolor=RGB(255,255,255);
}

BEGIN_MESSAGE_MAP(CMainFrame,CFrameWnd)
ON_BN_CLICKED(BUTTON,modelessButtonClick)
ON_WM_PAINT()
END_MESSAGE_MAP()


CSimpleApp MFCApp1;

//selects modeless dialog when button clicked
afx_msg void CMainFrame::modelessButtonClick()
{
CColorDialog Tmp( 0, CC_ANYCOLOR | CC_RGBINIT, this );
if( Tmp.DoModal() == IDOK )
{
CString Msg;
Msg.Format( "You selected color %d", Tmp.GetColor() );
wcolor = Tmp.GetColor();
Invalidate();
}
}

afx_msg void CMainFrame::OnPaint()
{
CPaintDC paintDC(this);
CRect rect;
GetClientRect(&rect);
paintDC.FillSolidRect(&rect,wcolor);
}

Download Code


Home | API | MFC | C++ | C | Previous | Next
Creating a Simple Window | Processing Messages | Device Context | Working with Graphics | Mapping Modes | Text Output | Working with the Mouse | Dealing with Keyboard Input | Drawing Lines and Shapes | Adding Menus | Child Windows | Dialog Windows | Common Dialog Box | Working with Bitmaps | Common Controls | Toolbars | Document View Architecture | Multi Document Interface | Timers | MFC Collections Classes

Last Updated: 16 October 2022