The way to change the size of the true type font without resource reducing.

Hi,
I created a true type font using CFont::CreateFont function in the
constructor of  my own class.
Whenever I change the MDI child window, the font size changes
according to the size of the MDI child window.
The Problem is that the GDI resource is reduced whenever the size
changes.
I think that this is caused because of the cache of the
true type font table.
I moved the font variable in my own class member to the local variable
in the function.
And the problem did not occurr again.
But I don't want to call CreateFont function whenever the function is
called.
Is there any other way to change the size of the true type font without
creating the font everytime?
Thanks for your time and effort.
sleepyreaAsked:
Who is Participating?
 
jstolanConnect With a Mentor Commented:
Your resources were being reduced because you weren't freeing them when the CFont object was a member variable.  The resources are freed when the CFont object is destroyed.  When you repeatedly called CreateFont without destroying it, resources were lost.

Here's what you can do:

1.  Create a pointer to a CFont in your class as a member variable.  Initialize it to NULL in the constructor.

2.  In the OnSize window function do the following

2.1 If the CFont pointer is not NULL, delete it.
2.2 Use the new operator to create a new CFont object, pointed to by your member variable.
2.3 Use CreateFont to create a font of the size you need

3.  In your OnDraw routine do the following

3.1  Select the CFont pointed to by the member variable into the display context.
3.2  Do any drawing operations
3.3  Select the CFont out of the display context.

Note that OnSize is called before any draw operations so that there will be a CFont object there for the initial display.  Sorry there's no shortcut for eliminating the CreateFont calls, but at least this only makes the calls as often as necessary.
0
 
jstolanCommented:
Oh yeah, you probably should make sure you delete the CFont in the destructor as well.
0
All Courses

From novice to tech pro — start learning today.