Solved

Richedit and Colors

Posted on 1998-04-16
10
699 Views
Last Modified: 2008-02-01
I am trying to use a Richedit Control with colored text . I have used colors for the text , using the CHARFORMAT structure , but I do not know how to set the background color of a chat . Warning : Not the background of the whole control
0
Comment
Question by:WxW
  • 5
  • 4
10 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1184343
I suspect that is not possible.  I don't know much about richedit controls.  But it is not possible (that I know of--and I have looked) to set the background color in Microsoft Word (except the entire background)  And I doubt that richedit controls would have features that Word doesn't.
0
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1184344
You can set the background color of a character with Rich Edit 2.0.

The CHARFORMAT2 structure contains information about character formatting in a rich edit control. CHARFORMAT2 is a Rich Edit version 2.0 extension of the CHARFORMAT structure. Rich Edit 2.0 allows you to use either structure with the EM_GETCHARFORMAT and EM_SETCHARFORMAT messages.

typedef struct _charformat2 {
    UINT        cbSize;
    _WPAD       _wPad1;
    DWORD       dwMask;
    DWORD       dwEffects;
    LONG        yHeight;
    LONG        yOffset;
    COLORREF    crTextColor;
    BYTE        bCharSet;
    BYTE        bPitchAndFamily;
    WCHAR       szFaceName[LF_FACESIZE];
    _WPAD       _wPad2;
    WORD        wWeight;
    SHORT       sSpacing;
    COLORREF    crBackColor;
    LCID        lcid;
    DWORD       dwReserved;
    SHORT       sStyle;
    WORD        wKerning;
    BYTE        bUnderlineType;
    BYTE        bAnimation;
    BYTE        bRevAuthor;
    BYTE        bReserved1;
} CHARFORMAT2;

0
 
LVL 6

Author Comment

by:WxW
ID: 1184345
Yes and why this code won't work ?

CharFormat.cbSize = sizeof(CharFormat);
::SendMessage(hW,EM_GETCHARFORMAT,false,(LPARAM)&CharFormat);
CharFormat.cbSize = sizeof(CharFormat);
ChararFormat.dwMask |= CFM_COLOR;
CharFormat.dwEffects &= ~(CFE_AUTOCOLOR);
CharFormat.yHeight = 0;
CharFormat.yOffset = 0;
CharFormat.crTextColor = GetRGB(TheColor1);
CharFormat.bCharSet = 0;
CharFormat.bPitchAndFamily = 0;
CharFormat.wWeight = 0;
CharFormat.sSpacing = 0;
CharFormat.crBackColor = GetRGB(TheColor2);
CharFormat.lcid = 0;
CharFormat.dwReserved = 0;
CharFormat.sStyle = 0;
CharFormat.wKerning = 0;
CharFormat.bUnderlineType = 0;
CharFormat.bAnimation = 0;
CharFormat.bRevAuthor = 0;
::SendMessage(hW,EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&CharFormat);

Note the hW = richedit handle , GetRGB just a macro returning COLORREF values

0
Technology Partners: 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!

 
LVL 23

Expert Comment

by:chensu
ID: 1184346
What do you mean by "this code won't work"? Compiling error or something else?

Make sure you are using CHARFORMAT2 and cbSize is sizeof(CHARFORMAT2).

0
 
LVL 6

Author Comment

by:WxW
ID: 1184347
I just mean that there are no compiling errors , but the Edit Control does not accept and recognize my color values . It does not recognize not only the BackGround color , but also the ForeGround . I believe that my code lacks a statement , but I do not know what
0
 
LVL 23

Expert Comment

by:chensu
ID: 1184348
Are you using Rich Edit 2.0 (RichEd20.DLL)?
0
 
LVL 6

Author Comment

by:WxW
ID: 1184349
I discovered that I had not created a "Richedit20A" class , but a RichEdit 1.0 . However I have another problem now : The RichEdit I create seems inactive , and no-messages processing . Is there any special call I must use except the standard RichEdit 1.0 functions and messages ?
0
 
LVL 23

Expert Comment

by:chensu
ID: 1184350
Did you call the LoadLibrary function to load the RICHED20.DLL before creating a rich edit control?
0
 
LVL 6

Author Comment

by:WxW
ID: 1184351
Thanks chensu . That was the problem . I have read also in the MSDN . If you can solve me the two more problems .... why the EM_SETSEL -1,-1 does not do the same like RichEdit 1.0 , but rather selects all the text . And b) , I want no-horizontal scroll bar . I want line breaking .Thanks a lot !!
0
 
LVL 23

Expert Comment

by:chensu
ID: 1184352
1. Do you send the EM_SETSEL message with wParam == -1 and lParam == -1? If so, I don't know why. According to the documentation, "If nStart is –1, any current selection is removed.".

2. Remove the WS_HSCROLL style of the Rich Edit control.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

713 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