Click here to Skip to main content
15,846,343 members
Articles / Desktop Programming / MFC

Using PreSubclassWindow

Rate me:
Please Sign up or sign in to vote.
4.95/5 (12 votes)
24 Oct 2001 97K   38   6
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;                          
    LOGFONT lf;                       
    CFont * f = GetParent()->GetFont();
    lf.lfHeight = -(int)((double)r.Height() * 0.8);


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.


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
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

Questionproblem in dynamic creation of the control Pin
leeksmcm15-Aug-07 17:36
leeksmcm15-Aug-07 17:36 
AnswerRe: problem in dynamic creation of the control Pin
Joseph M. Newcomer18-Aug-07 5:49
Joseph M. Newcomer18-Aug-07 5:49 
I have never seen this fail. Note that you must do this *after* the call to the parent OnInitDialog call. YOu have not shown enough code to allow me to diagnose the problem.
Generalrequest for code Pin
Balkrishna Talele12-Dec-03 1:02
Balkrishna Talele12-Dec-03 1:02 
GeneralRe: request for code Pin
Joseph M. Newcomer12-Dec-03 3:03
Joseph M. Newcomer12-Dec-03 3:03 
GeneralHandling WM_CREATE, WM_SIZE in custom controls. Pin
ilinov26-Nov-01 3:08
ilinov26-Nov-01 3:08 
GeneralRe: Handling WM_CREATE, WM_SIZE in custom controls. Pin
Joseph M. Newcomer26-Nov-01 7:21
Joseph M. Newcomer26-Nov-01 7:21 

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.