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

Using PreSubclassWindow

, 24 Oct 2001
Rate this:
Please Sign up or sign in to vote.
PreSubclassWindow is a very nice handler to use for certain effects in dialogs.

One of the lesser-understood handlers is the PreSubclassWindow handler. It turns out this is a very nice handler to use for certain effects in dialogs.

There are a couple problems in interfacing MFC and dialogs. I have often stated that MFC is a triumph of brute force over common sense. What I mean by this is that the object model of C++ is so completely different from the object model of Windows that the two are almost incompatible. In spite of this, some very clever programming makes the integration almost seamless. But there is a substantial difference between "seamless" and "almost seamless". 

In normal window handling, you will have an OnCreate handler that is invoked just as the window is created. In normal MFC window handling, you have have the PreCreateWindow virtual method which lets you change the CREATESTRUCT values, such as style parameters.

These are not accessible in dialogs. The reason is that the members of your subclass can be invoked only when the window is mapped into MFC, that is, its handler has been changed to be the AfxWnd handler. But dialog controls are created long before the subclassing, which takes place on the first DoDataExchange handler, which happens during the OnInitDialog processing. This is far too late.

I have found that the PreSubclassWindow is an ideal place to make certain modifications, such as changing styles. This applies only to those styles which have an effect after the window has been created (many styles cannot be changed once the window is created. You can change the style bits, but the window itself is oblivious to these changes). This is useful when the styles you want to change are not part of the styles presented by the dialog editor.

Another place I use it is when I want to set a font. For example, in this particular class, I needed to set a font that was 80% of the height of the window. The code is shown below. This creates a font which is a member variable of my class which uses the font that will be set during the OnPaint handler. I want to use the same font as the parent window, but 80% of the size of the current window.

void CMyControl::PreSubclassWindow()
   {
    CRect r;                          
    GetClientRect(&r); 
                                      
    LOGFONT lf;                       
    CFont * f = GetParent()->GetFont();
    f->GetLogFont(&lf);                
    lf.lfHeight = -(int)((double)r.Height() * 0.8);
    font.CreateFontIndirect(&lf);     

    CMySuperclass::PreSubclassWindow();
   } 

There are some things you cannot do in a PreSubclassWindow handler; for example, I found that doing a ShowWindow would cause NT 4.0 SP6 to bluescreen immediately. I have not tried the experiment on Win2K.

However, PreSubclassWindow is a much-neglected method, having little documentation to show its utility. In writing this article, I found precisely one useful reference in the MSDN, to an article Paul DiLascia wrote in the Microsoft System Journal in December, 1999, where he actually says pretty much the same thing. But having written this article, I might as well post it to my Web site.

Several people have pointed out that "You aren't doing anything in PreSubclassWindow that could not be done in OnInitDialog. They are absolutely right. The difference is one of encapsulation. If you force the user of your class (which is often yourself) to do the initialization in OnInitDialog, you must remember what to initialize, even if it is independent of the dialog. And if you change what needs to be done in the initialization, everyone who uses the class (and in every place they use the class), must add or modify the initialization in OnInitDialog. By putting it in PreSubclassWindow, the initialization "follows" the changes you make, and the clients of the class do not need to be concerned about this at all. This is good modularization.

You might also want to check out my essay on self-registering windows, another useful technique to know about if you are doing custom controls and want to put them in a dialog.

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

 
Questionproblem in dynamic creation of the control Pinmemberleeksmcm15-Aug-07 16:36 
AnswerRe: problem in dynamic creation of the control PinmemberJoseph M. Newcomer18-Aug-07 4:49 
Generalrequest for code PinmemberBalkrishna Talele12-Dec-03 0:02 
GeneralRe: request for code PinmemberJoseph M. Newcomer12-Dec-03 2:03 
No. This would be such a fundamentally bad idea I cannot imagine why it could be useful! Instead, tell me what you are trying to accomplish and I'll suggest a sensible solution.
GeneralHandling WM_CREATE, WM_SIZE in custom controls. Pinmemberilinov26-Nov-01 2:08 
GeneralRe: Handling WM_CREATE, WM_SIZE in custom controls. PinmemberJoseph M. Newcomer26-Nov-01 6:21 

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
Web04 | 2.8.141029.1 | Last Updated 25 Oct 2001
Article Copyright 2000 by Joseph M. Newcomer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid