【问题标题】:How to change the background colour of a wxTextCtrl without breaking the inactive selection colour?如何在不破坏非活动选择颜色的情况下更改 wxTextCtrl 的背景颜色?
【发布时间】:2012-07-05 00:24:37
【问题描述】:

我想在 wxGTK 应用程序中通过将文本字段的背景设置为错误时的红色和成功输入时的白色来指示输入错误。但是,当通过SetBackgroundColor 设置背景颜色时,非活动选择的背景颜色设置为相同 颜色。这会导致将背景颜色设置为白色时出现非常不理想的情况:由于所选文本的前景色为白色,而所选文本的背景颜色现在也为白色,因此文本不可读。

如何重置 wxTextCtrl 上的颜色,以使非活动选定文本具有灰色背景(SetBackgroundColour 之前的默认设置)? SetBackgroundStyle( wxBG_STYLE_SYSTEM) 是我的第一个猜测,但对 wxGTK 没有影响。

代码示例:

#include <wx/textctrl.h>
#include <wx/frame.h>
#include <wx/defs.h>
#include <wx/app.h>

class App : public wxApp {
    bool OnInit() {
        wxFrame* frame = new wxFrame(NULL, wxID_ANY, wxT("Frame"));
        wxTextCtrl* text = new wxTextCtrl( frame, wxID_ANY, wxT("foo bar") );
        text->SetBackgroundStyle( wxBG_STYLE_COLOUR );
        text->SetBackgroundColour( *wxWHITE );
        frame->Show();
        return true;
    }
};

IMPLEMENT_APP( App );

【问题讨论】:

    标签: c++ colors wxwidgets background-color


    【解决方案1】:

    您可以尝试使用 SetDefaultStyle,我自己没有尝试过,但这里是 wxwidgets 文档的摘录:

    text->SetDefaultStyle(wxTextAttr(*wxRED));
    text->AppendText("Red text\n");
    text->SetDefaultStyle(wxTextAttr(wxNullColour, *wxLIGHT_GREY));
    text->AppendText("Red on grey text\n");
    text->SetDefaultStyle(wxTextAttr(*wxBLUE);
    text->AppendText("Blue on grey text\n");
    

    这很可能允许您独立于文本更改颜色和/或更改文本本身的颜色。这是 wxTextCtrl 文档的链接,我在其中找到了这段代码 sn-p: http://docs.wxwidgets.org/2.8/wx_wxtextctrl.html

    问候, 罗因

    【讨论】:

      【解决方案2】:

      试过了,效果很好:

        TextCtrl1->SetBackgroundColour(wxColour(0xFF,0xA0,0xA0));
        TextCtrl1->SetStyle(0, -1, TextCtrl1->GetDefaultStyle());
      

      【讨论】:

        猜你喜欢
        • 2018-03-04
        • 1970-01-01
        • 2020-09-25
        • 2022-10-13
        • 2013-10-19
        • 2017-02-21
        • 1970-01-01
        • 1970-01-01
        • 2013-08-03
        相关资源
        最近更新 更多