Search code examples
c++mfcversionricheditcricheditctrl

How to use richedit control _RICHEDIT_VER over 2.1 in MFC


I'd like to use Rich edit control's underline color in MFC

but, in afxwin.h, _RICHEDIT_VER define 0x210. like this,

#define _RICHEDIT_VER 0x0210

I'm loading 'msftedit.dll'(8.1 Version) and Windows10 SDK (10.0.16299.0) but, bUnderlineColor is coded in Richedit.h

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

If I don't use wrapping class(CRichEditCtrl), Can I use this in MFC project. and How?


Solution

  • You can declare your own structure and add bUnderlineColor. Use this in CRichEdit::SendMessage(EM_SETCHARFORMAT...)

    This method is hack though. Maybe there is a better way to convince MFC to cooperate.

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

    Requires initial call to AfxInitRichEdit()

    Rich edit control has to be created manually with Create (not using SubclassDlgItem or DDX_Control), example:

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

    Result:
    enter image description here