我可以为特定的 wx[Aui]Notebook 选项卡使用自定义颜色吗

Can I use custom color for a specific wx[Aui]Notebook tab

我正在尝试用不同的颜色为 wxWidgets 上的每个选项卡着色,就像当您标记 Excel Sheet 时,有没有办法在 wxWidgets 的 C++ 版本中做到这一点,有或没有 AUI?

我认为没有任何东西可以让您开箱即用;但是使用 Aui 笔记本,您可以编写自定义选项卡艺术来为选项卡涂上您认为合适的颜色。这是一个可怕的例子,我只是把它放在一起来演示一种方法来做到这一点:

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include <wx/aui/auibook.h>
#include <map>

class MyTabArt:public wxAuiGenericTabArt
{
public:
    MyTabArt():wxAuiGenericTabArt(){}

    wxAuiTabArt* Clone()
    {
        return new MyTabArt(*this);
    }

    void AddTabColor(wxWindow* w, const wxColor& c)
    {
        m_tabColors[w] = c;
    }

    virtual void DrawTab(wxDC& dc, wxWindow* wnd, const wxAuiNotebookPage& page,
                         const wxRect& rect, int closeButtonState,
                         wxRect* outTabRect, wxRect* outButtonRect,
                         int* xExtent) wxOVERRIDE
    {
        wxSize tabSize = GetTabSize(dc, wnd, page.caption, page.bitmap,
                                    page.active, closeButtonState, xExtent);

        wxCoord tabHeight = m_tabCtrlHeight;
        wxCoord tabWidth = tabSize.x;
        wxCoord tabX = rect.x;
        wxCoord tabY = rect.y + rect.height - tabHeight;

        wxRect tabRect(tabX, tabY, tabWidth, tabHeight);

        wxDCClipper clipper(dc, tabRect);

        auto it = m_tabColors.find(page.window);

        if ( it != m_tabColors.end() )
        {
            wxDCBrushChanger bchanger(dc, it->second);
            wxDCPenChanger pchanger(dc, it->second);
            dc.DrawRectangle(tabRect);
        }
        else
        {
            wxDCBrushChanger bchanger(dc, *wxGREEN);
            wxDCPenChanger pchanger(dc, *wxGREEN);
            dc.DrawRectangle(tabRect);
        }

        dc.DrawText(page.caption,tabRect.x,tabRect.y);

        *outTabRect = tabRect;
    }

private:
    std::map<wxWindow*,wxColor> m_tabColors;
};

class MyFrame: public wxFrame
{
    public:
        MyFrame();

    private:
};

MyFrame::MyFrame()
        :wxFrame(NULL, wxID_ANY, "AUI Tab", wxDefaultPosition, wxSize(600, 400))
{
    wxAuiNotebook * auiNotebook =
        new wxAuiNotebook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 );
    wxPanel* panel1 = new wxPanel( auiNotebook, wxID_ANY );
    wxPanel* panel2 = new wxPanel( auiNotebook, wxID_ANY );
    auiNotebook->AddPage(panel1, "Page 1");
    auiNotebook->AddPage(panel2, "Page 2");

    MyTabArt* art = new MyTabArt();
    art->AddTabColor(panel1, *wxRED);
    art->AddTabColor(panel2, *wxBLUE);

    auiNotebook->SetArtProvider(art);
}

class MyApp : public wxApp
{
    public:
        virtual bool OnInit()
        {
            ::wxInitAllImageHandlers();
            MyFrame* frame = new MyFrame();
            frame->Show();
            return true;
        }
};

wxIMPLEMENT_APP(MyApp);

在 windows,这个怪物看起来像这样:

您可以查看源代码 wxWidgets source for the other tab arts 以查看如何使它更漂亮的示例。