Click here to Skip to main content
15,879,095 members
Articles / Desktop Programming / MFC
Article

Subclassing Dialogs and Property Pages

Rate me:
Please Sign up or sign in to vote.
4.44/5 (6 votes)
5 Apr 20013 min read 123.9K   23   14
You have a lot of dialogs that have a lot of properties in common. You'd like to derive the specialized ones from the general ones. Here's how.

Introduction

One of the limits of the ClassWizard is its inability to handle various useful forms of subclassing. In particular, it does not intrinsically allow you to subclass from a subclass, or specify a subclassed subclass as a variable. For example, I have a class called CSmartDropdown which does dynamic resizing of combo boxes. I have a subclass CIDCombo which is a subclass of CSmartDropdown which handles the issues of combo box initialization in a better fashion than the resources.

The two classes CIDCombo and CSmartDropdown were merged in the CIDCombo class I publish on my Web site.

In fact, I often work with a CGraphicsCombo class which is a subclass of CIDCombo, but supports owner-draw bitmap images, or CColorCombo which is a subclass of CGraphicsCombo that displays some selected colors. So I often work several levels of derivation down, and the ClassWizard makes this inconvenient.

But if you want to subclass a dialog or property page, it gets even worse. Consider the case where you have a combo box which appears on every property page, or on every dialog, and a few buttons that have the same behavior. It seems silly to create a set of independent dialogs that handle this, yet there is no support in ClassWizard that helps you.

Fortunately, it is not hard to change this. You just have to know the Magic Incantations. Here they be!

First, create a dialog with the common controls that are shared by all the subclasses you plan to create. This dialog will not actually be used (although it could be, I'm not that ambitious here...), but it is used to create all the handlers. You will find in the .h file a declaration of the constructor:

CMyDialog(CWnd * parent = NULL);

Change this to read

CMyDialog(CWnd * parent = NULL) { ASSERT(FALSE); } // never create!

Add a new constructor:

CMyDialog(CWnd * parent = NULL) { ASSERT(FALSE); } // never create!

Go into the .cpp file and modify the constructor code. It reads

CMyDialog::CMyDialog(CWnd * parent /* = NULL */)
        : CDialog(CMyDialog::IDD, parent)
    {
     ...
    }

Change this to read

CMyDialog::CMyDialog(UINT id, CWnd * parent /* = NULL */)
        : CDialog(id, parent)
    {
     ...
    } 

Now when you create a new dialog which is a subclass, just create the dialog as you normally would. Now comes the tedious part that ClassWizard should have done for you (send email to the Microsoft Wish site if you think ClassWizard should support this!)

Go into the .h file for your new class and change all instances of CDialog to CMyDialog

Go into the .cpp file for your new class and change all instances of CDialog to CMyDialog

That's it. You're done. When you create a subclassed dialog, you still have to copy the common controls from your master dialog template into the child dialogs (note that Microsoft could have made this easy, by simply inheriting the controls during dialog construction; a subclassed dialog would open the resource of each enclosing parent and include all the controls from that class into the current dialog, which is fairly trivial to implement). 

You can do exactly the same technique to subclass a CPropertyPage-derived class.


The views expressed in these essays are those of the author, and in no way represent, nor are they endorsed by, Microsoft.

Send mail to newcomer@flounder.com with questions or comments about this article.
Copyright © 1999 All Rights Reserved
www.flounder.com/mvp_tips.htm

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
Retired
United States United States
PhD, Computer Science, Carnegie Mellon University, 1975
Certificate in Forensic Science and the Law, Duquesne University, 2008

Co-Author, [i]Win32 Programming[/i]

Comments and Discussions

 
GeneralHi to all !! Pin
vidya.p.nair30-Jul-07 23:01
vidya.p.nair30-Jul-07 23:01 
GeneralRe: Hi to all !! Pin
Joseph M. Newcomer31-Jul-07 13:09
Joseph M. Newcomer31-Jul-07 13:09 
GeneralThanks Pin
S Douglas13-Jun-05 6:17
professionalS Douglas13-Jun-05 6:17 
QuestionHow about CFormViews? Pin
infidel7924-Jul-03 5:19
infidel7924-Jul-03 5:19 
Questionhow add icon to the tab in the property page Pin
siong22-Jul-03 17:27
siong22-Jul-03 17:27 
AnswerRe: how add icon to the tab in the property page Pin
Joseph M. Newcomer26-Jul-03 7:29
Joseph M. Newcomer26-Jul-03 7:29 
QuestionHow to use the Wizard Dialog in VC? Pin
wangnanjing21-May-03 20:08
wangnanjing21-May-03 20:08 
AnswerRe: How to use the Wizard Dialog in VC? Pin
Joseph M. Newcomer22-May-03 4:03
Joseph M. Newcomer22-May-03 4:03 
GeneralProperty pages in VC++ Pin
10-Jun-02 1:51
suss10-Jun-02 1:51 
GeneralRe: Property pages in VC++ Pin
*RPM16-Apr-03 3:23
*RPM16-Apr-03 3:23 
GeneralProblem with Proprerty pages Pin
14-Mar-02 18:50
suss14-Mar-02 18:50 
GeneralRe: Problem with Proprerty pages Pin
Joseph M. Newcomer14-Mar-02 19:45
Joseph M. Newcomer14-Mar-02 19:45 
GeneralMistake in header Pin
5-Apr-01 22:02
suss5-Apr-01 22:02 
GeneralRe: Mistake in header Pin
Sean Cundiff9-Apr-01 7:41
Sean Cundiff9-Apr-01 7:41 

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.