-
November 12th, 2013, 07:31 AM
#1
About CreateFont function ..
Hi All ,
I needed to change Default Text attributes (font and color) . So I wrote following code , UnicodeTextOut function. I called this function once in each 55ms to update the display screen. but I observed that my Screen used to become grey
like a crash after sometime . If I use Minimize an Maximize window , the screen was restoring back for some time.
When I thought over the code , I got my mistake that CreateFont initialization should be done once in the program and probably outside the function.
void UnicodeTextOut(int x, int y, CString s, UINT justify,int GreyScaleFlag)
{
HFONT hFontUnicode;
HFONT hfOld ;
hFontUnicode = CreateFont(16, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, _T("Aerial"));
hfOld = (HFONT) SelectObject(hdc, hFontUnicode);
SetBkColor(hdc,(RGB(128,128,128)));
SetTextColor(hdc,(RGB(255,255,255)));
SetTextAlign(hdc, justify);
TextOut(hdc, x, y, s, s.GetLength());
SelectObject(hdc, hfOld);
}
Then I shifted the line hFontUnicode = CreateFont(16, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, _T("Aerial")); in my screen class constructor and made hFontUnicode global .
This solved my problem as expected.
PL explain me why a system crash after certain no of CreateFont calls ? I am curious to know.
pl pl ..
-
November 12th, 2013, 08:07 AM
#2
Re: About CreateFont function ..
Have a look at CreateFont function, under Remarks section:
When you no longer need the font, call the DeleteObject function to delete it.
If you don't do it, then may result GDI resource leaks and finally, your application will behave abnormally.
One additional note:
Are you dealing with MFC? If yes, much more handy is to use CFont, as well as CBrush, CPen, and other MFC classes, instead of raw API GDI functions.
That way, it's not necessary to take care of freeing GDI resources.
-
November 13th, 2013, 01:13 AM
#3
Re: About CreateFont function ..
Thanks a lot ovidiucucu sir ,
I will read the link and will use CFont ..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|