Click here to Skip to main content
16,016,775 members
Articles / Desktop Programming / MFC
Article

CSettingsDialog, a Dialog Customizing Your Project Settings and More

Rate me:
Please Sign up or sign in to vote.
4.86/5 (16 votes)
11 Mar 20023 min read 305.7K   6K   114   59
A Netscape Preferences-Like Dialog

Image 1
PropertyPage Image

FormView Image
Generic View Image

Introduction

This article extends Chris Losinger's work on CSAPrefsDlg.

CSettingsDialog is an MFC class which enables to customize the project settings. The settings of a project are categorized as pages based on their nature of those pages. If you are familiar with the Netscape preferences-dialog, you will have no problem understanding the interfaces of CSettingsDialog

Unlike those similar dialogs posted in CodeProject and CodeGuru, which only allow one type of Window (e.g.,CPropertyPage or CDialog), CSettingsDialog allows any CWnd derived windows to be used as setting pages (Look at the demo figures above, a property page, a form view and a generic CWnd Windows are used for different settings.I believe that this design extend the applicability of the class to meet the need of various situations. Furthermore, each tree node does not have to have a setting page to be associated, it can be a generic tree node, e.g., a label used as a category index.

New features are added to this newest version. CHtmlView is supported (take a look at the first demo picture). However, in order to use the CHtmlView (or derived) class, you need to override the OnMouseActive virtual function to avoid the ASSERTion error in your CHtmlView (or derived) class. Another new feature of CSettingsDialog is that it support both modal and modaless state of display. There are some rules need to be followed. For the modeless dialog, the dialog variable must be declared as a pointer and be instantiated using new operator. You need to create the dialog using member function CSettingsDialog::Create() and using ShowWindow(SW_SHOW) to diaplay the dialog. The Apply button in the dialog is disabled in the modal state and is enabled in the modeless state.

How to use CSettingsDialog

Use of the class is pretty simple. Just follow the following steps:
  • Add the following files to your project :
    • SettingsDialog.cpp, SettingsDialog.h
    • CSAPrefsStatic.cpp,.h


  • Copy the IDD_SETTINGS_DLG dialog resource from the sample project to your project.

  • Create your Settings "pages" in the resource editor. If the page is a dialog, make sure that the dialog has the following settings:
    • Style - Child
    • Border - None
    • No OK or Cancel buttons!
    Use Class Wizard to create the dialog classes for the pages.

  • If used as a modal dialog: Create and initialize the CSettingsDialog as demonstrated in the demo project: CSettingsDialog dlg;
    CSettingsDialog dlg;
    
    dlg.AddPage(RUNTIME_CLASS(CMyHtmlView), _T("Project Setting"), 0);
    CPropPage1 *pModelPage = (CPropPage1*) dlg.AddPage(RUNTIME_CLASS(CPropPage1),
                                 _T("Model"), IDD_PROPERTY_PAGE1, 
                                 _T("Project Setting"));
    dlg.AddPage(RUNTIME_CLASS(CPropPage2), _T("Visibility"), 
                IDD_PROPERTY_PAGE2, pModelPage);
    dlg.AddPage(RUNTIME_CLASS(CMyFormView), 
                _T("Form View"), IDD_FORMVIEW, pModelPage);
    
    dlg.AddPage(RUNTIME_CLASS(CMyView), _T("Generic View Page"), 0);
    dlg.AddPage(NULL, _T("Generic Tree Item"), 0);
    
    dlg.SetTitle("Project Settings");
    dlg.SetLogoText("CSettingsDialog 1.0");    
    int nResponse = dlg.DoModal();
    
    if (nResponse == IDOK)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with Cancel
    }
  • If used as a modaless dialog:
    • Declare a CSettingsDialog variable as a pointer in your parent window class, eg.,m_pDlg in CMainFrame.
    • Instantiate an object of CSettingsDialog:
      CSettingsDialog *m_pDlg = new CSettingsDialog(this) ; 
    • create, initialize, and display the settings pages and dialog, e.g.,
      if (!m_pDlg) 
      {
          m_pDlg = new CSettingsDialog(this);    
          m_pDlg->AddPage(RUNTIME_CLASS(CMyHtmlView), 
                              _T("Project Setting"), 0);
          CPropPage1 *pModelPage 
                    = (CPropPage1*) m_pDlg->AddPage(RUNTIME_CLASS(CPropPage1),
                             _T("Model (PropertyPage)"), IDD_PROPERTY_PAGE1,  
                             _T("Project Setting"));
          m_pDlg->AddPage(RUNTIME_CLASS(CPropPage2), 
                             _T("Visibility (PropertyPage)"), IDD_PROPERTY_PAGE2, 
                             pModelPage);
          m_pDlg->AddPage(RUNTIME_CLASS(CMyFormView), _T("Form View"), 
                             IDD_FORMVIEW, pModelPage);
      
          m_pDlg->AddPage(RUNTIME_CLASS(CMyView), _T("Generic View Page"), 0);
          m_pDlg->AddPage(NULL, _T("Generic Tree Item"), 0);
      
          m_pDlg->SetTitle("Project Settings");
          m_pDlg->SetLogoText("CSettingsDialog 1.0");    
      
          m_pDlg->Create();
      }
      m_pDlg->ShowWindow(SW_SHOW);
    • Add a message handler of CSettingsDialog in your parent window which "owns" the dialog.
    • Finally do not forget to free the dialog memory in the parent window's destructor.
    • If you are not sure exactly how to do it, take a look at the attached demo project,

Acknowledgments

History

  • V1.0 02/12/02 First version of the class
  • V1.1 03/01/02
    • Memory leak bug fixed.
    • Thick frame style of CView type class (including CFormView) is removed to make it look more industrial standard.
    • Html view supported.
  • V1.2 03/08/02 Both modal and modaless states supported. The demo project shows the CSettingsDialog in both modal and modaless way.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Yellowine is a fan of MFC. He has been programming for several years.

Comments and Discussions

 
NewsNo recommended class. Pin
Kim Moung Soo25-Jan-16 21:16
Kim Moung Soo25-Jan-16 21:16 
QuestionCrash when use nonmodal window Pin
_Flaviu22-May-12 1:51
_Flaviu22-May-12 1:51 
Questionadding scrollbars..? Pin
mimosa5-Jan-10 10:49
mimosa5-Jan-10 10:49 
Questionwhere is the newest source? Pin
xinyuediandian27-Jul-08 18:00
xinyuediandian27-Jul-08 18:00 
QuestionAnd if a CDialog Derived class is to be added? Pin
Divya Rathore3-Feb-07 6:48
Divya Rathore3-Feb-07 6:48 
GeneralHelp me, please ! MDI child frame inteads of CSettingsDialog Pin
Nhiem17-Apr-04 18:49
Nhiem17-Apr-04 18:49 
Generalnewest source Pin
one_eddie29-Feb-04 2:53
one_eddie29-Feb-04 2:53 
GeneralAccessing data members Pin
Atlence22-Nov-03 4:17
Atlence22-Nov-03 4:17 
GeneralRe: Accessing data members Pin
Axel Bittkau28-Jan-04 21:59
Axel Bittkau28-Jan-04 21:59 
GeneralRe: Accessing data members Pin
cristitomi20-Mar-07 22:57
cristitomi20-Mar-07 22:57 
GeneralI am getting lots of memory leaks Pin
JWood18-Sep-03 17:10
JWood18-Sep-03 17:10 
GeneralRe: I am getting lots of memory leaks Pin
lxwde6-Nov-03 16:19
lxwde6-Nov-03 16:19 
the following code dose work in my project,please try it


BOOL CSettingsDialog::DestroyPages()
{
for (int i=0; i<m_pInfo.GetSize(); i++)
{
PAGE_INFO *pInfo = (PAGE_INFO *)m_pInfo.GetAt(i);
if (pInfo && pInfo->pWnd)
{
if (::IsWindow(pInfo->pWnd->m_hWnd))
{
pInfo->pWnd->DestroyWindow();
if (!(pInfo->bViewClass))
{
delete(pInfo->pWnd);
pInfo->pWnd=NULL;
}

}
else
{
delete(pInfo->pWnd);
pInfo->pWnd=NULL;
}
}
delete pInfo;//delete
pInfo=NULL;
}

m_pInfo.RemoveAll();
return(true);
}

GeneralRe: I am getting lots of memory leaks Pin
Kim Moung Soo24-Jan-16 18:00
Kim Moung Soo24-Jan-16 18:00 
Generalnon standard behavior of property page message Pin
tyounsi12-Jun-03 0:53
tyounsi12-Jun-03 0:53 
QuestionVersion 1.3?? Pin
Stefan Dahlin2-May-03 11:28
Stefan Dahlin2-May-03 11:28 
AnswerRe: Version 1.3?? Pin
one_eddie29-Feb-04 9:40
one_eddie29-Feb-04 9:40 
GeneralMemory leaks problem solution Pin
Jaime Stuardo21-Apr-03 15:07
Jaime Stuardo21-Apr-03 15:07 
GeneralCould not download v1.1 or v1.2 Pin
icnocop22-Oct-02 13:24
icnocop22-Oct-02 13:24 
GeneralRe: Could not download v1.1 or v1.2 Pin
Brian van der Beek17-Dec-02 11:01
Brian van der Beek17-Dec-02 11:01 
GeneralProblem with Controls Pin
avtar7-Sep-02 0:02
avtar7-Sep-02 0:02 
GeneralCHtmlView and Swing Pin
Sky17-Jul-02 22:41
Sky17-Jul-02 22:41 
GeneralPlease post source code here!!!! Pin
4-Jul-02 6:15
suss4-Jul-02 6:15 
GeneralRe: Please post source code here!!!! Pin
flector5-Jul-02 10:35
flector5-Jul-02 10:35 
GeneralRe: Please post source code here!!!! Pin
netseeker16-Aug-02 7:10
netseeker16-Aug-02 7:10 
GeneralIt can't compile pass in Visual Studio.Net Pin
25-Mar-02 19:51
suss25-Mar-02 19:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.