change text color in win32 richedit

风格不统一 提交于 2019-12-10 11:18:33

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/24786430/change-text-color-in-win32-richedit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!