如何更改 Rich Edit 控件中的下划线颜色 (Win32/C)

发布于 2024-08-12 08:56:06 字数 673 浏览 8 评论 0原文

我正在寻找一种在 Rich Edit 控件中制作红色波浪下划线的方法(我正在使用带有 Msftedit.dll 的版本 4.1)。我可以使用此代码生成波浪形下划线:

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

MSDN 文档没有指定如何更改下划线的颜色,仅指定文本(带下划线)和文本背景。我发现一些代码表示使用下半字节作为下划线类型(CFU_UNDERLINEWAVE),使用上半字节作为颜色。所以我尝试过:

format.bUnderlineType = CFU_UNDERLINEWAVE | 0x50;

但这不起作用。

更新

我已经使用 3.0 版 (Riched20.dll) 测试了此代码,并且它可以正常工作。所以问题出在4.1。该功能是否被删除或移至其他地方?

它在版本 6(office 2007 使用的 dll)中也不起作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

超可爱的懒熊 2024-08-19 08:56:06

扩展 DaveCamp 的答案,CHARFORMAT2W 结构包含一个 bReserved1 条目:

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

但是如果您查看最新的 (8.0) SDK,bReserved1 条目具有现在已赋予下划线颜色

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

这被定义为 Widows 8 功能 (_RICHEDIT_VER >= 0x0800)。

设置下划线颜色的方法如戴夫的回答:

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);

剩下的技巧是颜色BYTE值。它们尚未记录,但有 16 种颜色:

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;

在此处输入图像描述

编辑:将颜色名称从 Cyan 更改为 Aqua。修复了 Fuchsia 的拼写。

注意:发布到公共领域的任何代码。无需归属。

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.

电影里的梦 2024-08-19 08:56:06

我知道这是挖掘一个旧线程,但我刚刚在网上搜索了几个小时,寻找这个问题的答案,却发现到处都是类似的答案!

这实际上是由 Microsoft 记录的 ( http://msdn.microsoft.com/en-gb/library/windows/desktop/bb787883(v=vs.85).aspx )并且非常容易做到,一旦您知道如何!我刚刚设法让它在使用 msftedit.dll 中的 RichEdit50W 控件的 Windows7 和 Windows8 上运行。

需要注意的一点是,Win8 中的颜色索引有所不同。对于红色,我必须使用颜色 0x06,而不是 0x05。

好的,这就是你需要做的:

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);

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);
晌融 2024-08-19 08:56:06

很抱歉这么说,但是如果 Microsoft 没有记录更改下划线的颜色,那么您不应该使用它。像这样未记录的功能可能会在以后的版本中被删除,这可能已经发生在这里。

最好的办法是询问微软。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文