我可以为特定的wx [Aui]“笔记本”标签使用自定义颜色

问题描述

我想用不同的颜色给wxWidgets的每个选项卡上色,就像标记Excel Sheet一样,是否有办法在C ++版本的wxWidgets中使用或不使用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,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上,这种怪兽看起来像这样:

enter image description here

您可以查看来源wxWidgets source for the other tab arts,以查看如何使自己更漂亮的示例。