A SetFont problem
-
Hi all, I have a question about SetFont. Now I create a class that is derived from CWnd, and use SetFont to set a font to it. Then I override the Paint function to display the content I want. The problem happens there, no matter what font I set, when I draw the text in Paint, it display with the same font, I think it is the system font. How can I make the SetFont work? Thanks in advance
-
Hi all, I have a question about SetFont. Now I create a class that is derived from CWnd, and use SetFont to set a font to it. Then I override the Paint function to display the content I want. The problem happens there, no matter what font I set, when I draw the text in Paint, it display with the same font, I think it is the system font. How can I make the SetFont work? Thanks in advance
if you are using DrawText or TextOut, you have to use CreateFont/SelectObject . SetFont doesn't do what you think it does. something like this:
CFont fnt;
fnt.CreateFont(...);CFont *pOldFont = dc.SelectObject(&fnt);
dc.TextOut(...);
or
dc.DrawText(...);dc.SelectObject(pOldFont);
-c CheeseWeasle
-
if you are using DrawText or TextOut, you have to use CreateFont/SelectObject . SetFont doesn't do what you think it does. something like this:
CFont fnt;
fnt.CreateFont(...);CFont *pOldFont = dc.SelectObject(&fnt);
dc.TextOut(...);
or
dc.DrawText(...);dc.SelectObject(pOldFont);
-c CheeseWeasle
Thank you for your reply. After reading your comment, I have another question. I have used SetFont to set a font to it. How can I know which font I have set when I want to use it in Paint function. Because my class is used by other people, I have to use a generic way to know which font they set. Actually I have tried another way, I override the SetFont function in my class, and remember the font they said. It worked. But because the SetFont is not a virtual one, when they use a CWnd pointer to call the SetFont function, my SetFont will not be called. Thanks again.
-
Thank you for your reply. After reading your comment, I have another question. I have used SetFont to set a font to it. How can I know which font I have set when I want to use it in Paint function. Because my class is used by other people, I have to use a generic way to know which font they set. Actually I have tried another way, I override the SetFont function in my class, and remember the font they said. It worked. But because the SetFont is not a virtual one, when they use a CWnd pointer to call the SetFont function, my SetFont will not be called. Thanks again.
vincentye wrote: How can I know which font I have set when I want to use it in Paint function have you tried GetFont() ? -c CheeseWeasle
-
vincentye wrote: How can I know which font I have set when I want to use it in Paint function have you tried GetFont() ? -c CheeseWeasle
Sure I did. Some of my code is below: void CColItem::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rect; GetClientRect(rect); CFont *OldFont = NULL; OldFont = dc.SelectObject(GetFont()); .............. dc.TextOut(xText, yText, strWindowText); .............. dc.SelectObject(OldFont); } Thanks again
-
Sure I did. Some of my code is below: void CColItem::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rect; GetClientRect(rect); CFont *OldFont = NULL; OldFont = dc.SelectObject(GetFont()); .............. dc.TextOut(xText, yText, strWindowText); .............. dc.SelectObject(OldFont); } Thanks again
The painting code is right. It sounds more like that the code you're using to test it is somehow not passing the right font. Or perhaps you're creating a font that's local (allocated on the stack) and it's going out of scope. SetFont takes a pointer. If that pointer becomes invalid, I'd suspect that the dc.SelectObject( GetFont() ); would fail. If you're unsure, post the code that calls SetFont and I'll take a look. Joel Lucsy
-
The painting code is right. It sounds more like that the code you're using to test it is somehow not passing the right font. Or perhaps you're creating a font that's local (allocated on the stack) and it's going out of scope. SetFont takes a pointer. If that pointer becomes invalid, I'd suspect that the dc.SelectObject( GetFont() ); would fail. If you're unsure, post the code that calls SetFont and I'll take a look. Joel Lucsy
Thank you for your information. However it is not that case. Below is the answer from MS newsgroup: " There is no default implementation of WM_SETFONT and WM_GETFONT. You need to implement these message handlers to store and return the current HFONT (or NULL as the case may be), and Invalidate if LPARAM is TRUE. IIRC, ClassWizard won't help with these, so you'll need to add them manually using ON_MESSAGE macros in your message-map, and remember that the prototype is 'LRESULT memberFxn(WPARAM, LPARAM)'. In your WM_PAINT handler, you will need to SelectObject the currently stored HFONT (if there is one) into your CPaintDC, and SelectObject it back out again when you're done. -- Jeff Partch [VC++ MVP] " I follow his instruction. It works well. Anyway, thanks again.
-
Sure I did. Some of my code is below: void CColItem::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rect; GetClientRect(rect); CFont *OldFont = NULL; OldFont = dc.SelectObject(GetFont()); .............. dc.TextOut(xText, yText, strWindowText); .............. dc.SelectObject(OldFont); } Thanks again
When you call
SetFont()
, are you doing something like this?CFont font;
font.CreateFont(...);
SetFont(&font);If you do this, the font will be destroyed as soon as the CFont object goes out of scope, so the default system font will be returned by GetFont(). Make sure you either detach the font (
CFont::Detach()
) or use a font object that is a class member. Hope this helps,Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"