15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Adding Controls to Property Sheets at Run Time

Nick Gall -- nick_gall@usemail.com
Monday, February 05, 1996

[I am using VC++ 4.0 on Windows 95.]

I want to add some buttons to a modeless CPropertySheet. Remarkably, I
found a Knowledge Base article on just this subject. (Note that there
is a paragraph or two on this subject in Books Online, but it is WRONG!
-- It says to override OnCreate, which is too early in the process.) I
followed the KB article's instructions (which I have included below)
and got my extra buttons, BUT the font used for the added buttons is
not the same as the font used in the rest of the property sheet. After
a long search of code and documentation, I discovered that controls
added at run time to a dialog do not use the font specified in the
dialog template resource (MS Sans Serif 8 pt); instead, they use the
system font, which at the time of the call to Create, is bold and
slightly larger.

The fix I came up with is to add the following code after the call to
m_Button.Create:

     m_Button.SetFont(GetFont());

This gets the font of CMySheet and sets the button's font to it.

I find it difficult to believe that this is the best way to achieve my
goal of adding buttons with a matching font at run time.

Questions:

(1) Is the KB article in error for not including code to match the
button's font to the CPropertySheet's font or is there something
different about my application that causes a font mismatch requiring
correction? Note that I have tried the absolutely simplest case: a
clean standard MDI App in which I simply added code in OnInitDialog to
add a CStatic control with the text "Test" to the About Dialog at run
time. Guess what, the font did not match. So I am fairly certain that
the KB article is in error (or at least incomplete).

(2) Is there a better way to ensure that controls with text (e.g.,
buttons, static text) added to a dialog or property sheet at run time
use a matching font?

(3) Where is this behavior documented? All I could find was a somewhat
obscure Knowledge Base article entitled "SAMPLE: PRPFONT - How to Set
CPropertySheet Fonts" (ARTICLE ID: Q142170), which is on the Web
Knowledge Base, but not on the VC++ CD.  Unfortunately, I cannot find
the sample program it discusses PRPFONT.EXE anywhere, even though the
article claims it is in the various MS Software Libraries.

(4) Where is PRPFONT.EXE?

(5) Why is the System Font bold and larger than 8 pts at the time
m_button.Create is called? Is this normal? Note, I discovered it is the
same when I create Static Text in the About dialog box at run time.

Thanks.

-- Nick

----------
How to Add Buttons to a Modeless CPropertySheet

ARTICLE ID: Q140586



John Moulder -- jm@wg.icl.co.uk
Wednesday, February 07, 1996

[Mini-digest: 2 responses]

At 13:16 05/02/96 -0500, you wrote:
[lines deleted]
>(3) Where is this behavior documented? All I could find was a somewhat
>obscure Knowledge Base article entitled "SAMPLE: PRPFONT - How to Set
>CPropertySheet Fonts" (ARTICLE ID: Q142170), which is on the Web
>Knowledge Base, but not on the VC++ CD.  Unfortunately, I cannot find
>the sample program it discusses PRPFONT.EXE anywhere, even though the
>article claims it is in the various MS Software Libraries.
>
>(4) Where is PRPFONT.EXE?
ftp://ftp.microsoft.com/Softlib/MSLFILES/PRPFONT.EXE

I make no guarantees about UPPER or lower case in the URL. The file was
there yesterday (February 6th).

John

-----From: Brad Wilson 

At 01:16 PM 2/5/96 -0500, you wrote:
> The fix I came up with is to add the following code after the call to
> m_Button.Create:
>
>     m_Button.SetFont(GetFont());
>
>
> This gets the font of CMySheet and sets the button's font to it.
>
> I find it difficult to believe that this is the best way to achieve my
> goal of adding buttons with a matching font at run time.

Actually, that's the best way.

> (1) Is the KB article in error for not including code to match the
> button's font to the CPropertySheet's font or is there something
> different about my application that causes a font mismatch requiring
> correction?

The KB article does not mention fonts, so I'm unsure if I would use the
word "wrong".  When dialogs are created from their templates, one of
the items in the template is the font.  When Windows creates the dialog
and the controls, it sets the font for all the controls (as well as using
some spacing information from the font to determine sizes).

When you add controls after the fact, you don't get the benefit of Windows
doing all the work for you.  Luckily, it's as easy as the one line of
code you have above.

IMO, the KB should have documented the font issue.

> (2) Is there a better way to ensure that controls with text (e.g.,
> buttons, static text) added to a dialog or property sheet at run time
> use a matching font?

No.

> (5) Why is the System Font bold and larger than 8 pts at the time
> m_button.Create is called? Is this normal? Note, I discovered it is the
> same when I create Static Text in the About dialog box at run time.

Yes.  The system font is always the system font.  The default font for
a window is the system font (try calling DrawText() in an OnPaint() handler
without first setting a font; the text will be drawn in the font that
you see in your dialog controls).

This is normal behavior and has been the "standard" way of doing things
ever since the beginning of time.  :-)

Brad

--
class CBradWilson : public CWorldWatchProgrammingTeam {
  public:
    void GetInetAddr  ( CString& s ) { s = "bradw@exptech.com";      }
    void GetE164Addr  ( CString& s ) { s = "+1 (810) 620-9803";      }
    void GetURL       ( CString& s ) { s = "http://www.exptech.com"; }
    void GetDisclaimer( CString& s ) { s = "All I say is fact :-p";  }
};

//  LOTW:  "In my den of inequity, viciousness and subtelty, struggle to ease
//          the pain, struggle to find the sane; ignorance surrounding me"




| Вернуться в корень Архива |