Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

changing font in CEdit control

Posted on 2001-08-07
18
Medium Priority
?
462 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

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 150 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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…

730 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