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.
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 -
>