Transparency PNG Topic is solved

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
mxoliveira73
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sun May 05, 2019 7:12 am

Transparency PNG

Post by mxoliveira73 »

I'm trying create a image png with transparency. Very basic...
But.. in the example bellow, i created a wxPanel black, and over it there are a image PNG with transparency. In true, part white in the image is a
PNG transparent, it's no white... I don't understand what is wrong. It's so basic.... Coul'd someone give one tip, please?

Panel_box = new wxPanel(newPanel, wxID_ANY,wxPoint(0,50),wxSize(200,100));
Panel_box->SetBackgroundColour(wxColor(0,0,0));

wxBitmap bmp;
bmp.LoadFile("image.png", wxBITMAP_TYPE_PNG);

ball = new wxButton(Panel_box, ID_ball, wxEmptyString, wxPoint(50,05), bmp.GetSize(), wxTRANSPARENT_WINDOW | wxBORDER_NONE);

ball->SetBitmap(bmp);

ball.png
ball.png (7.81 KiB) Viewed 911 times
New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 466
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: Transparency PNG

Post by New Pagodi »

When the "transparent" parts of an image are drawn, what really happens is that the background color of the window is drawn instead. ball thinks its background color is white so that's what gets drawn for the transparent parts of the image. You can try

Code: Select all

ball->SetBackgroundColour(wxColor(0,0,0));
to make the button think it has the same background color as the panel. That works on windows, but it may not work on GTK+ or mac since setting colors for buttons may not be supported on those platforms.
mxoliveira73
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sun May 05, 2019 7:12 am

Re: Transparency PNG

Post by mxoliveira73 »

One more time, big big thanks, New Pagodi!!!!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Transparency PNG

Post by ONEEYEMAN »

New Pagodi,
So it is best to modify the PNG and give it an appropriate background colour in order to make it cross-platform?

Thank you.
New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 466
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: Transparency PNG

Post by New Pagodi »

ONEEYEMAN wrote: Mon Jun 24, 2019 2:52 pm New Pagodi,
So it is best to modify the PNG and give it an appropriate background colour in order to make it cross-platform?

Thank you.
That will work if the background color of the panel never changes. The only solution that is guaranteed to work on all ports is to skip the button class altogether, draw the bitmap directly on to the panel, and handle the mouse left up event on the panel and check if it is over the bitmap. But that's complicated enough that if setting the background color works for what OP wants, it might as well be done that way.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Transparency PNG

Post by ONEEYEMAN »

New Pagodi,
Well for my use case I need the button on the toolbar, not the panel.

So I will just modify the PNG background and everybody will be happy. ;-)

Thank you.
Post Reply