About CreateFont function ..
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: About CreateFont function ..

  1. #1
    Join Date
    Jan 2012
    Location
    India
    Posts
    145

    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 ..

  2. #2
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,057

    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.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  3. #3
    Join Date
    Jan 2012
    Location
    India
    Posts
    145

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center