Solved

change Font  Size & Color of Rich Edit during runtime

Posted on 1998-02-18
6
693 Views
Last Modified: 2013-12-03
I want to develop a dialog based application with an rich edit control, whose Font Size and Color can be changed by pressing Buttons. I use the CHARFORMAT, SetDefaultCharFormat, SetSelCharFormat, and a custom function:
Format(CHARFORMAT &cf)
{
 cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_BOLD|CFM_COLOR;
cf.dwEffects =...
cf.yHeight = ...
.
.
}

After complier it, there appears a big "I" when the rich edit get focus. Moreover, the editor does not accept any input. What's wrong with it? Or someone can tell me another way to do the task?
0
Comment
Question by:erin021898
  • 3
  • 2
6 Comments
 

Expert Comment

by:akamal
ID: 1316331
You can use CFontDialog for font options as user click the button, here is the full code:
CFontDialog fntdlg;
if(fntdlg.DoModal()==IDOK)
{
TEXTMETRIC tm;
CClientDC dc(this);
GetTextMetrics(dc.m_hDC,&tm);
CHARFORMAT cf;
cf.cbSize=sizeof(CHARFORMAT);
cf.dwMask=CFM_BOLD|CFM_COLOR|CFM_FACE|CFM_ITALIC|CFM_SIZE|CFM_STRIKEOUT|CFM_UNDERLINE;
cf.dwEffects=0;
if(fntdlg.IsBold())cf.dwEffects=cf.dwEffects|CFE_BOLD;
if(fntdlg.IsItalic())cf.dwEffects=cf.dwEffects|CFE_ITALIC;
if(fntdlg.IsUnderline())cf.dwEffects=cf.dwEffects|CFE_UNDERLINE;
if(fntdlg.IsStrikeOut())cf.dwEffects=cf.dwEffects|CFE_STRIKEOUT;
cf.yHeight=tm.tmHeight*fntdlg.GetSize()/8;
cf.crTextColor=fntdlg.GetColor();
cf.bPitchAndFamily=fntdlg.m_cf.lpLogFont->lfPitchAndFamily;
cf.szFaceName[LF_FACESIZE]=fntdlg.m_cf.lpLogFont->lfFaceName[LF_FACESIZE];
SetCharFormat(cf);
}

I have used this code in one of my projects and it worked.
Ahmed Kamal
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 1316332
Note for both of ya:  instead of typing all that code, note that there is an undocumented CFontDialog constructor that takes a CHARFORMAT structure as its first parameter.  It is prototyped like this:

CFontDialog( const CHARFORMAT& charformat, DWORD dwFlags = CF_SCREENFONTS, CDC* pdcPrinter = NULL, CWnd* pParentWnd = NULL );

   Then use the undocumented CFontDialog::GetCharFormat(...) to get the user selected CHARFORMAT.

The quickest way to do it is to the following steps (Error checking left out):

   GetSelectionCharFormat( m_cfFormat );
   pFontDlg = new CFontDialog( m_cfFormat );
   if( pFontDlg -> DoModal() == IDOK )
   {
      pFontDlg -> GetCharFormat( m_cfFormat );
      SetSelectionCharFormat( m_cfFormat );
   }

   This will NOT work with RichEdit2.0 (CHARFORMAT2 structure).

-=- James.
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 1316333
Almost forgot...  If you want to have pre-set CHARFORMAT structures to apply formats with, create a little app that uses the CFontDialog stuff above, and when you get the CHARFORMAT that you want, store it someplace (file, registry, etc), and then you can use them in your application.  No need for the user to select something.

-=- James.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:erin021898
ID: 1316334
I want to "visual" change the char in the edit box. When the user click the button, the user can see the change at the same time.
0
 

Expert Comment

by:akamal
ID: 1316335
You haven't specified your request clearly. I cannot answer a question whose owner isn't making it enough clear. May be next time I will try to help you, but this time, sorry.
Ahmed
0
 
LVL 4

Accepted Solution

by:
jtwine100697 earned 100 total points
ID: 1316336
Lemme get this straight...  You want effects similar to this:

   The user has two buttons, labeld [Big] and [Small].  When the user clicks [Big], you want the Font/Size/Color to change to the "Big" state, and when the user hits [Small], you want the Font/Size/Color to change to the "Small" state, right?

   Simple:  Pre-set two (or more) CHARFORMAT structures, store them in the registry, or right in the program, select all text in the control using "SetSel( 0, -1 )", and then apply them using "SetSelectionCharFormat( m_cfBigCharFormat )" and/or "SetSelectionCharFormat( m_cfSmallCharFormat )".

-=- James.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 42
conditional code and condition difference 9 82
True Type Font Files? 4 89
how to split multiple lines delimiter : 8 99
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 …
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.
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…

773 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