Solved

Font problems in a CRichEditCtrl

Posted on 1997-11-22
3
1,086 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
  • 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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…

759 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

26 Experts available now in Live!

Get 1:1 Help Now