Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / MFC
Article

SAPrefs - Netscape-like Preferences Dialog

Rate me:
Please Sign up or sign in to vote.
5.00/5 (29 votes)
20 Apr 2002CPOL2 min read 619.3K   4.7K   148   142
A base class for a prefereneces dialog, similar to that used in Netscape

Sample Image

CSAPrefsDialog is a base for a preferences dialog, similar to the one used in Netscape. Page selection is handled with a CTreeCtrl - pages can be "sub-pages" of other pages!

Use is very similar to CPropertySheet / CPropertyPage : create a CSAPrefsDialog object. This implements the container for your property pages. The pages are objects of type CPrefsSubDlg (a subclass of CDialog). Using these classes is as easy as this :

// our preferences dialog
CSAPrefsDialog dlg;

// the "pages" (all derived from CSAPrefsSubDlg)
CPage1 page1;
CPage2 page2;
CPage3 page3;
CPage4 page4;

// add 3 pages
dlg.AddPage(page1, "Page 1");
dlg.AddPage(page2, "Page 2");
dlg.AddPage(page3, "Page 3");

// this one will be a child node on the tree
// (&page3 specifies the parent)
dlg.AddPage(dlg4, "Page 4", &page3);

// the prefs dialog title
dlg.SetTitle("This is pretty");

// text drawn on the right side of the shaded
// page label. this does not change when the
// pages change, hence "constant".
dlg.SetConstantText("SAPrefs");

// launch it like any other dialog...
dlg1.m_csText = m_csStupidCString;
if (dlg.DoModal()==IDOK)
{
  m_csStupidCString = dlg1.m_csText;
}

To use this in your own application, you need to follow these steps :

  1. Add the following files to your project :
    • CSAPrefsDialog.cpp,.h
    • CSAPrefsSubDlg.cpp,.h
    • CSAPrefsStatic.cpp,.h
  2. Copy the IDD_SAPREFS dialog resource from the sample project to your project.
  3. Create your preference "pages" in the resource editor with the following settings :
    • Style - Child
    • Border - None
    • No OK or Cancel buttons! (pretend these are CPropertyPages!)
  4. Use Class Wizard to create the dialog classes for the pages.
  5. In the .cpp and .h files for your new dialog classes, replace all occurances of CDialog with CSAPrefsSubDlg. (you will need to #include "SAPrefsSubDlg.h")
  6. Follow the steps shown in the sample code (above) to create the main dialog and add your pages.

Notes

  • The parent/child relationships that you specify with AddPage(page, text, &parent) are strictly cosmetic. Only the tree control knows about them! As far as the CSAPrefsDialog is concerned, all pages are equal and independent of each other.
  • OnOK and OnCancel are virtual functions of CSAPrefsSubDlg. You can override them in your own derived dialogs. But, you will have to do this by-hand (implement them like any other function). Class Wizard will not be able to do this for you. These functions are called, for every page that has been viewed, when CSAPrefsSubDlg is closed by OK or Cancel.
  • You should handle "Help" as with CPropertyPage : with a WM_NOTIFY message handler :
    BOOL CMyPage::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
    {
    	NMHDR* pnmh = (LPNMHDR) lParam;
    	if (pnmh->code == PSN_HELP) {
    		AfxGetApp()->WinHelp(some help ID);
    	}
    
    	return CSAPrefsSubDlg::OnNotify(wParam, lParam, pResult);
    }
  • Your pages should fit inside the IDC_DLG_FRAME rectangle on the IDD_SAPREFS dialog. CSAPrefsSubDlg does not resize to fit your pages, like CPropertySheet does. This keeps things nice and simple. Auto-resizing would be a nice addition, but I don't need it for my purposes, so I didn't add it.

History

  • Jan 27 2002 - updated source files.
  • 21 April 2002 - updated source files.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
Chris Losinger was the president of Smaller Animals Software, Inc. (which no longer exists).

Comments and Discussions

 
GeneralRe: Your Code is TERRIBLE! Pin
Bill Leibold1-Mar-02 8:47
Bill Leibold1-Mar-02 8:47 
GeneralRe: Damn, and I thought I was bad Pin
Tim Smith1-Mar-02 6:48
Tim Smith1-Mar-02 6:48 
GeneralRe: Damn, and I thought I was bad Pin
Chris Losinger1-Mar-02 7:05
professionalChris Losinger1-Mar-02 7:05 
GeneralRe: Damn, and I thought I was bad Pin
Tim Smith1-Mar-02 7:13
Tim Smith1-Mar-02 7:13 
GeneralRe: Damn, and I thought I was bad Pin
Nish Nishant1-Mar-02 7:30
sitebuilderNish Nishant1-Mar-02 7:30 
GeneralRe: Damn, and I thought I was bad Pin
Tim Smith1-Mar-02 7:41
Tim Smith1-Mar-02 7:41 
GeneralRe: Damn, and I thought I was bad Pin
Christian Graus1-Mar-02 9:34
protectorChristian Graus1-Mar-02 9:34 
GeneralRe: Your Code is TERRIBLE! Pin
Bill Leibold1-Mar-02 8:42
Bill Leibold1-Mar-02 8:42 
GeneralRe: Your Code is TERRIBLE! Pin
PJ Arends1-Mar-02 8:57
professionalPJ Arends1-Mar-02 8:57 
GeneralRe: Your Code is TERRIBLE! Pin
Bill Leibold1-Mar-02 9:04
Bill Leibold1-Mar-02 9:04 
Questiondynamically add a tree node on execute time? Pin
hunglin26-Feb-02 14:12
hunglin26-Feb-02 14:12 
AnswerRe: dynamically add a tree node on execute time? Pin
Chris Losinger27-Feb-02 9:51
professionalChris Losinger27-Feb-02 9:51 
AnswerRe: dynamically add a tree node on execute time? Pin
Carlos Antollini27-Feb-02 10:17
Carlos Antollini27-Feb-02 10:17 
AnswerRe: dynamically add a tree node on execute time? Pin
28-Feb-02 20:26
suss28-Feb-02 20:26 
AnswerRe: dynamically add a tree node on execute time? Pin
csavie29-Sep-03 1:29
csavie29-Sep-03 1:29 
QuestionHow to add a font dialog :( Pin
21-Jan-02 8:02
suss21-Jan-02 8:02 
QuestionModeless Dialog? Pin
leo12-Dec-01 10:19
leo12-Dec-01 10:19 
AnswerRe: Modeless Dialog? Pin
Chris Losinger12-Dec-01 10:40
professionalChris Losinger12-Dec-01 10:40 
Questiondynamically update page name in tree view? Pin
Philip Chang28-Sep-01 10:29
Philip Chang28-Sep-01 10:29 
AnswerRe: dynamically update page name in tree view? Pin
Philip Chang3-Oct-01 6:26
Philip Chang3-Oct-01 6:26 
GeneralCSAPrefsSubDlg as a tabbed control Pin
Kannan Kalyanaraman17-Sep-01 19:40
Kannan Kalyanaraman17-Sep-01 19:40 
GeneralInside a Wizard Pin
Kannan Kalyanaraman4-Sep-01 4:35
Kannan Kalyanaraman4-Sep-01 4:35 
GeneralRe: Inside a Wizard Pin
Chris Losinger4-Sep-01 4:38
professionalChris Losinger4-Sep-01 4:38 
Generalexcellent job! Pin
28-Jun-01 14:14
suss28-Jun-01 14:14 
Generalnice work !!! Pin
Noman Nadeem25-Jun-01 18:44
Noman Nadeem25-Jun-01 18:44 

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.