Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing font of CEdit

1 view
Skip to first unread message

Claire Sylvestre

unread,
Feb 11, 1998, 3:00:00 AM2/11/98
to

Your CFont object (font) is destroyed when the function is exited. You must
make it a member variable of your CTextPropDlg class. On the other hand, the
LOGFONT structure (m_logfont) can be local to the function.

Once this is corrected, you will also have to delete the current font before
assigning a new one to the CFont object, otherwise your app will assert the
next time the user presses the button.

Last, it would be a good idea to initialize the LOGFONT structure with the
current font from the edit control.

LOGFONT logfont;
CFont * pFont = m_editText.GetFont();
pFont->GetLogFont( &logfont );
CFontDialog dlg;
dlg.m_cf.lpLogFont = &logfont;
dlg.m_cf.Flags |= CF_INITTOLOGFONTSTRUCT | CF_EFFECTS;
if (dlg.DoModal() != IDOK)
return;
m_Font.DeleteObject();
m_Font.CreateFontIndirect(&logfont);
m_editText.SetFont(&m_Font, TRUE);


Claire Sylvestre


Ki-Yong Kim wrote in message <34E130B8...@nownuri.net>...
>CTextPropDlg dialog has a CEdit control and a "Font..." Button.
>When user clicks on the button, the font in the edit control should be
>changed into the font.
>
>I wrote:
>
>void CTextPropDlg::OnTextPropFont()
>{
> CFontDialog dlg;
> dlg.m_cf.lpLogFont = &m_logfont;
> dlg.m_cf.Flags |= CF_INITTOLOGFONTSTRUCT | CF_EFFECTS;
> if (dlg.DoModal() != IDOK)
> return;
> m_logfont = *dlg.m_cf.lpLogFont;
> CFont font;
> font.CreateFontIndirect(&m_logfont);
> m_editText.SetFont(&font, TRUE);
>}
>
>I can only find the caret size and spacing are as large as I specified
>in the CFontDialog.
>The font of displayed text is the previous one.


Florent

unread,
Feb 11, 1998, 3:00:00 AM2/11/98
to

Hi
(Sorry I don't speak english very well)
The problem is that you local object CFont dosn't longer exist when the call
to void CTextPropDlg::OnTextPropFont() is over, so the edit control can't
use the font. You should use a member variable CFont. And you should use a
COLORREF variable too if you want your edit control to use the right color.

1)
class CTextPropDlg...
{
protected:
CFont* m_pFont ;
COLORREF m_rgbText ;
}

2) In the constructor of your CTextPropDlg class
CTextPropDlg::CTextPropDlg()
{
m_pFont = NULL ;
m_rgbText = ::GetSysColor(COLOR_WINDOWTEXT) ;
}

3) In the destructor
CTextPropDlg::~CTextPropDlg()
{
if (m_pFont)
{
delete m_pFont ;
m_pFont = NULL ;
}
}

4) Your function
void CTextPropDlg::OnTextPropFont()
{
LOGFONT LogFont ;
CFont* pFont = (m_pFont) ? m_pFont : m_editText.GetFont() ;
if (pFont)
{
pFont->GetLogFont(&LogFont) ;
}
CFontDialog dlg(&LogFont) ;
dlg.m_cf.rgbColors = m_rgbText ;
if (dlg.DoModal() == IDOK)
{
m_rgbText = dlg.GetColor() ;
if (m_pFont==NULL)
{
m_pFont = new CFont ;
}
else
{
m_pFont->DeleteObject() ;
}
m_pFont->CreateFontIndirect(dlg.m_cf.lpLogFont) ;
m_editText.SetFont(m_pFont) ;
}
}

5) To use you m_rgbText defined color you have to handle the WM_CTLCOLOR
HBRUSH CTextPropDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor);

// TODO: Change any attributes of the DC here
if (pWnd==&m_editText)
{
pDC->SetTextColor(m_rgbText) ;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}

Bye
Florent

Ki-Yong Kim a écrit dans le message <34E130B8...@nownuri.net>...


>CTextPropDlg dialog has a CEdit control and a "Font..." Button.
>When user clicks on the button, the font in the edit control should be
>changed into the font.
>
>I wrote:
>
>void CTextPropDlg::OnTextPropFont()
>{
> CFontDialog dlg;
> dlg.m_cf.lpLogFont = &m_logfont;
> dlg.m_cf.Flags |= CF_INITTOLOGFONTSTRUCT | CF_EFFECTS;
> if (dlg.DoModal() != IDOK)
> return;
> m_logfont = *dlg.m_cf.lpLogFont;
> CFont font;
> font.CreateFontIndirect(&m_logfont);
> m_editText.SetFont(&font, TRUE);
>}
>
>I can only find the caret size and spacing are as large as I specified
>in the CFontDialog.
>The font of displayed text is the previous one.
>

>What should I additionally do?
>
>- scg...@nownuri.net -
>

0 new messages