Click here to Skip to main content
Click here to Skip to main content

Subclassing Dialogs and Property Pages

, 5 Apr 2001
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Joseph M. Newcomer

United States United States
No Biography provided

Comments and Discussions

 
GeneralHi to all !! Pinmembervidya.p.nair30-Jul-07 23:01 
GeneralRe: Hi to all !! PinmemberJoseph M. Newcomer31-Jul-07 13:09 
GeneralThanks Pinmembersfdougl13-Jun-05 6:17 
QuestionHow about CFormViews? Pinmemberinfidel7924-Jul-03 5:19 
Questionhow add icon to the tab in the property page Pinmembersiong22-Jul-03 17:27 
AnswerRe: how add icon to the tab in the property page PinmemberJoseph M. Newcomer26-Jul-03 7:29 
QuestionHow to use the Wizard Dialog in VC? Pinmemberwangnanjing21-May-03 20:08 
AnswerRe: How to use the Wizard Dialog in VC? PinmemberJoseph M. Newcomer22-May-03 4:03 
GeneralProperty pages in VC++ Pinmembersheethal10-Jun-02 1:51 
GeneralRe: Property pages in VC++ Pinmember*RPM16-Apr-03 3:23 
GeneralProblem with Proprerty pages PinmemberAnonymous14-Mar-02 18:50 
GeneralRe: Problem with Proprerty pages PinmemberJoseph M. Newcomer14-Mar-02 19:45 
GeneralMistake in header Pinmemberdramon5-Apr-01 22:02 
GeneralRe: Mistake in header PinmemberSean Cundiff9-Apr-01 7:41 
Actually:
 
CMyDialog(UINT id, CWnd * parent = NULL); Big Grin | :-D
 
in the .h file.

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 6 Apr 2001
Article Copyright 2001 by Joseph M. Newcomer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid