• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

changing font in CEdit control

How do I change the font of text written to a CEdit control
0
MeatWeasel
Asked:
MeatWeasel
  • 10
  • 4
  • 2
  • +2
1 Solution
 
jkrCommented:
You'll have to call

CEdit::SetFont() // inherited from CWnd

to achieve this.
0
 
MeatWeaselAuthor Commented:
I need a code example showing a CEdit control being changed from one font to another
0
 
jkrCommented:
/* 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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
MeatWeaselAuthor Commented:
is there for me to choose a different font, such as courier new, rather than creating my own
0
 
MeatWeaselAuthor Commented:
is there for me to choose a different font, such as courier new, rather than creating my own
0
 
jkrCommented:
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
 
LockiasCommented:
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
 
MeatWeaselAuthor Commented:
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
 
MeatWeaselAuthor Commented:
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
 
MeatWeaselAuthor Commented:
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
 
jkrCommented:
>>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
 
MeatWeaselAuthor Commented:
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
 
LockiasCommented:
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
 
jacobkristensenCommented:
Try setting the bPitchAndFamily attribute of the LOGFONT structure like this:

lf.bPitchAndFamily = DEFAULT_PITCH || FF_MODERN;

I have used this myself...
0
 
MeatWeaselAuthor Commented:
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
 
xu2000Commented:
Hi,
Because font object is destroyed. Make a member varible like CFont m_font;

Xu
0
 
MeatWeaselAuthor Commented:
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
 
MeatWeaselAuthor Commented:
If this code is added to the initinstance() funtion do not forget to make the cf variable a member of the class.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 10
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now