简体   繁体   中英

change text color in win32 richedit

I want to show different text colors in win32 rich edit control, here' my test

#include <windows.h>
#include <richedit.h>
#include <commctrl.h>

HWND console;

// util function for rich edit
namespace rich_edit {
    CHARFORMAT get_char_fmt(HWND hwnd, DWORD range = SCF_DEFAULT) {
        CHARFORMAT cf;
        SendMessage(hwnd, EM_GETCHARFORMAT, range, (LPARAM)&cf);
        return cf;
    }
    void set_char_fmt(HWND hwnd, const CHARFORMAT& cf, DWORD range = SCF_DEFAULT) {
        SendMessage(hwnd, EM_SETCHARFORMAT, range, (LPARAM)&cf);
    }
    void replace_sel(HWND hwnd, const char* str) {
        SendMessage(hwnd, EM_REPLACESEL, 0, (LPARAM)str);
    }
    void cursor_to_bottom(HWND hwnd) {
        SendMessage(hwnd, EM_SETSEL, -2, -1);
    }
    void scroll_to(HWND hwnd, DWORD pos) {
        SendMessage(hwnd, WM_VSCROLL, pos, 0);
    }
    void scroll_to_bottom(HWND hwnd) {
        scroll_to(hwnd, SB_BOTTOM);
    }
    // this function is used to output text in different color
    void append(HWND hwnd, COLORREF clr, const char* str) {
        cursor_to_bottom(hwnd); // move cursor to bottom

        CHARFORMAT cf = get_char_fmt(hwnd); // get default char format
        cf.cbSize = sizeof(cf);
        cf.dwMask = CFM_COLOR; // change color
        cf.crTextColor = clr; 

        set_char_fmt(hwnd, cf); // set default char format

        replace_sel(hwnd, str); // code from google
        scroll_to_bottom(hwnd); // scroll to bottom
    }
}

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int main() {

    LoadLibrary("riched20.dll"); // for using rich edit


    static char szAppName[] = "winhello";
    HWND        hwnd;
    MSG         msg;
    WNDCLASSEX  wndclass;

    wndclass.cbSize         = sizeof(wndclass);
    wndclass.style          = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc    = WndProc;
    wndclass.cbClsExtra     = 0;
    wndclass.cbWndExtra     = 0;
    wndclass.hInstance      = GetModuleHandle(0);
    wndclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground  = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wndclass.lpszClassName  = szAppName;
    wndclass.lpszMenuName   = NULL;

    RegisterClassEx(&wndclass);

    hwnd = CreateWindow(szAppName, "Hello, world!",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT,
            400, 300,
            NULL, NULL, GetModuleHandle(0), NULL);

    ShowWindow(hwnd, SW_SHOW);
    UpdateWindow(hwnd);

    while ( GetMessage(&msg, NULL, 0, 0) ) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    } 
    return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {
    switch ( iMsg ) {
    case WM_CREATE:
        console = CreateWindow(RICHEDIT_CLASS, "",
                               WS_CHILD | ES_SAVESEL | ES_NOHIDESEL | WS_CHILDWINDOW | WS_BORDER | WS_VISIBLE | ES_MULTILINE | WS_VSCROLL | WS_EX_STATICEDGE,
                               0, 0, 300, 200, hwnd, 0, GetModuleHandle(0), 0);

        // output a red string
        rich_edit::append(console, RGB(255, 0, 0), "aaaa\n");

        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

The line:

rich_edit::append(console, RGB(255, 0, 0), "aaaa\\n");

is supposed to output a red string, but actually it's black, why is that?

Thanks.

Problem solved by setting the dwEffects to 0:

    void append(HWND hwnd, COLORREF clr, const char* str) {
        CHARFORMAT cf = get_char_fmt(hwnd);
        cf.cbSize = sizeof(cf);
        cf.dwMask = CFM_COLOR;
        cf.dwEffects = 0; // add this line

and change SCF_DEFAULT to SCF_SELECTION

http://win32assembly.programminghorizon.com/tut33.html

There's a bit on Setting default text and background color, that should help.

Edit:

Ah ok, sorry I'm new to this site, so I didn't realize I should also give an explanation.

Essentially, what you did below will make it work again. You need SCF_SELECTION because that will tell the RichEdit to format the text with the CHARFORMAT object with whatever you want inputted next or whatever you selected using SetSel (this means you can also redraw text already in the RichEdit by using the SetSel member). dwEffect is for things like bold, italics, etc. This also needs to be set - by setting it to 0 you just get regular text. If you wish to use dwEffect make sure you set dwMask properly. If you need things explained in better detail, just use the link or this one: http://msdn.microsoft.com/en-us/library/windows/desktop/bb787881(v=vs.85).aspx

Try this:

HWND hRicheditControl;

void txtBold(HWND hWindow) {
    CHARFORMAT boldfont;
    boldfont.cbSize = sizeof(CHARFORMAT);
    boldfont.dwMask = CFM_BOLD;
    boldfont.dwEffects = CFE_BOLD;
    SendMessage(hWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&boldfont);
}
void txtUnderlined(HWND hWindow) {
    CHARFORMAT2 underlinedfont;
    underlinedfont.cbSize = sizeof(CHARFORMAT);
    underlinedfont.dwMask = CFM_UNDERLINE;
    underlinedfont.dwEffects = CFM_UNDERLINE;

    SendMessage(hWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&underlinedfont);
}
void txtItalic(HWND hWindow) {
    CHARFORMAT Kursivfont;
    Kursivfont.cbSize = sizeof(CHARFORMAT);
    Kursivfont.dwMask = CFM_ITALIC;
    Kursivfont.dwEffects = CFM_ITALIC;
    SendMessage(hWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&Kursivfont);
}
void txtStrikeout(HWND hWindow) {
    CHARFORMAT underlinedfont;
    underlinedfont.cbSize = sizeof(CHARFORMAT);
    underlinedfont.dwMask = CFM_STRIKEOUT;
    underlinedfont.dwEffects = CFM_STRIKEOUT;
    SendMessage(hWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&underlinedfont);
}
void Subscript(HWND hWindow) {
    CHARFORMAT2 cf;
    cf.cbSize = sizeof(cf);
    cf.dwMask = CFE_SUBSCRIPT;
    cf.dwEffects = CFE_SUBSCRIPT;
    SendMessage(hWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
}

void Superscript(HWND hWindow) {
    CHARFORMAT2 cf;
    cf.cbSize = sizeof(cf);
    cf.dwMask = CFM_SUPERSCRIPT;
    cf.dwEffects = CFM_SUPERSCRIPT;
    SendMessage(hWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
}
void SetFont(HWND hWindow, const char * Font) {
    CHARFORMAT2 cf;
    memset(&cf, 0, sizeof cf);
    cf.cbSize = sizeof cf;
    cf.dwMask = CFM_FACE;
    wsprintf(cf.szFaceName, Font);
    SendMessage(hWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
}

void FontSize(HWND hWindow, int size) {
    CHARFORMAT2 cf;
    memset(&cf, 0, sizeof cf);
    cf.cbSize = sizeof cf;
    cf.dwMask = CFM_SIZE;
    cf.yHeight = size * 20;
    SendMessage(hWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
}
void txtColor(HWND hWindow, COLORREF clr) {
    CHARFORMAT cf;
    cf.cbSize = sizeof(cf);
    cf.dwMask = CFM_COLOR;
    cf.crTextColor = clr;
    cf.dwEffects = 0;
    SendMessage(hWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
}

void txtBackColor(HWND hWindow, COLORREF clr) {
    CHARFORMAT2 cf;
    cf.cbSize = sizeof(cf);
    cf.dwMask = CFM_BACKCOLOR;
    cf.crBackColor = clr;
    cf.dwEffects = 0;
    SendMessage(hWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
}

Using:

txtBold(hRicheditControl); //for bold-text
txtUnderlined(hRicheditControl); //for underlined-text
txtItalic(hRicheditControl); //for itaic-text
txtSrikeout(hRicheditControl); //for strikeout-text
Subscript(hRicheditControl); //for Sub-text
Superscript(hRicheditControl); //for Super-text
SetFont(hRicheditControl, "Arial"); //define the fontname in the ""
FontSize(hRicheditControl, 32); //set the fontsize as int
txtColor(hRicheditControl, RGB(255,0,0)); //set the text to red
txtBackColor(hRicheditControl, RGB(0,255,0)); //Set the textbackgroundcolor to green

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM