Transparency with wxGraphicsContext 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
Maeglix
Experienced Solver
Experienced Solver
Posts: 74
Joined: Thu Nov 17, 2016 8:07 am

Transparency with wxGraphicsContext

Post by Maeglix »

Hi guys,

I am trying to draw some polygon which can be place on top of each other with some transparency.
Example: The kind of tranparency of those graph (if i am not wring it is called alpha transparency )
https://rud.is/b/wp-content/uploads/201 ... galpha.png

I know i have to use wxGraphicsContext and so at the moment my code looks like this :

Code: Select all

void GADrawingArea::paintEvent(wxPaintEvent & evt)
{
    wxBufferedPaintDC dc(this);
    PrepareDC(dc);
    render(dc);
}

void GADrawingArea::render(wxDC&  dc)
{
	dc.Clear();
	wxPoint tab2[4] = {wxPoint(10,50), wxPoint(50,50), wxPoint(10,10), wxPoint(10,50)};
	dc.SetPen( *wxBLACK_PEN );
	dc.SetBrush(*wxBLACK_BRUSH);
	dc.DrawPolygon(4,tab2);


	wxGraphicsContext* gc;
	wxMemoryDC *memdc = wxDynamicCast(&dc, wxMemoryDC);
	gc = wxGraphicsContext::Create(*memdc);

        wxPoint2DDouble tab[4] = {wxPoint(25,50), wxPoint(65,50), wxPoint(25,10), wxPoint(25,50)};
	gc->SetPen( *wxRED_PEN );
	gc->SetBrush(*wxRED_BRUSH);
	gc->DrawLines(4,tab);
}
And i have this result:
Image

As you can see, there is not transparency.
I guess there is a function or something like a parameter in a function but i am not finding it.

Thanks for your help.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Transparency with wxGraphicsContext

Post by doublemax »

Use a color with alpha value for the pen and brush:

Code: Select all

    wxPoint2DDouble tab[4] = {wxPoint(25,50), wxPoint(65,50), wxPoint(25,10), wxPoint(25,50)};
    gc->SetPen( wxColor(255,0,0,128) );
    gc->SetBrush( wxColor(255,0,0,128) );
    gc->DrawLines(4,tab);
128 = 50% transpareny, of course you can also use other values.

BTW:

Code: Select all

   wxGraphicsContext* gc;
   wxMemoryDC *memdc = wxDynamicCast(&dc, wxMemoryDC);
   gc = wxGraphicsContext::Create(*memdc);
This cast is unneccesary and potentially dangerous.

This should work:

Code: Select all

wxGraphicsContext* gc = wxGraphicsContext::Create(dc);
Use the source, Luke!
Maeglix
Experienced Solver
Experienced Solver
Posts: 74
Joined: Thu Nov 17, 2016 8:07 am

Re: Transparency with wxGraphicsContext

Post by Maeglix »

Oh ok thanks !
doublemax wrote: BTW:

Code: Select all

   wxGraphicsContext* gc;
   wxMemoryDC *memdc = wxDynamicCast(&dc, wxMemoryDC);
   gc = wxGraphicsContext::Create(*memdc);
This cast is unneccesary and potentially dangerous.

This should work:

Code: Select all

wxGraphicsContext* gc = wxGraphicsContext::Create(dc);
I wanted to do that at start because i saw it in samples but when i do it i have this error:
error: no matching function for call to 'wxGraphicsContext::Create(wxDC&)
It told me too that candidates are the same function but with wxWindowDC&, wxMemoryDC&, wxPrinterDC& .... and that's why i thought i had to do the cast. But if you told me it's dangerous i should probably find why i have this error. Any idea ?
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Transparency with wxGraphicsContext

Post by doublemax »

It's because the render() method only takes the base class wxDC as reference.

Do you actually need the option to render into a different wxDC? If not, get rid of the render() method and put everything into the paint event handler.
Use the source, Luke!
Maeglix
Experienced Solver
Experienced Solver
Posts: 74
Joined: Thu Nov 17, 2016 8:07 am

Re: Transparency with wxGraphicsContext

Post by Maeglix »

Ok i did that and it works. Thanks a lot !
Post Reply