Own, transparent wxFrame background

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
pompex
In need of some credit
In need of some credit
Posts: 2
Joined: Fri Jun 05, 2015 1:24 am

Own, transparent wxFrame background

Post by pompex » Fri Jun 05, 2015 1:55 am

I decided to use wxWidgets for the very first time to develop my file-patcher application GUI.

What you need to know is:
  • I want to use completely different GUI style, and not the Windows one,
  • Whole UI is based on the external PNG images
This is my base GUI background preview:

Image

As you can see, the gray colour should be actually transparent.

Using:

Code: Select all

SetBackgroundColour(wxTransparentColour);
Doesn't even works, since the gray color just turing black:

Image

Unfortunately, using:

Code: Select all

SetTransparent(0);
Won't help neither, since it will apply to the parent & all childrens - in the results, the whole GUI is invisible.

I've been also trying to use the paint event which doesn't worked neither.

So, is there any solution for this, so I could the images on my GUI transparent (not fully transparent, just the places they are gray/black)?

User avatar
tierra
Site Admin
Site Admin
Posts: 1342
Joined: Sun Aug 29, 2004 7:14 pm
Location: Salt Lake City, Utah, USA
Contact:

Re: Own, transparent wxFrame background

Post by tierra » Fri Jun 05, 2015 3:31 am

pompex wrote:What you need to know is:
  • I want to use completely different GUI style, and not the Windows one,
  • Whole UI is based on the external PNG images
Pretty much the most important goal of wxWidgets is to offer native user interface components in a cross-platform manner, and these two requirements are entirely contrary to that goal. As much as I don't like saying this, it's very likely that this is not the ideal library you should be building your application on top of.

That said, it has been done before still, but it's just going to involve a lot more work than you'd probably like.
pompex wrote:So, is there any solution for this, so I could the images on my GUI transparent (not fully transparent, just the places they are gray/black)?
Typically, I believe this is ideally done using a "shaped frame", but note that shaped frames don't support any kind of alpha transparency, it's all just a mask, so you'll likely run into more problems if you expect your window to be anti-aliased. See this for more info: http://docs.wxwidgets.org/trunk/classwx ... 0b55e89613

pompex
In need of some credit
In need of some credit
Posts: 2
Joined: Fri Jun 05, 2015 1:24 am

Re: Own, transparent wxFrame background

Post by pompex » Fri Jun 05, 2015 12:14 pm

Thanks for your reply.

Could you recommend any library that would be suitable for my needs?

Too bad that wxWidgets wouldn't be an ideal library for this, I really liked to code with it as the documentation was very nice and clear.

joecarl
Knows some wx things
Knows some wx things
Posts: 36
Joined: Sat Jul 16, 2011 1:05 pm

Re: Own, transparent wxFrame background

Post by joecarl » Tue Jun 23, 2015 5:13 pm

Hi. I'm having this problem too. And I would be really interested in finding another library which is able to do this. Any suggestion? Please.

Thanks in advance,

Joe.

User avatar
doublemax
Moderator
Moderator
Posts: 14509
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Own, transparent wxFrame background

Post by doublemax » Tue Jun 23, 2015 5:27 pm

The only real alternative that covers pretty much everything is Qt.

https://en.wikipedia.org/wiki/List_of_widget_toolkits
As i don't know any of them well enough, i can't tell you which one is most suitable for your needs.
Use the source, Luke!

User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 429
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Re: Own, transparent wxFrame background

Post by xaviou » Tue Jun 23, 2015 6:14 pm

Hi
pompex wrote:Too bad that wxWidgets wouldn't be an ideal library for this, I really liked to code with it as the documentation was very nice and clear.
I think that wxWidgets can do what you want.
What tierra said is that your window can have a custom shape (the one defined by your image file for example), but you won't be able to make this shape "not fully opaque" (semi transparent).

To apply your shape, you'll have to create a wxRegion witrh a black and while image (for example, the white pixels will be transparents, and the white ones will be visibles).

You'll also have to create your wxFrame with the "SHAPED" style and apply the wxRegion with "SetShape".
Then, you'll be able to draw your final image on your wxFrame.

Regards
Xav'
My wxWidgets stuff web page : [email protected]'s wxStuff

User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 429
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Re: Own, transparent wxFrame background

Post by xaviou » Tue Jun 23, 2015 6:55 pm

Here if the result (quickly) obtained with your images :
Image

Here is the code :
mainframe.h

Code: Select all

class MainFrame: public wxFrame
{
    public:
        MainFrame();
        ~MainFrame();
    private:
        void CreateShapedFrame();
        void OnDraw(wxPaintEvent &event);
        wxBitmap m_bmpBackground;
};
mainframe.cpp:

Code: Select all

MainFrame::MainFrame() : wxFrame(NULL, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxFRAME_SHAPED|wxCLIP_CHILDREN)
{
    CreateShapedFrame();

    Connect(wxEVT_PAINT, wxPaintEventHandler(MainFrame::OnDraw));

    CentreOnScreen();
}


MainFrame::~MainFrame()
{
    //
}

void MainFrame::CreateShapedFrame()
{
    wxBitmap bmpMask;
    bmpMask.LoadFile(_T("mask.png"), wxBITMAP_TYPE_PNG);

    m_bmpBackground.LoadFile(_T("skin.png"), wxBITMAP_TYPE_PNG);
    SetClientSize(m_bmpBackground.GetSize());

    wxRegion rgn(bmpMask, *wxBLACK);

    SetShape(rgn);
}

void MainFrame::OnDraw(wxPaintEvent& event)
{
    wxPaintDC dc(this);

    dc.DrawBitmap(m_bmpBackground, wxPoint(-1, -1));
}
Hope it'll help.

Regards
Xav'
My wxWidgets stuff web page : [email protected]'s wxStuff

DavidKlecker
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 232
Joined: Sun Nov 29, 2009 10:35 am

Re: Own, transparent wxFrame background

Post by DavidKlecker » Tue Jan 17, 2017 1:29 pm

Does this work on all three operating systems: MSW, OSX and Unix (Linux)? Cause this also answers a question about using a "custom" frame on a window.

User avatar
doublemax
Moderator
Moderator
Posts: 14509
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Own, transparent wxFrame background

Post by doublemax » Tue Jan 17, 2017 2:27 pm

Does this work on all three operating systems: MSW, OSX and Unix (Linux)?
Yes, but beware that you don't get per-pixel alpha, it's just a 1bit mask.
Use the source, Luke!

DavidKlecker
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 232
Joined: Sun Nov 29, 2009 10:35 am

Re: Own, transparent wxFrame background

Post by DavidKlecker » Wed Jan 18, 2017 1:33 pm

One more question. Let's say I have a wxShape window that just a custom frame. Inside this wxShape window I want controls like a wxComboBox control, wxStatic control, wxButton control, just to name a few. Basically I'm constructing a dialog box but it has a custom frame and background thanks to wxShape. Is this possible or this is very complicated to get work.

User avatar
doublemax
Moderator
Moderator
Posts: 14509
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Own, transparent wxFrame background

Post by doublemax » Wed Jan 18, 2017 1:45 pm

In general it's same as with a "normal" frame. You just have to make sure that the controls are positioned correctly relative to the frame. E.g. if you have a circled shaped background, you should make sure the controls are not "hanging" outside.
Use the source, Luke!

DavidKlecker
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 232
Joined: Sun Nov 29, 2009 10:35 am

Re: Own, transparent wxFrame background

Post by DavidKlecker » Fri Jan 20, 2017 3:28 pm

Awesome! Thanks!

Post Reply