Valhalla Legends Forums Archive | C/C++ Programming | RichEdit Color Bug

AuthorMessageTime
Spilled[DW]
Hi, I wrote a class to support color in my RichEdit and it works for about 3 lines then all text is printed in Black. Here is some of my Code:

[code]
void QueueShit::SetColor(int i)
{
                CHARFORMAT m;
               
                SendMessage( theRTB, EM_GETCHARFORMAT, 0, (LPARAM)&m);
             
                m.cbSize = sizeof( CHARFORMAT);
                m.dwMask = CFM_COLOR;
            switch(i)
            {
                    case cGreen: m.crTextColor = RGB(0,255,127); break;
                    case cRed: m.crTextColor = RGB(255,0,0); break;
                    case cBlack: m.crTextColor = RGB(0,0,0); break;
                    case cBlue: m.crTextColor = RGB(0,0,255); break;
                    case cBrown: m.crTextColor = RGB(139,69,19); break;
                    case cGrey: m.crTextColor = RGB(190,190,190); break;
                    case cPurple: m.crTextColor = RGB(160,32,240); break;
                    case cCyan: m.crTextColor = RGB(0,255,255); break;
                    case cWhite: m.crTextColor = RGB(255,255,255); break;
                    case cYellow: m.crTextColor = RGB(255,255,0); break;
                    default: m.crTextColor = RGB( 255, 255, 255);
            }
             
            SendMessage(theRTB, EM_SETCHARFORMAT, 1, (LPARAM)&m);   
}
QueueShit &QueueShit::operator<<(const char *msg)
      {
            int iLength = GetWindowTextLength(theRTB);
        SendMessage(theRTB, EM_SETSEL, iLength, iLength);
        SendMessage(theRTB, EM_REPLACESEL, 0, (LPARAM)msg);
        SendMessage(theRTB, WM_VSCROLL, SB_BOTTOM, (LPARAM)NULL);
        delete msg;
      }
QueueShit &QueueShit::operator<<(int i)
{
              SetColor(i);
}
void QueueShit::SetRTB(HWND f)
{
              theRTB = f;
}
[/code]

After the RichEdit is created, I use the SetRTB(HWND) Method to set the richedit handle then i use the operators << to control the color and msg's added. It works for the first few times then all text goes black but is still added so im thinking there is a bug in the SetColor() method. Since the RichEdit is MultiLine I am processing WM_CTLCOLOREDIT like I read but im not sure if I'm doing that right either...

[code]
        case WM_CTLCOLOREDIT:
            {
                if ((HWND)lParam == GetDlgItem(hwnd, RICHEDIT))
                {
                // Set the text background color.
                SetBkColor((HDC)wParam, RGB(255,255,255));
                return (BOOL)GetStockObject(LTGRAY_BRUSH);
                }
                else
                return (BOOL)GetStockObject(LTGRAY_BRUSH);
            }
[/code]

Any ideas? Thanks guys!

Spilled
September 4, 2006, 2:28 AM
Spilled[DW]
Sorry about double posting and bring up an old topic but problem was solved. In my SetColor(int) method i wasn't using ZeroMemory() on the CHARFORMAT.

[code]
ZeroMemory(&m, sizeof(CHARFORMAT));
[/code]

Once again sorry about the double post but thought someone later on down the road would use this information and would need the solution.
September 16, 2006, 4:39 AM

Search