Hi everyone... I was hoping someone could help me with a little bug I appear to be having with the EM_SETCHARFORMAT message.

I very simply need to write a string to a rich edit control, which is colour formatted in the following way:

<username> message

where username is some colour and message is another colour.

I've been doing this using EM_SETCHARFORMAT with SCF_SELECTION - and it initially appears to work fine ... but after writing maybe 3 - 5 strings (and moving the window around, sizing it etc) ... it sometimes doesn't colour the <username> part ... instead, this is just black ... the message however always seems to be coloured.

I'll paste the code I have made for this:

CHARFORMAT ChatOut;

ChatOut.dwMask = CFM_BOLD | CFM_COLOR;
ChatOut.dwEffects = CFE_BOLD;
ChatOut.cbSize = sizeof(ChatOut);

switch(MessageType)
{

case MESSAGE_TYPE_NORMAL_MESSAGE:

ChatOut.crTextColor = UserNameCR;
SendMessage(hwndChatOutEdit, EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)&ChatOut);

AppendWindowText(hwndChatOutEdit, "<");
AppendWindowText(hwndChatOutEdit, Username);
AppendWindowText(hwndChatOutEdit, "> ");

ChatOut.crTextColor = NormalMessageCR;
SendMessage(hwndChatOutEdit, EM_SETCHARFORMAT, (WPARAM)SCF_SELECTION, (LPARAM)&ChatOut);

AppendWindowText(hwndChatOutEdit, Message);
AppendWindowText(hwndChatOutEdit, "\r\n");

break;

}

UserNameCR and NormalMessageCR are just COLORREF's defined globally.

Username and Message are two string pointers passed to the function where this code belongs and AppendWindowText is defined as:

void AppendWindowText(HWND hWnd, const char * lpString)
{

int iLength = GetWindowTextLength(hWnd);

SendMessage(hWnd, EM_SETSEL, iLength, iLength);
SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM) lpString);
SendMessage(hWnd, WM_VSCROLL, SB_BOTTOM, (LPARAM)NULL);

}

If anyone has any ideas at all, I'd really appreciate some suggestions about what I have done wrong with this thing.

Thanks a lot - Bob