Solved

changing font in CEdit control

Posted on 2001-08-07
18
453 Views
Last Modified: 2013-12-02
How do I change the font of text written to a CEdit control
0
Comment
Question by:MeatWeasel
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ShiftLeft challenge 21 76
MaxSpan challenge 9 79
Updating statistics with error notification email in SQL server 4 72
unix example issues 18 73
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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…

912 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

15 Experts available now in Live!

Get 1:1 Help Now