Changing Font Style / Selecting and Deleting Objects 
Author Message
 Changing Font Style / Selecting and Deleting Objects

Hi,

I am repeatedly drawing Messages in a Timer loop.
I can see the Memory, in Task Manager, increase by 4K approx every second.
I isolated the problem down to the way I change the font.
The problem happens when the ChangeFontItalic() is called.
It works just fine with the Default Font.

I believe I am doing the correct procedure, but can't figure out
why the memory increases?

I have omitted the MemDC setup for easier reading.
void CMyView::DrawMessages()
{
 m_Font.CreateFontIndirect(&m_LogFont); //Default Log Font Structure
 m_pFontOrig = MEMDC.SelectObject(&m_Font);

 ChangeFontItalic(&MEMDC); //Adding this causes Memory Leak
 MEMDC.SelectObject(&m_pFontOrig);  //Select the Font out of the Context
 m_Font.DeleteObject();  //Delete the Object

Quote:
}

void CMyView::ChangeFontItalic(CDC *pDC)
{
 pDC->SelectObject(&pFontOrig); //Select the Font out of the Context
 m_Font.DeleteObject();   //Delete the Object
 m_LogFont.lfItalic = TRUE; //Change the Font Attributes
 VERIFY(m_Font.CreateFontIndirect(&m_LogFont) ); //Create another Font
 pFontOrig = pDC->SelectObject(&m_Font); //Select the Font into the Device
Context

Quote:
}

Thanks for your help,


Tue, 15 Feb 2005 04:33:41 GMT  
 Changing Font Style / Selecting and Deleting Objects
Er, is the "pDC->SelectObject(&pFontOrig); " line a typo?
Because if it's not a typo and it actually compiled, then you have a
pFontOrig in addition to the m_pFontOrig.

Nish



Quote:
> Hi,

> I am repeatedly drawing Messages in a Timer loop.
> I can see the Memory, in Task Manager, increase by 4K approx every second.
> I isolated the problem down to the way I change the font.
> The problem happens when the ChangeFontItalic() is called.
> It works just fine with the Default Font.

> I believe I am doing the correct procedure, but can't figure out
> why the memory increases?

> I have omitted the MemDC setup for easier reading.
> void CMyView::DrawMessages()
> {
>  m_Font.CreateFontIndirect(&m_LogFont); //Default Log Font Structure
>  m_pFontOrig = MEMDC.SelectObject(&m_Font);

>  ChangeFontItalic(&MEMDC); //Adding this causes Memory Leak
>  MEMDC.SelectObject(&m_pFontOrig);  //Select the Font out of the Context
>  m_Font.DeleteObject();  //Delete the Object
> }

> void CMyView::ChangeFontItalic(CDC *pDC)
> {
>  pDC->SelectObject(&pFontOrig); //Select the Font out of the Context
>  m_Font.DeleteObject();   //Delete the Object
>  m_LogFont.lfItalic = TRUE; //Change the Font Attributes
>  VERIFY(m_Font.CreateFontIndirect(&m_LogFont) ); //Create another Font
>  pFontOrig = pDC->SelectObject(&m_Font); //Select the Font into the Device
> Context
> }

> Thanks for your help,



Tue, 15 Feb 2005 08:49:17 GMT  
 Changing Font Style / Selecting and Deleting Objects
Hi,

That might have been causing some trouble, So I went ahead a changed it to
m_pFontOrig.
and got rid of pFontOrig.

Still, I get memory leaks.

Thanks


Quote:
> Er, is the "pDC->SelectObject(&pFontOrig); " line a typo?
> Because if it's not a typo and it actually compiled, then you have a
> pFontOrig in addition to the m_pFontOrig.

> Nish



> > Hi,

> > I am repeatedly drawing Messages in a Timer loop.
> > I can see the Memory, in Task Manager, increase by 4K approx every
second.
> > I isolated the problem down to the way I change the font.
> > The problem happens when the ChangeFontItalic() is called.
> > It works just fine with the Default Font.

> > I believe I am doing the correct procedure, but can't figure out
> > why the memory increases?

> > I have omitted the MemDC setup for easier reading.
> > void CMyView::DrawMessages()
> > {
> >  m_Font.CreateFontIndirect(&m_LogFont); //Default Log Font Structure
> >  m_pFontOrig = MEMDC.SelectObject(&m_Font);

> >  ChangeFontItalic(&MEMDC); //Adding this causes Memory Leak
> >  MEMDC.SelectObject(&m_pFontOrig);  //Select the Font out of the Context
> >  m_Font.DeleteObject();  //Delete the Object
> > }

> > void CMyView::ChangeFontItalic(CDC *pDC)
> > {
> >  pDC->SelectObject(&pFontOrig); //Select the Font out of the Context
> >  m_Font.DeleteObject();   //Delete the Object
> >  m_LogFont.lfItalic = TRUE; //Change the Font Attributes
> >  VERIFY(m_Font.CreateFontIndirect(&m_LogFont) ); //Create another Font
> >  pFontOrig = pDC->SelectObject(&m_Font); //Select the Font into the
Device
> > Context
> > }

> > Thanks for your help,



Tue, 15 Feb 2005 16:55:22 GMT  
 Changing Font Style / Selecting and Deleting Objects
   Hi..

Try the following

        m_Font = GetFont();

        if (m_Font )
        {
            LOGFONT lf;
            m_Font->GetLogFont(&lf);
            if ( ! lf.lfItalic )
            {
                lf.lfItalic = TRUE;         // Change Font to Italic..
                CFont* pFontOrig = MEMDC.SelectObject(&m_Font);

                // Do your drawing...

                MEMDC.SelectObject(&pFontOrig);  //Select the Original font.

Quote:
}

        HTH.

--
Cheers
Check Abdoul
----------------



Quote:
> Hi,

> I am repeatedly drawing Messages in a Timer loop.
> I can see the Memory, in Task Manager, increase by 4K approx every second.
> I isolated the problem down to the way I change the font.
> The problem happens when the ChangeFontItalic() is called.
> It works just fine with the Default Font.

> I believe I am doing the correct procedure, but can't figure out
> why the memory increases?

> I have omitted the MemDC setup for easier reading.
> void CMyView::DrawMessages()
> {
>  m_Font.CreateFontIndirect(&m_LogFont); //Default Log Font Structure
>  m_pFontOrig = MEMDC.SelectObject(&m_Font);

>  ChangeFontItalic(&MEMDC); //Adding this causes Memory Leak
>  MEMDC.SelectObject(&m_pFontOrig);  //Select the Font out of the Context
>  m_Font.DeleteObject();  //Delete the Object
> }

> void CMyView::ChangeFontItalic(CDC *pDC)
> {
>  pDC->SelectObject(&pFontOrig); //Select the Font out of the Context
>  m_Font.DeleteObject();   //Delete the Object
>  m_LogFont.lfItalic = TRUE; //Change the Font Attributes
>  VERIFY(m_Font.CreateFontIndirect(&m_LogFont) ); //Create another Font
>  pFontOrig = pDC->SelectObject(&m_Font); //Select the Font into the Device
> Context
> }

> Thanks for your help,



Tue, 15 Feb 2005 17:14:52 GMT  
 Changing Font Style / Selecting and Deleting Objects
Hi,

Quote:
>         m_Font = GetFont();

This doesn't work unless I do a SetFont() beforehand, which doesn't seem to
work.
I believe it changes the Window Title font too?

 LOGFONT lf;
 m_Font->GetLogFont(&lf);
if ( ! lf.lfItalic )
{
    lf.lfItalic = TRUE;         // Change Font to Italic..
   CFont* pFontOrig = MEMDC.SelectObject(&m_Font);

  // Do your drawing...

MEMDC.SelectObject(&pFontOrig);  //Select the Original font.
I don't believe I can change the Font Attributes while the FontObject is
still in the Device Context?

I have a class member m_Font that I select into the Device Context.
Then, I try to change the Font. So First, I select the font out of the
device Context.
Then, I delete the font object. Next, I create a new font with the new
attributes
then I select it into the Device Context.

This is working. The font is changed, but the memory keeps increasing.

ChangeFont()
{
 //Delete the Current Font
 pDC->SelectObject(&m_pFontOrig); //Select the Font out of the Context
 m_Font.DeleteObject();

 //Change the Attributes
 m_LogFont.lfWeight = FW_NORMAL;
 m_LogFont.lfItalic = FALSE;
 m_LogFont.lfUnderline = FALSE;

 //Create and Select the New Font
 m_Font.CreateFontIndirect(&m_LogFont);
 m_pFontOrig = pDC->SelectObject(&m_Font);

Quote:
}

Thanks for your help,


Tue, 15 Feb 2005 22:10:09 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How I can change the Font or Style to CStatic object

2. changing font selected

3. Help, selecting and changing a font...

4. Changing font of selected text

5. changing font selected

6. Changing Font style and size in control windows

7. System font settings changes Window style!

8. changing the font-style in a clistview control

9. Changing font style in CEdit derived control.

10. Changing font style using SetCharFormat()

11. How to change font style in AfxMessageBox() ?

12. Change font style in a dialog

 

 
Powered by phpBB® Forum Software