simple question -- Set font for CEdit control 
Author Message
 simple question -- Set font for CEdit control

Hi all.

I have a dialog with a cedit control in it. I give the user the option of
choosing the font for the control, which brings up a cfiledialog. I get the
LOGFONT from that. What is the simplest way to then set the font of the edit
control?

I have something like this, where m_editConsonants is the edit control variable.

        LOGFONT lf;
        CFontDialog dlg(&lf, CF_SCREENFONTS|CF_INITTOLOGFONTSTRUCT);
        if (dlg.DoModal() == IDOK)
        {
                // switch to new font.
                CFont m_font;
                if (m_font.CreateFontIndirect(&lf))
                {
                        m_editConsonants.SetFont(&m_font);
                }
        }

what else do I need to do?

Thanks to anyone who can help.

wil snyder



Tue, 26 Feb 2002 03:00:00 GMT  
 simple question -- Set font for CEdit control
You have to initialize lf before passing it to CFontDialog:

(This is from VC++ help)

LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));CClientDC dc(this);
lf.lfHeight = -MulDiv(12, dc.GetDeviceCaps(LOGPIXELSY), 72);
strcpy(lf.lfFaceName, "Times New Roman");
CFontDialog dlg(&lf);
dlg.DoModal();

--
Ajay Kalra

Microsoft Visual C++  MVP

Quote:

>Hi all.

>I have a dialog with a cedit control in it. I give the user the option of
>choosing the font for the control, which brings up a cfiledialog. I get the
>LOGFONT from that. What is the simplest way to then set the font of the
edit
>control?

>I have something like this, where m_editConsonants is the edit control
variable.

> LOGFONT lf;
> CFontDialog dlg(&lf, CF_SCREENFONTS|CF_INITTOLOGFONTSTRUCT);
> if (dlg.DoModal() == IDOK)
> {
> // switch to new font.
> CFont m_font;
> if (m_font.CreateFontIndirect(&lf))
> {
> m_editConsonants.SetFont(&m_font);
> }
> }

>what else do I need to do?

>Thanks to anyone who can help.

>wil snyder



Tue, 26 Feb 2002 03:00:00 GMT  
 simple question -- Set font for CEdit control
It appears that you are creating the CFont instance in a function, so that
the font will die as soon as the function exits. Move the:

    CFont m_Font;

line to th header for this file and you might get better results...

Cecil

--
CodeSoup Web Site with free sample code/tips
www.codesoup.com


Quote:
> Hi all.

> I have a dialog with a cedit control in it. I give the user the option of
> choosing the font for the control, which brings up a cfiledialog. I get
the
> LOGFONT from that. What is the simplest way to then set the font of the
edit
> control?

> I have something like this, where m_editConsonants is the edit control
variable.

> LOGFONT lf;
> CFontDialog dlg(&lf, CF_SCREENFONTS|CF_INITTOLOGFONTSTRUCT);
> if (dlg.DoModal() == IDOK)
> {
> // switch to new font.
> CFont m_font;
> if (m_font.CreateFontIndirect(&lf))
> {
> m_editConsonants.SetFont(&m_font);
> }
> }

> what else do I need to do?

> Thanks to anyone who can help.

> wil snyder



Tue, 26 Feb 2002 03:00:00 GMT  
 simple question -- Set font for CEdit control
The real problem is, as pointed out below, the fact that your CFont is
destroyed on exit, deleting the font. You can get around this by doing
        {
        CFont font;
        font.CreateFontIndirect(...);
        c_Edit.SetFont(&font);
        font.Detach();
        }

The Detach operation dissociates the HFONT (the underlying Windows
object) from the CFont (the C++ wrapper for the HFONT) so when the
wrapper is destroyed, the font is not. However, note that you now have
a "resource leak", in that if you then set the font again, the only
reference to the HFONT is lost and the font space is lost until your
program exits. Therefore, you probably need to do something like
        {
        CFont font;
        c_Edit.GetFont(&font);
        font.DeleteObject();
        font.CreateFontIndirect(...);
        c_Edit.SetFont(&font);
        font.Detach();
        }

Note, by the way, that it is silly to use the "m_" prefix for a local
variable; this is supposed to be used only for member variables of a
class, not local variables. Personally, I think the m_ prefix is a bit
of gratuitous nonsense, and I only use it for member variables that
are initialized by the caller of a dialog, but if you are going to use
it, it should be used in a fashion consistent with its intended usage.
                                joe

On 10 Sep 1999 18:08:11 PDT, "Cecil A. Galbraith"

Quote:

>It appears that you are creating the CFont instance in a function, so that
>the font will die as soon as the function exits. Move the:

>    CFont m_Font;

>line to th header for this file and you might get better results...

>Cecil



Sat, 02 Mar 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. how to set the color of font for a CEdit Control

2. Setting font monospaced in CEdit dlg control

3. Question about setting the font type in CEdit box

4. CFont, CEdit, setting font colour question

5. Set CEdit font to COleControl Font

6. Setting Fonts on CEdit - URGENT, please help!!!

7. How to set the font of CEdit?

8. Help: how to dynamicly set font in CEdit

9. Setting Fonts on CEdit - URGENT, please help!!!

10. How to set default font in the simplest text editor

11. Changing Font in CEdit control

12. Changing the font of a CEdit control (non dialog)

 

 
Powered by phpBB® Forum Software