Solved

changing font in CEdit control

Posted on 2001-08-07
18
457 Views
Last Modified: 2013-12-02
How do I change the font of text written to a CEdit control
0
Comment
Question by:MeatWeasel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 4
  • 2
  • +2
18 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6360962
You'll have to call

CEdit::SetFont() // inherited from CWnd

to achieve this.
0
 

Author Comment

by:MeatWeasel
ID: 6361029
I need a code example showing a CEdit control being changed from one font to another
0
 
LVL 86

Expert Comment

by:jkr
ID: 6361062
/* create strikeout font */
CFont cf;
HANDLE hSystemFont = GetStockObject (SYSTEM_FONT);
GetObject ( hSystemFont, sizeof (LOGFONT), &lf);
lf.lfStrikeOut = TRUE;
hHANDLE StrikeoutFont = cf.CreateFontIndirect (&lf);

pEdit->SetFont ( &cf, TRUE);
0
Technology Partners: 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

by:MeatWeasel
ID: 6361084
is there for me to choose a different font, such as courier new, rather than creating my own
0
 

Author Comment

by:MeatWeasel
ID: 6361088
is there for me to choose a different font, such as courier new, rather than creating my own
0
 
LVL 86

Expert Comment

by:jkr
ID: 6361130
Well, use the 'ChooseFont()' common dialog:

    CHOOSEFONT cf;
    LOGFONT lf;
    CFont cf;

    cf.lStructSize = sizeof(cf);
    cf.hwndOwner = ghwndSpyApp;
    cf.hDC = NULL;
    cf.lpLogFont = &lf;
    cf.iPointSize = 0;
    cf.Flags = CF_ANSIONLY | CF_FORCEFONTEXIST | CF_INITTOLOGFONTSTRUCT
        | CF_SCREENFONTS;
    cf.rgbColors = 0;
    cf.lCustData = 0;
    cf.lpfnHook = NULL;
    cf.lpTemplateName = NULL;
    cf.hInstance = NULL;
    cf.lpszStyle = NULL;
    cf.nFontType = 0;
    cf.nSizeMin = 0;
    cf.nSizeMax = 0;

    if (ChooseFont(&cf))
    {
        cf.CreateFontIndirect (&lf);
        pEdit->SetFont ( &cf, TRUE);
    }
0
 
LVL 2

Expert Comment

by:Lockias
ID: 6361429
This creates a Times New Roman font that is 12pt (120 / 10)and normal weight.

   LOGFONT lf;
   memset(&lf, 0, sizeof(LOGFONT));
   strcpy(lf.lfFaceName, "Times New Roman");
   lf.lfHeight = 120;
   lf.lfWeight = FW_NORMAL;

   CFont oFont;
   oFont.CreateFontIndirect(&lf);

   pEdit->SetFont(&oFont, TRUE);

~Lockias
0
 

Author Comment

by:MeatWeasel
ID: 6364156
I tired both suggestions, however neither was what I was looking for.  

Lockias suggestion did not actually change the font, it just changed the vertical space availabe for each line of text.

Jkr suggestion would not compile.  I realised it had 2 variables with the same name.  I tried changing the name but I am not sure which cf goes where in the actual code.
0
 

Author Comment

by:MeatWeasel
ID: 6364167
All I want to do is change the font so that it is a monospace font instead of whatever the default currently is.

I need the font to be monospace so that when text is displayed in the edit box all of the columns line up.
0
 

Author Comment

by:MeatWeasel
ID: 6364175
p.s. Why are fonts so damned complicated.  It would make sense, to me anyway, to just have a function called ChooseFont("Courier"); That selected the font Courier.  Anyway thats just my 2 cents.
0
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 6364182
>>All I want to do is change the font so that it is a
>>monospace font instead of whatever the default currently
>>is.

Well, in this case, the following should be suitable:

/* create monospaced font */
CFont cf;
HANDLE hSystemFixedFont = GetStockObject (SYSTEM_FIXED_FONT);
GetObject ( hSystemFixedFont, sizeof (LOGFONT), &lf);
cf.CreateFontIndirect (&lf);

pEdit->SetFont ( &cf, TRUE);
0
 

Author Comment

by:MeatWeasel
ID: 6364222
It still did not change the font.  II have been trying to set the font in onInitDialog.  Is that the correct place to try and set the font?
0
 
LVL 2

Expert Comment

by:Lockias
ID: 6364393
This Works!  Sorry, I forgot the "Point" in my original posting of the create function.

  LOGFONT lf;
  memset(&lf, 0, sizeof(LOGFONT));
  strcpy(lf.lfFaceName, "Courier");
  lf.lfHeight = 120;
  lf.lfWeight = FW_NORMAL;

  CFont oFont;
  oFont.CreatePointFontIndirect(&lf);

  pEdit->SetFont(&oFont, TRUE);

~Lockias
0
 
LVL 1

Expert Comment

by:jacobkristensen
ID: 6367089
Try setting the bPitchAndFamily attribute of the LOGFONT structure like this:

lf.bPitchAndFamily = DEFAULT_PITCH || FF_MODERN;

I have used this myself...
0
 

Author Comment

by:MeatWeasel
ID: 6367686
Both lockias and jkr's suggestions worked.  However, now the edit control is acting quite odd.  I change the font as the first part of InitInstance, then when I type text into the window it is in new font.  When I press the delete key the text in the new font changes back to the old font.  Also when I try to write to the dialog using setWindowText it writes using the old font.  Any help woudl be appreciated
0
 
LVL 2

Expert Comment

by:xu2000
ID: 6368654
Hi,
Because font object is destroyed. Make a member varible like CFont m_font;

Xu
0
 

Author Comment

by:MeatWeasel
ID: 6369747
wohoooo it worked.  That last little tip from xu2000 was what made it work.  I am willing to give 50 to both jkr and lockias and how bout 25 to xu2000.  However, I will not be able to do that until monday.  Is everyone happy with that?
0
 

Author Comment

by:MeatWeasel
ID: 6388111
If this code is added to the initinstance() funtion do not forget to make the cf variable a member of the class.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows 10 how make picture as desktop background 2 66
WinWaitActive parameters 12 31
Smart Camera scanning and reading information 3 121
Making an alias 7 113
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…

756 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