Re: CFont frustration

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Johan Rosengren (johan.rosengren_at_telia.com)
Date: 02/28/04


Date: Sat, 28 Feb 2004 20:53:03 +0100

Erik,

Your problem should not be with the CFont*, I would ratehr guess that you
are destroying your bold font too early.

First of all, if you want the font from dialogs, you could get it as a stock
object. You don't need any control for this, and might thus create the bold
font in the ctor. Then, you will want to destroy the same font in the dtor.
You can just forget about the font already existing for the control - you
don't need to reset it. Thus, we get:

CMyStatic::CMyStatic()
{
 LOGFONT lf;
 // Get the logfont for the default dialog font
 m_fntOriginal.CreateStockObject(DEFAULT_GUI_FONT);
 m_fntOriginal.GetLogFont(&lf);

 // Get rid of the stock object. We don't need to delete it
 m_fntOriginal.Detach();

 // Modify the logfont and create our CFont
 lf.lfWeight=FW_BOLD;
 m_fntOriginal.CreateFontIndirect(&lf);
}

CMyStatic::~CMyStatic()
{
 // Get rid of it, now, it's
 // definitely not selected anywhere
 m_fntOriginal.DeleteObject();
}

void CMyStatic::PreSubclassWindow()
{
 CStatic::PreSubclassWindow();

 // Set our font
 SetFont(&m_fntOriginal,FALSE);
}

Voilà. Now, the font is most certainly not selected when we delete it, and
the stock object we based it on needs not be deleted at all.

Johan Rosengren
Abstrakt Mekanik AB

"Erik J Sawyer" <anonymous@discussions.microsoft.com> a écrit dans le
message de news:8C5B9D9A-DBF3-450B-9556-EEFC7A695800@microsoft.com...
> I'm trying to create a static text field on a dialog, with a bold font. I
want to use the system/control assigned font, to allow flexibility at
runtime. Therefore, what I am trying to do is A) retrieve the pre-selected
font, B) use the LOGFONT to create an equivilent font that's bold, and C)
pass the new font to the control.
>
> All this works, but VC6 keeps telling me it's finding memory leaks and/or
access faults. These appear to be related to the (supposedly)
garbage-collected CFont* returned by GetFont(). Where am I going wrong?
>
> /* <codesnippet> */
> void CMyStatic::PreSubclassWindow()
> {
> // Get the current window font.
> CFont* pFont = GetFont();
>
> ASSERT_VALID(pFont);
>
> if (pFont) {
> m_fntOriginal.Attach(pFont->m_hObject);
>
> LOGFONT lf;
> pFont->GetLogFont(&lf);
> lf.lfWeight = FW_BOLD;
>
> if (m_fntBold.CreateFontIndirect(&lf))
> {
> CStatic::SetFont(&m_fntBold);
> }
> // tried the following, didn't make any difference
> // pFont->Detach();
> // pFont->m_hObject = NULL;
> }
>
> CStatic::PreSubclassWindow();
> }
>
> void CMyStatic::OnDestroy()
> {
> CStatic::SetFont(&m_fntOriginal);
> m_fntBold.DeleteObject();
> m_fntBold.m_hObject = NULL;
> CStatic::OnDestroy();
> }
>
> /* </codesnippet> */
>
> Thanks,
> Erik J Sawyer
> CFT Programmer
> Appro Systems



Relevant Pages

  • Re: How to change to BOLD in CEdit
    ... the font seems to change to the system font which ... I've tried to put CFONT font in the header file (as per one of the ... LOGFONT logfont; ... "MS Shell Dlg" ...
    (microsoft.public.vc.mfc)
  • Change System Font Properties in Applications?
    ... OE displays subscribed newsgroups and and message ... subjects using bold font properties that I would like to change. ... change fonts via the Tools menu -- but only for the text in the body of ... I am using the Silver Display Appearance color scheme and font size ...
    (microsoft.public.windowsxp.configuration_manage)
  • Re: matrices, vectors, tensors
    ... > What are you using to sign a matrix, vector or tensor? ... > I don't like bold font, because you don't see if it is a vector or not. ... arrow) if needed. ...
    (comp.text.tex)
  • Re: Readability :-((
    ... > zulu wrote: ... > to drop the resolution to 800x600 and you specifically stated that you ... I really do NEED to use a BOLD font, as normal fonts aren't *solid* enough ...
    (microsoft.public.windowsxp.basics)
  • Re: Another Table of Contents question
    ... different font from the rest of the entry except by unlinking the TOC (when ... > The titles shown on my table of contents are not in bold font. ...
    (microsoft.public.word.docmanagement)