如何在MFC中使用版本大于2.1的richedit控件_RICHEDIT_VER?

3
我希望在MFC中使用富文本编辑控件的下划线颜色,但在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

如果我不使用包装类(CRichEditCtrl),我能在MFC项目中使用它吗?如何使用?

这个解决方案适合您吗?https://www.codeguru.com/cpp/controls/richedit/editorsandediting/how-to-use-richeditcontrol-41-in-cricheditview.htm - Flaviu_
1个回答

4

您可以声明自己的结构并添加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()

富文本编辑控件需手动创建,不能使用 SubclassDlgItemDDX_Control,示例:

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

结果:
这是图片描述

感谢您的回答。 - JaeHyeok Kim
需要为 UNICODE 进行单独声明。 - Barmak Shemirani
欢迎 @JaeHyeokKim,我不得不进行更改,请查看编辑。 - Barmak Shemirani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接