CStatic + font setting on init

Posted on 2005-05-01
Medium Priority
Last Modified: 2013-12-03
Hi all..

Warm greetings..

Is it possible to set the font size of a CStatic control while initializing the application..?
I tried but it didnt work..!
Just add a CStatic control in a new MFC exe project and i added the following code.

            CFont font;
   20,                        // nHeight
   0,                         // nWidth
   0,                         // nEscapement
   0,                         // nOrientation
   FW_NORMAL,                 // nWeight
   FALSE,                     // bItalic
   TRUE,                     // bUnderline
   0,                         // cStrikeOut
   ANSI_CHARSET,              // nCharSet
   OUT_DEFAULT_PRECIS,        // nOutPrecision
   CLIP_DEFAULT_PRECIS,       // nClipPrecision
   DEFAULT_QUALITY,           // nQuality
   DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
   "Verdana");                 // lpszFacename



When i am adding the above to a button control member function..it works..but i want to load the above font..while initialising the application..!

May I know why the code above didnt work..?
Also Please advice me..how to get rid of this..!


Question by:yuvaprakash
  • 3
  • 3
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13905311
you object must be CREATED after aplying font. If you just have declared the object as a member of a window object, then it is not still created.

Author Comment

ID: 13905596
Thanks jaime,

One more thing i have to tell you..! Whenever I am calling the above two lines with a button object then i could change the font..!
But I want to display the font size (ex. with 20 size) when the application starts..!

Will wait for some more replies.. :)

LVL 55

Expert Comment

by:Jaime Olivares
ID: 13905626
You can load font anywhere in your app, including constructor and InitInstance (but it is prefereably in your window's constructor).

Also, you can't declare this:
CFont font;
inside any object's function because it will be destroyed when function exists, it has to be a member of some object, or a global variable.

If you are creating a simple font (without any special decoration), then will be easy to use CreatePointFont():

something like:
font.CreatePointFont(20*10, "Verdana");
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13909218
hi Jaime,

thx for ur comments..!

Jaime..do you mind to explain little bit clear..!

I tried to create a global variable to the dialog class and initiated the CFont* in InitDialog fn..then assigned to control object..then also its not working..! :(

LVL 55

Accepted Solution

Jaime Olivares earned 500 total points
ID: 13909298
This is the best alternative, I think

class CYourDialog : public CDialog
       CFont m_Font;   // here font is **constructed**
       // etcetera
       virtual BOOL OnInitDialog();   // don't declare it manually, use class Wizard (by pressing Ctrl-W)
       // etcetera

BOOL CYourDialog ::OnInitDialog()

      m_Font.CreatePointFont(20*10, "Verdana");   // here font is **created**
                // Apply the font to any control here

                // etcetera
      return TRUE;

But if you plan to use the font in many dialogs, you can declare m_Font as a global variable instead of a class' member (let's name it g_Font), in that case, you will have to use:  extern CFont g_Font;   in every file you will use it.

Author Comment

ID: 13909503
wow..this seems to work great..!

thanks jaime..!


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question