Solved

changing font in CEdit control

Posted on 2001-08-07
18
455 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Named range not carried over 10 63
sum13 challenge 24 106
ShiftLeft challenge 21 80
noX challenge 17 112
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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…

786 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