MFC中如何使用richedit控件_RICHEDIT_VER over 2.1

How to use richedit control _RICHEDIT_VER over 2.1 in MFC

我想在 MFC 中使用 Rich edit 控件的下划线颜色

但是,在afxwin.h、_RICHEDIT_VER中定义0x210。 像这样,

#define _RICHEDIT_VER 0x0210

我正在加载 'msftedit.dll'(8.1 版本)和 Windows10 SDK (10.0.16299.0) 但是,bUnderlineColor 编码为 Richedit.h

#if (_RICHEDIT_VER >= 0x0800)
    BYTE        bUnderlineColor;    // Underline color
#endif

如果我不使用包装 class(CRichEditCtrl),我可以在 MFC 项目中使用它吗? 以及如何?

您可以声明自己的结构并添加bUnderlineColor。在 CRichEdit::SendMessage(EM_SETCHARFORMAT...)

中使用它

虽然这个方法是 hack。或许有更好的办法说服MFC合作

#ifdef UNICODE
struct MY_CHARFORMAT8 : _charformatw //<--- edited
#else
struct MY_CHARFORMAT8 : _charformat
#endif
{
    WORD        wWeight;            // Font weight (LOGFONT value)
    SHORT       sSpacing;           // Amount to space between letters
    COLORREF    crBackColor;        // Background color
    LCID        lcid;               // Locale ID
    union
    {
        DWORD       dwReserved;     // Name up to 5.0
        DWORD       dwCookie;       // Client cookie opaque to RichEdit
    };
    SHORT       sStyle;             // Style handle
    WORD        wKerning;           // Twip size above which to kern char pair
    BYTE        bUnderlineType;     // Underline type
    BYTE        bAnimation;         // Animated text like marching ants
    BYTE        bRevAuthor;         // Revision author index
    BYTE        bUnderlineColor;    // Underline color
};

MY_CHARFORMAT8 format;
memset(&format, sizeof(format), 0);
format.cbSize = sizeof(format);
format.dwMask = CFM_UNDERLINETYPE | CFM_UNDERLINE;
format.dwEffects = CFE_UNDERLINE;
format.crBackColor = RGB(255,0,0);
format.bUnderlineType = CFU_UNDERLINEHAIRLINE;
format.bUnderlineColor = 0x06; //red underline color
m_richedit.SetSel(0, -1);
m_richedit.SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);

需要初始调用 AfxInitRichEdit()

必须使用 Create 手动创建 Rich edit 控件(不使用 SubclassDlgItemDDX_Control),示例:

m_richedit.Create(ES_MULTILINE | WS_VISIBLE | WS_CHILD, rc, this, id);

结果: