CStatic + font setting on init

Posted on 2005-05-01
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 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 to get rid of this..!


Question by:yuvaprakash
    LVL 55

    Expert Comment

    by:Jaime Olivares
    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

    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
    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");

    Author Comment

    hi Jaime,

    thx for ur comments..! 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

    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

    wow..this seems to work great..!

    thanks jaime..!


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
    Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
    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…

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now