I am trying to modify the font of an Edit box in my dialog based
application
All I want is to change the font to be monospace- I'm quite happy with all
the other attributes
So I tried the following:
CEdit* pScriptEdit;
pScriptEdit = (CEdit*) GetDlgItem(IDC_SCRIPT_EDIT); // pointer to the
edit control
CFont* CMyFont;
LOGFONT lf;
memset((void*)&lf,0,sizeof(lf));
// Get the current Font used by the Edit control
CMyFont = pScriptEdit->GetFont();
CMyFont->GetLogFont(&lf);
// Modify the Font to use MonoSpace
lf.lfPitchAndFamily = FF_MODERN ;
_tcscpy(lf.lfFaceName,_T("CourierNew"));
CMyFont->CreateFontIndirect(&lf);
// Set the new Font to the Edit control
pScriptEdit->SetFont(CMyFont,TRUE);
This ultimately works but when debugging a Debug Assert failure occurs
for the CreatFontIndirect call. I believe this because of the GetFont call
beforehand.
I have tried creating a Font from scratch using CreateFont and using the
values
I see from the LOGFONT structure when I debugged the above code but the
result is weird
- the text isn't refreshed correctly, the characters seem chopped off etc.
So, how can I go about getting a decent monospace font without debug
assertions?
Thanks in advance!
Stephanie Cowie
You never "new" CMyFont (which should be called pMyFont, pardon me :)
You must have a member CFont m_MyFont, that you create with
CreateFontIndirect() and set as the font for the CEdit. The new font must
live as long as the CEdit control, and you shouldn't mess around with the
font that is already in there (except for getting the LOGFONT.)
cU
--
Dirk Djuga mailto:di...@solaris.stuttgart.netsurf.de
http://djuga.home.ml.org
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Retriever 1.1 is out: http://retriever.home.ml.org
Shareware image viewer, thumbnailer & database
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<
<
<
<
<
<
<
<
<
I had struggled the last few days to change some
CEdit's to FIXED fonts also; here is the working
solution I came up with.
1) Create compatible CClientDC of CEdit Object.
2) Use GetDeviceCaps() to calculate Height value
3) CreateFont() using static (global) m_CFontFixed
4) SetFont() for all necessary CEdit Objects.
Code is as follows; ctl_IOIn = my CEdit Object
This code is in my View::OnInitialUpdate()
// Set CEdit fonts to ANSI FIXED
CClientDC dc(FromHandle(ctl_IOIn.m_hWnd));
int nHeight = -((dc.GetDeviceCaps(LOGPIXELSY)*8)/72);
m_CFontFixed.CreateFont(nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
ANSI_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, FIXED_PITCH | FF_DONTCARE, "System");
// set font of both CEdit Objects to our Fixed Font
ctl_IOIn.SetFont(&m_CFontFixed);
ctl_IOOut.SetFont(&m_CFontFixed);
Hope this helps you! :)