How to change underlining color in a Rich Edit control (Win32/C)
Asked Answered
D

3

7

I’m looking for a way to make red squiggly underlining in a Rich Edit control (I’m using version 4.1 with Msftedit.dll). I’m able to produce squiggly underlining with this code :

CHARFORMAT2 format;
format.cbSize = sizeof(format);
format.dwMask = CFM_UNDERLINETYPE;
format.bUnderlineType = CFU_UNDERLINEWAVE;
SendMessage(hWndEdit,EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);

The MSDN documentation doesn’t specify how to change the color of underlines, just the text (with underlines) and the text background. I’ve found some code that says to use the lower nibble for the underline type (CFU_UNDERLINEWAVE) and the upper one for color. So I’ve tried :

format.bUnderlineType = CFU_UNDERLINEWAVE | 0x50;

But that doesn't work.

UPDATE

I've tested this code with version 3.0 (Riched20.dll) and it's working. So the problem lies in 4.1. Was the feature removed or moved elsewhere ?

It's not working in version 6 (the dll used by office 2007) also.

Digital answered 18/11, 2009 at 14:11 Comment(1)
Your original solution worked for me (tested on Windows XP, 7, 8, 8.1, 10). The only downside is you have to use fairly old richedit implementation (namely RICHEDIT20W or some similar version), which may not be an issue for most users.Scheel
C
2

I'm sorry to say this, but if changing the color of the underline is not documented by Microsoft you should not use it. Undocumented featured like this are subject to be removed in later versions, which might have happened here.

Your best bet is to ask Microsoft.

Censor answered 28/11, 2009 at 12:14 Comment(0)
S
5

Expanding on DaveCamp's answer, the CHARFORMAT2W structure contained a bReserved1 entry:

typedef struct _charformat2w
{
    UINT        cbSize;
    DWORD       dwMask;
    DWORD       dwEffects;
    ...
    BYTE        bReserved1;
} CHARFORMAT2W;

But if you look at the latest (8.0) SDK, the bReserved1 entry has now been given to underline color:

typedef struct _charformat2w
{
    UINT        cbSize;
    DWORD       dwMask;
    DWORD       dwEffects;
    ...
#if (_RICHEDIT_VER >= 0x0800)
    BYTE        bUnderlineColor;    // Underline color
#endif
} CHARFORMAT2W;

This is defined as a Widows 8 feature (_RICHEDIT_VER >= 0x0800).

The way to set the underline color is as Dave's answer:

CHARFORMAT2 format;
format.cbSize = sizeof(format);

format.dwMask = CFM_UNDERLINETYPE | CFM_UNDERLINE;
format.dwEffects = CFE_UNDERLINE;
format.bUnderlineType = CFU_UNDERLINEWAVE;
format.bUnderlineColor = 0x05;

SendMessage(hWndEdit,EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);

The remaining trick is the color BYTE values. They're not yet documented, but there are 16 colors:

UnderlineColor_Black =      0x00;
UnderlineColor_Blue =       0x01;
UnderlineColor_Aqua =       0x02;
UnderlineColor_Lime =       0x03;
UnderlineColor_Fuchsia =    0x04;
UnderlineColor_Red =        0x05;
UnderlineColor_Yellow =     0x06;
UnderlineColor_White =      0x07;
UnderlineColor_Navy =       0x08;
UnderlineColor_Teal =       0x09;
UnderlineColor_Green =      0x0A;
UnderlineColor_Purple =     0x0B;
UnderlineColor_Maroon =     0x0C;
UnderlineColor_Olive =      0x0D;
UnderlineColor_DkGray =     0x0E;
UnderlineColor_LtGray =     0x0F;

enter image description here

Edit: Changed the name of the color from Cyan to Aqua. Fixed spelling of Fuchsia.

Note: Any code released into public domain. No attribution required.

Socket answered 22/3, 2014 at 21:16 Comment(1)
@Xam You can find them in my question: UnderlineColor_Yellow = 0x06;Socket
A
3

I know this is digging up an old thread, but I've just searched the 'net for several hours looking for an answer to this only to find similar answers everywhere!

This is in fact documented by Microsoft ( http://msdn.microsoft.com/en-gb/library/windows/desktop/bb787883(v=vs.85).aspx ) and as is very easy to do, ONCE you know how! I've just managed to get it working on Windows7 and Windows8 that use the RichEdit50W control from the msftedit.dll.

One thing to note is that the colour indexes are different in Win8. For RED I have to use color 0x06 as opposed to 0x05.

Ok here's what you need to do:

CHARFORMAT2 format;
format.cbSize = sizeof(format);

format.dwMask = CFM_UNDERLINETYPE | CFM_UNDERLINE;
format.dwEffects = CFE_UNDERLINE;
format.bUnderlineType = CFU_UNDERLINEWAVE;
format.bUnderlineColor = 0x05;

SendMessage(hWndEdit,EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
Afterburning answered 17/12, 2012 at 19:41 Comment(1)
This solution is good for a fairly new version of RichEdit library. However it won't work for the previous ones (2, 3 or (maybe) 4. The comment given by "Honza R" also doesn't work - I just tried it with MSVC 2010 and Win 8.1.Ferroelectric
C
2

I'm sorry to say this, but if changing the color of the underline is not documented by Microsoft you should not use it. Undocumented featured like this are subject to be removed in later versions, which might have happened here.

Your best bet is to ask Microsoft.

Censor answered 28/11, 2009 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.