Johnny Mnemonic
Johnny Mnemonic

Reputation: 3912

Add formatting to Richtext box

I have a rich edit control and I want to add formatted text in it but it doesn't work:

const char* str="{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard\
This is some {\b bold} text.\par\
}";
    LoadLibrary("riched32.dll");

    HWND hEdit= CreateWindowEx(WS_EX_CLIENTEDGE, RICHEDIT_CLASS, str,
        WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL 
| ES_READONLY, 0, 0, 100, 100,
        hwnd, (HMENU) IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);

I know I can add formatting using PARAFORMAT and CHARFORMAT and the sending a message to the control like this:

PARAFORMAT pf;
CHARFORMAT cf;

memset( &cf, 0, sizeof cf );
cf.cbSize = sizeof cf;
cf.dwMask = CFM_BOLD;
cf.dwEffects = CFE_BOLD;
SendMessage( hEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
SendMessage( hEdit, EM_REPLACESEL, FALSE, (LPARAM) "bold ");

But how can I make the first one work or if there is no way, is there a simpler method than the second? Thanks in advance.

Upvotes: 1

Views: 572

Answers (1)

user31394
user31394

Reputation:

Rich edit controls won't accept RTF in the way you're trying in the first code block. To get RTF into the control you need to use the ES_STREAMIN message.

Upvotes: 1

Related Questions