Re: Default font in dialog property pages

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 02/07/05


Date: Mon, 07 Feb 2005 18:52:34 -0500

Static text controls are the ones I most often change the font of; for example, to get
nice-looking headings (bold, large, centered types of headings).

Each control IS a separate window, by definition. All are subclasses of CWnd, implicitly,
So nothing special here. My own choice is to create a subclass of CStatic. Sometimes I
build the font knowledge into it directly, e.g., use ClassWizard to create a new class
which is a subclass of CStatic, such as

class CMyBigHeader : public CStatic {
       protected:
            CFont font;
};

then I just add the code to PreSubclassWindow to create the font and set it. All this
requires is assigning an ID other that IDC_STATIC to the control and creating a control
variable for it, no big deal.

I would have assumed that setting the font in the dialog editor would have done the job,
except for the observation that this apparently doesn't work for property pages. Since I
usually need to change the font only for one or two controls, as I said, I never bothered
to change the overall font.

By the way, what I usually do is create a font the height of the static control, so it
automatically adjusts for the scaling applied in various resolutions. Makes it a lot
safer. So for example, in the PreSubclassWindow handler I would do

        CRect r;
        GetClientRect(&r);
        LOGFONT lf = {0};
        lf.lfHeight = r.Height();
        lf.lfWeight = FW_BOLD;
        lstrcpy(lf.lfFaceName, _T("Arial"));
        VERIFY(font.CreateFontIndirect(&lf));
        SetFont(&font);

sometimes I will subtract off a height such as an integer multiple of
::GetSystemMetrics(SM_CXBORDER) if I want it a bit smaller. This is usually the most
reliable method. You should not pick a fixed font height such as "10" because this will
invariably screw up on some high-resolution display (e.g., my new laptop does 1280x1024,
and it is amazing how many programs look really ugly because the programmers did not
program the dialogs as resolution-independent!)
                                        joe

On Mon, 7 Feb 2005 13:21:01 -0800, Andrew K. <AndrewK@discussions.microsoft.com> wrote:

>Hi Joe, thank you for finding time to look into this.
>
>I just have a number of the Static Text controls in the welcoming page of
>the Property*** based wizard. And all I need is having them printed in
>something bigger than MS Sans Serif 8. Nothing relatively complex like
>dynamic controls changing at run-time and so on...
>Your response suggests me that each dialog control, even including static
>text ones, has to be treated as a separate window, or, in terms of MFC, a
>derivative of the CWnd class. Is my interpretation correct ?
>Because so far, my understanding was that use of the resource editor to
>create the property page template with description of the required
>attributes, including font type and size, is sufficient, and I would not
>actually have to use CFont and SetFont() for this purpose.
>
>Thanks a lot,
>Andrew
>
>
>
>"Joseph M. Newcomer" wrote:
>
>> You have not clarified what you have tried or what the effect is.
>>
>> For example, if I want to change the font on a single control, I will create a CFont in
>> the control, or in the dialog, and use SetFont to set the font in the control, or for each
>> control of the dialog. This works; at least I've never seen it fail. Generally I need to
>> change the font only in a single control, so I've never tried to override the font has you
>> have done (and everything I know suggests that what you did should have worked, but I was
>> not aware of the problem Jeff posted). But given that I can override the font in a single
>> control, a simple for(CWnd * wnd = GetWindow(GW_CHILD); wnd != NULL; wnd =
>> GetWindow(GW_HWNDNEXT) type loop should allow you to set, individually, the font in every
>> window. Create it in a CFont member variable of the dialog in OnInitDialog. Not elegant,
>> but it should work.
>> joe
>>
>> Joseph M. Newcomer [MVP]
>> email: newcomer@flounder.com
>> Web: http://www.flounder.com
>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>>

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Loading