Solved

Font problems in a CRichEditCtrl

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wait notify demo infinite loop 3 148
changeXy challenge 13 97
I could not set window to top 4 62
How do I reset a theme that I fooled around with and changed? 3 48
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. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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…

732 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