Strange Font bolding
-
Hi all, I've just tried to bold the font in a particular static in one of my dialogs. for this i have done this : I Have 2 statics left aligned, containing exactly the same string (the upper one is for comparison purpose). The static I modify has a control variable of type
CStatic
on it. here is the code i put in the dialog'sOnInitDialog()
:HDC hdc = (HDC)(\*m\_myStatic.GetDC()); TEXTMETRIC tm; ::GetTextMetrics(hdc, &tm); int n = ::GetTextFace(hdc, 0, NULL); TCHAR\* pszFontName = new TCHAR\[n\]; ::GetTextFace(hdc, n, pszFontName); CFont nf; nf.CreateFont(tm.tmHeight, tm.tmAveCharWidth, 0, 0, tm.tmWeight, FALSE, tm.tmUnderlined, tm.tmStruckOut, tm.tmCharSet, OUT\_DEFAULT\_PRECIS, CLIP\_DEFAULT\_PRECIS, DEFAULT\_QUALITY, tm.tmPitchAndFamily, pszFontName); delete\[\] pszFontName; m\_myStatic.SetFont(&nf);
as you can see, i merely duplicate the original font without bolding it, but what a surprise, the static is bold anyway :wtf: can anyone explain me why the font (Tahoma) is acting like this ?
-
Hi all, I've just tried to bold the font in a particular static in one of my dialogs. for this i have done this : I Have 2 statics left aligned, containing exactly the same string (the upper one is for comparison purpose). The static I modify has a control variable of type
CStatic
on it. here is the code i put in the dialog'sOnInitDialog()
:HDC hdc = (HDC)(\*m\_myStatic.GetDC()); TEXTMETRIC tm; ::GetTextMetrics(hdc, &tm); int n = ::GetTextFace(hdc, 0, NULL); TCHAR\* pszFontName = new TCHAR\[n\]; ::GetTextFace(hdc, n, pszFontName); CFont nf; nf.CreateFont(tm.tmHeight, tm.tmAveCharWidth, 0, 0, tm.tmWeight, FALSE, tm.tmUnderlined, tm.tmStruckOut, tm.tmCharSet, OUT\_DEFAULT\_PRECIS, CLIP\_DEFAULT\_PRECIS, DEFAULT\_QUALITY, tm.tmPitchAndFamily, pszFontName); delete\[\] pszFontName; m\_myStatic.SetFont(&nf);
as you can see, i merely duplicate the original font without bolding it, but what a surprise, the static is bold anyway :wtf: can anyone explain me why the font (Tahoma) is acting like this ?
super_ttd wrote:
CFont nf; nf.CreateFont(tm.tmHeight, tm.tmAveCharWidth, 0, 0, tm.tmWeight, FALSE, tm.tmUnderlined, tm.tmStruckOut, tm.tmCharSet, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, tm.tmPitchAndFamily, pszFontName);
You are creating a local CFont object. Hence the CFont object get's destroyed. This is the reason for this wierd behavior, might be since the font object got destroyed it's using DEFAULT_GUI_FONT.:~
From MSDN:
Specifically, do not destroy the specified CFont object until after the CWnd control has been destroyed.
Windows does not copy the font specified in a SetFont() call.
If the font is destroyed before the control is destroyed, unpredictable results can occur."new" the font object and see the difference... And by the way you don't have to write this much code for modifying an existing font object... Here is the code snippet that does this...
CFont* pFont = m_cstc.GetFont();
LOGFONT lfLogFont = { 0 };
pFont->GetLogFont( &lfLogFont );
lfLogFont.lfWeight = FW_BOLD;CFont pNewFont = new CFont;// Delete this afterwards, or make this a member variable
pNewFont->CreateFontIndirect( &lfLogFont );
m_cstc.SetFont( pNewFont );
Nibu thomas A Developer Programming tips[^] My site[^]
-
Hi all, I've just tried to bold the font in a particular static in one of my dialogs. for this i have done this : I Have 2 statics left aligned, containing exactly the same string (the upper one is for comparison purpose). The static I modify has a control variable of type
CStatic
on it. here is the code i put in the dialog'sOnInitDialog()
:HDC hdc = (HDC)(\*m\_myStatic.GetDC()); TEXTMETRIC tm; ::GetTextMetrics(hdc, &tm); int n = ::GetTextFace(hdc, 0, NULL); TCHAR\* pszFontName = new TCHAR\[n\]; ::GetTextFace(hdc, n, pszFontName); CFont nf; nf.CreateFont(tm.tmHeight, tm.tmAveCharWidth, 0, 0, tm.tmWeight, FALSE, tm.tmUnderlined, tm.tmStruckOut, tm.tmCharSet, OUT\_DEFAULT\_PRECIS, CLIP\_DEFAULT\_PRECIS, DEFAULT\_QUALITY, tm.tmPitchAndFamily, pszFontName); delete\[\] pszFontName; m\_myStatic.SetFont(&nf);
as you can see, i merely duplicate the original font without bolding it, but what a surprise, the static is bold anyway :wtf: can anyone explain me why the font (Tahoma) is acting like this ?
super_ttd wrote:
can anyone explain me why the font (Tahoma) is acting like this ?
Don't make things any more complicated than necessary. See here.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
super_ttd wrote:
can anyone explain me why the font (Tahoma) is acting like this ?
Don't make things any more complicated than necessary. See here.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
hi David, Thanks for answering. but you know, i refered this exact post to initiate my test, and i found that the answers to this guy were far from being complete... see, i couldn't figure out myself the right way with your 3 replies there. but thanks to Babu, I think I'm on the road again :cool:
-
hi David, Thanks for answering. but you know, i refered this exact post to initiate my test, and i found that the answers to this guy were far from being complete... see, i couldn't figure out myself the right way with your 3 replies there. but thanks to Babu, I think I'm on the road again :cool:
super_ttd wrote:
thanks to Babu
Nibu babu thomas. ;)
Nibu thomas A Developer Programming tips[^] My site[^]