making a wxPanel transparent

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
cyrano1960
In need of some credit
In need of some credit
Posts: 6
Joined: Wed Apr 11, 2018 6:39 am

making a wxPanel transparent

Post by cyrano1960 »

Hello there,

I have written some simple code for displaying a LED-like control (s. attached image). The LED class inherits from wxPanel and I have painted them on a graphics context as a circle with a RadialGradientBrush. I set the wxPanel to the size of the circle, so the black square is the wxPanel. But I want to set the background colour of the wxPanel to the background color of the parent so that it appears in a transparent look. Therefore I placed the following statement into the constructor of the LED class:

Code: Select all

this->SetBackgroundColour(parent->GetBackgroundColour());
But it is still displayed in black. Even if I change the colour directly i. e. with:

Code: Select all

this->SetBackgroundColour(*wxRED);
there is no changing. I assume that it has to do with the graphic context, because I am drawing on it so I have placed the statement above after deleting the gc, but there is also no change.

Could anybody help me with this issue??? Thanks in advance,
Cyrano
Attachments
Screenshot 2022-09-26 142444.png
Screenshot 2022-09-26 142444.png (11.71 KiB) Viewed 387 times
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: making a wxPanel transparent

Post by ONEEYEMAN »

Hi,
Check the documentation - wxDC has everything you need...

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: making a wxPanel transparent

Post by doublemax »

cyrano1960 wrote: Mon Sep 26, 2022 8:13 pm Could anybody help me with this issue??? Thanks in advance,
viewtopic.php?p=205697#p205697
Use the source, Luke!
cyrano1960
In need of some credit
In need of some credit
Posts: 6
Joined: Wed Apr 11, 2018 6:39 am

Re: making a wxPanel transparent

Post by cyrano1960 »

Hi doublemax,

thank you so much for your help and the helpful topic, but something seems to be wrong with my code, it still doesn´t work :roll:

here is my code:

rxLED.cpp

Code: Select all

#include "rxLED.h"
	
rxLED::rxLED(wxWindow* parent, int diameter = 50) : wxWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTRANSPARENT_WINDOW)
	{
		Connect(wxEVT_PAINT, wxPaintEventHandler(rxLED::OnPaint));
		this->diameter = diameter;
		this->radius = diameter / 2;
		this->colour = rxGrey;
		this->SetMaxSize(wxSize(diameter, diameter));
		this->SetWindowStyleFlag(wxTRANSPARENT_WINDOW);		
		this->SetBackgroundStyle(wxBG_STYLE_PAINT);
	}

void rxLED::OnPaint(wxPaintEvent& event)
	{
		wxBufferedPaintDC dc(this);
		repaint(dc);
	}

void rxLED::setColour(wxColour* colour) 
	{
		this->colour = colour;
	}
	
void rxLED::repaint(wxBufferedPaintDC &dc) {
		
		wxGraphicsGradientStops led_stops;
		led_stops.Add(*wxWHITE, 0.0f);
		led_stops.Add(*colour, 0.7f);
		led_stops.Add(*colour, 1.0f);

		wxGraphicsGradientStops ring_stops;
		ring_stops.Add(wxColour(30, 30, 30), 0.0f);
		ring_stops.Add(wxColour(30, 30, 30), 0.8f);
		ring_stops.Add(wxColour(255, 255, 255), 0.9f);
		ring_stops.Add(wxColour(60, 60, 60), 1.0f);

		wxPoint start(radius, radius);
		wxPoint end(radius, radius);
		
		wxGraphicsContext* gc = wxGraphicsContext::Create(dc);
		
		gc->SetBrush(gc->CreateRadialGradientBrush(start.x, start.y, end.x, end.y, radius, ring_stops));
		gc->DrawEllipse(0, 0, diameter, diameter);
	
		gc->SetBrush(gc->CreateRadialGradientBrush(start.x, start.y, end.x, end.y, radius, led_stops));
		
		gc->DrawEllipse(diameter / 10, diameter / 10, diameter - (diameter / 5), diameter - (diameter / 5));
		delete gc;
	}
And only for my interest: do you have any idea, why the way to paint the child's background in the parent's background colour doesn´t work?

Thanks again and best regards,
Cyrano
cyrano1960
In need of some credit
In need of some credit
Posts: 6
Joined: Wed Apr 11, 2018 6:39 am

Re: making a wxPanel transparent

Post by cyrano1960 »

I have changed the DC buffer from wxBufferedPaintDC to wxPaintDC and now it works, but to be honest I have no idea why :cry: :(
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: making a wxPanel transparent

Post by doublemax »

cyrano1960 wrote: Tue Sep 27, 2022 7:46 pm And only for my interest: do you have any idea, why the way to paint the child's background in the parent's background colour doesn´t work?
When you call parent->GetBackgroundColour(), you will only get the correct color, if the background color of the parent has been explicitly set with SetColour() before.
Use the source, Luke!
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: making a wxPanel transparent

Post by doublemax »

cyrano1960 wrote: Tue Sep 27, 2022 7:52 pm I have changed the DC buffer from wxBufferedPaintDC to wxPaintDC and now it works, but to be honest I have no idea why :cry: :(
A wxBufferedPaintDC will redirect all drawing operations into a wxBitmap through a wxMemoryDC, and at the end just blit that wxBitmap onto the underlying wxPaintDC. But this bitmap is always opaque, even in the parts you didn't draw into.
Use the source, Luke!
cyrano1960
In need of some credit
In need of some credit
Posts: 6
Joined: Wed Apr 11, 2018 6:39 am

Re: making a wxPanel transparent

Post by cyrano1960 »

Thank you so much for your friendly support. You are such a great help to many of us. Best regards, Cyrano
Post Reply