如何在winapi中正确设置文本颜色

问题描述

我将句柄包裹在一个小部件类中,以便前景色,背景色和光标随处可见。

我在弄清楚如何设置静态控件的前景色(文本)方面遇到很多麻烦。我可以更改前景色,但是背景色与“窗口”颜色不匹配,或者“窗口”颜色正确,但文本颜色不匹配。

这里是我所拥有的,该控件是问题所在,是一个静态控件,但是窗口上也有编辑控件:

case WM_CTLCOLOREDIT:
case WM_CTLCOLORSTATIC:
    widget = Widget::find((HWND)lParam); // find a widget from handle
    if (widget)
    {
        COLORREF color = widget->color();
        HBrush bg = widget->background(); // may be nullptr if none set (use default window bg)

        SetBkMode((HDC)wParam,TRANSPARENT);
        SetTextColor((HDC)wParam,color);

        // try 1: always return the background,even if nullptr
        // result: correct foreground color,incorrect background color (white)
        return (INT_PTR)bg;

        // try 2: only return the background if set,else default to DefWindowProcW(...)
        // result: incorrect foreground color (black),correct background color
        if (bg) return (INT_PTR)bg;
        break;

        // try 3: if no background,ask for parent's background
        // result: both foreground and background are correct,//         however,every control has the same background color (the main window's),//         but some controls I want to have their default
        //         (i.e. edit controls have a white background by default)
        while (!bg && widget)
        {
            widget = widget->parent();
            if (widget) bg = widget->background();
        }
        return (INT_PTR)bg;
    }

是否可以在不设置背景的情况下设置前景色(文本)?我并不总是知道背景颜色是什么(当background()返回nullptr时),并想使用控件的认颜色。

或者,我是采用完全错误方法吗?

我是Win32 API和GUI编程的新手。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)