Font problem
-
Hello, I'm changing a form background color using OnEraseBkgnd(CDC* pDC) I'm also drawing texto and using a CFont for the propose. The problem is when OnEraseBkgnd(CDC* pDC) is called the second time, i'm using the font like: VERIFY(hFont.CreateFont(40, 15, 0, 0, FW_NORMAL,FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial Bold")); pOldFont=pDC->SelectObject(&hFont); pDC->SelectObject(hFont); pDC->DrawText(sFormName, -1, CRect(15, 0, rc.Width(),80), DT_LEFT|DT_SINGLELINE|DT_VCENTER); pDC->SelectObject(&pOldFont); But, after it exits the function i get an error. What am I doing wrong? Thank you for your time
-
Hello, I'm changing a form background color using OnEraseBkgnd(CDC* pDC) I'm also drawing texto and using a CFont for the propose. The problem is when OnEraseBkgnd(CDC* pDC) is called the second time, i'm using the font like: VERIFY(hFont.CreateFont(40, 15, 0, 0, FW_NORMAL,FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial Bold")); pOldFont=pDC->SelectObject(&hFont); pDC->SelectObject(hFont); pDC->DrawText(sFormName, -1, CRect(15, 0, rc.Width(),80), DT_LEFT|DT_SINGLELINE|DT_VCENTER); pDC->SelectObject(&pOldFont); But, after it exits the function i get an error. What am I doing wrong? Thank you for your time
Hallo, Don't create the font resource in the OnEraseBkgnd(...). Create the font in a function, which is called only once or try this:
if(hFont == NULL)
{
hFont.CreateFont(40, 15, 0, 0, FW_NORMAL,FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial Bold"));
}But the way better method is to make the font resource global and create it once, for example in the class'es constructor. And don't forget to release the font resource when exiting the program with DeleteObject. :-D -Dominik
-
Hallo, Don't create the font resource in the OnEraseBkgnd(...). Create the font in a function, which is called only once or try this:
if(hFont == NULL)
{
hFont.CreateFont(40, 15, 0, 0, FW_NORMAL,FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial Bold"));
}But the way better method is to make the font resource global and create it once, for example in the class'es constructor. And don't forget to release the font resource when exiting the program with DeleteObject. :-D -Dominik
Thank you Dominik for your advise, Well, the '==' operator dosen't work, because hFont is not a pointer. Is there a way to test if hFont was created? Now its Working fine, thank you very much