简体   繁体   中英

How to set wxAuiToolBar background?

On wxWidgets3.0.0, I want change wxAuiToolBar background, so I create wxAuiToolBar use wxAUI_TB_PLAIN_BACKGROUND style. However, did not reach the expected results. Example:

// create some toolbars
wxAuiToolBar* tb1 = new wxAuiToolBar(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxAUI_TB_PLAIN_BACKGROUND);
tb1->SetToolBitmapSize(wxSize(48,48));
tb1->AddTool(ID_SampleItem+1, wxT("Test"), wxArtProvider::GetBitmap(wxART_ERROR));
tb1->AddSeparator();
tb1->AddTool(ID_SampleItem+2, wxT("Test"), wxArtProvider::GetBitmap(wxART_QUESTION));
tb1->AddTool(ID_SampleItem+3, wxT("Test"), wxArtProvider::GetBitmap(wxART_INFORMATION));
tb1->AddTool(ID_SampleItem+4, wxT("Test"), wxArtProvider::GetBitmap(wxART_WARNING));
tb1->AddTool(ID_SampleItem+5, wxT("Test"), wxArtProvider::GetBitmap(wxART_MISSING_IMAGE));
tb1->SetCustomOverflowItems(prepend_items, append_items);
tb1->Realize();

First, wxAUI_TB_PLAIN_BACKGROUND does not do anything else than just changing whether the background will be drawn with some fancy gradient or, well, plain. It does not anything to do with transparency, despite what its description in the documentation says.

How can one make the AUI toolbar to be drawn differently then? Use a custom "toolbar art provider". wxAuiToolBar has a method SetArtProvider() , which allows you to set an instance of any class derived from wxAuiToolBarArt . As you can see in its documentation , it has many methods to draw various parts of the tool bar. You can create a custom art provider and do anything differently.

That's not entirely what you want, you want to alter just how the background is painted (to not paint it at all and leave it transparent). So create a custom class which inherits from wxAuiDefaultToolBarArt , which is used by default when you don't set a custom art provider, and override just the required method DrawPlainBackground() (or DrawBackground() - which one of those functions is called is the only thing that "plain background flag" seems to affect):

class MyToolBarArt : public wxAuiDefaultToolBarArt {
  public:
    virtual wxAuiToolBarArt* Clone()
    {
        return new MyToolBarArt(*this);
    }

    virtual void DrawPlainBackground(wxDC& dc, wxWindow* wnd, const wxRect& rect)
    {
        // TODO
    }
};

...

tb1->SetArtProvider(new MyToolBarArtProvider);

If you try it just like this, you will notice the gray background is no longer painted - but the control has black background instead of being transparent! The default black background is left there. We need to use a trick - to make the background transparent, we ask the parent window to paint its contents for us, then let the tool bar continue with the rest of the drawing.

Now this is where it gets ugly. I could not find any way in wxWidgets to ask the parent window to draw itself into a custom DC. So you will need to implement this yourself.

If the parent background consists of just some plain color, you can simply paint the rectangle with the same color. If the background is eg some image, you can draw it there likewise. Similarly with even more complex backgrounds.

In the later cases, it is probably best to accomplish this by a custom method in your parent window (which knows how it paints itself anyway):

class MyParentWindowWithBackground : public wxWindow ... {
  ...
  public:
    void DrawBackground(wxDC& dc, wxRect& rect)
    {
        // TODO: Perform the drawing here...
    }
};

...

    virtual void DrawPlainBackground(wxDC& dc, wxWindow* wnd, const wxRect& rect)
    {
        wxWindow* parent = wnd->GetParent();
        if (parent)
            static_cast<MyParentWindowWithBackground*>(parent)->DrawBackground(dc, rect);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM