c++ - MFC中2.1以上如何使用richedit控件_RICHEDIT_VER

标签 c++ mfc version richedit cricheditctrl

我想在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

如果我不使用包装类(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);

结果:
enter image description here

关于c++ - MFC中2.1以上如何使用richedit控件_RICHEDIT_VER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48332663/

相关文章:

c++ - 无法使用 istream C++ 重载 >> 运算符

c++ - 如何在派生 View 类中实现 CView::OnUpdate

c++ - CSocket 在发送时不阻塞

c++ - 使用宏动态选择版本

version - Nuget安装错误的包版本

c++ - Mountain Lion 上的 Mex 文件 : explicit instantiation error

c++ - 如何向编译器表明函数总是抛出异常?

android - 如何从 ARCore NDK 中的 ArCamera Pose 获取 Matrix4x4?

c++ - CPoint 只保存点 x 和 y 的整数值?备择方案?

android - 如何在以前的版本上测试 apk