c++ - 在 win32 richedit 中更改文本颜色

标签 c++ winapi richedit

我想在 win32 rich edit 控件中显示不同的文本颜色,这是我的测试

#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);
}

行:

rich_edit::append(控制台, RGB(255, 0, 0), "aaaa\n");

本应输出红色字符串,但实际上是黑色字符串,这是为什么?

谢谢。

最佳答案

通过将 dwEffects 设置为 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

并将 SCF_DEFAULT 更改为 SCF_SELECTION

关于c++ - 在 win32 richedit 中更改文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24786430/

相关文章:

c++ - C++中的类型和名称有什么区别?

c++ - 替换并写入文件 C++

c++ - 使用 std::unique_copy 从 vector 中提取唯一的、不区分大小写的字符串

c++ - 对大 vector 进行计时数值运算。如何进行公平比较?

python-3.x - 从窗口获取文本(记事本)

c++ - 打印 : Wrong bottom margin

delphi - 从 RichEdit 获取未格式化的文本

c++ - std::unique_ptr 带有用于 win32 LocalFree 的自定义删除器

c++ - 在 C++ 中将结构保存到剪贴板

c++ - 资源中的 CRichEditCtrl 加载 1.0 而不是 2.0