Solved

Font problems in a CRichEditCtrl

Posted on 1997-11-22
3
1,132 Views
Last Modified: 2013-12-02
I am having trouble changing the font in a CRichEditCtrl (created as a child window).  When I tried to use the CRichEditControl's SetDefaultCharFormat() function, no matter what I did to it, the font always remained at an 8 point system font (I *was* setting the mask).  Next I tried to use the window's SetFont() function to set the font.  Now here comes the weird part:

When I use the SetFont() function before I use SetDefualtCharFormat() (which I am still using for other details, such as color) AND I have the mask's CFM_SIZE flag set, then SetFont correctly changes the font to what I defined.  However, there are some weird things that happen in this situation.  Mainly, if I use GetDefaultCharFormat() again, the font name is correct in the structure, but the yHeight variable is some huge number (eg. with a 9 point Arial font the yHeight is 105, with 20 point it is 255).  I am not sure if there is a relation between the two at all, but I assume since it changes with it, that there is.

On the other hand, if I do not set the CFM_SIZE flag, or I call SetDefaultCharFormat() with the other changes (color, etc.) the font point seems to be extremely offset by something (9 point font is barely legible, 15 point is about the size 9 should be).

This problem has been bugging me for about a week now, and I can find no answers on newsgroups or other message boards.  Along with this I would like to ask what the formula is (if there is one) for obtaining the size the edit box should be to allow full space for all the chars (plus a couple pixels above and below).  Please help.

- prodos
0
Comment
Question by:prodos
[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
  • 2
3 Comments
 

Author Comment

by:prodos
ID: 1310563
Adjusted points to 150
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 200 total points
ID: 1310564
You shouldn't use SetFont().

This code works fine for me when setting the default character format on a rich edit control:

      CHARFORMAT cfm;
      cfm.cbSize = sizeof(cfm);
      cfm.dwMask = CFM_FACE | CFM_SIZE;
      cfm.yHeight = 240;
      lstrcpy(cfm.szFaceName, "Arial");

      CRichEditCtrl* pCtrl = (CRichEditCtrl*) GetDlgItem(IDC_RICHEDIT1);
      VERIFY(pCtrl->SetDefaultCharFormat(cfm));

The value in yHeight is in twips.  There's 20 twips in a point. 240, which I used above, is 12 points.

Why didn't you look at the WORDPAD sample that ships with VC++, by the way?

You ask two questions, so I guess I have to answer the second one for free: you'll find that setting the window height of the edit control to 1.5 times the cell height of the characters you're using is most pleasing to the eye.  Don't mulitply by 1.5; do something sensible with integers, like this:

   int nHeight = /* initialized someplace else */;
   int nSize = nHeight + nHeight/2;

.B ekiM


0
 

Author Comment

by:prodos
ID: 1310565
Adjusted points to 200
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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