How make wxPanel transparent on Windows and Linux?

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.
User avatar
doublemax
Moderator
Moderator
Posts: 15866
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How make wxPanel transparent on Windows and Linux?

Post by doublemax » Wed Aug 27, 2014 8:53 am

In principle it should work this way, but i can't tell you where exactly it fails.

First try without the child panels and make sure that the parent window redraws correctly. Then add the panel, try commenting out parts of the paining process to see where it fails.
Use the source, Luke!

gpu
Experienced Solver
Experienced Solver
Posts: 92
Joined: Tue Aug 12, 2014 3:46 am

Re: How make wxPanel transparent on Windows and Linux?

Post by gpu » Wed Aug 27, 2014 9:25 am

doublemax wrote:In principle it should work this way, but i can't tell you where exactly it fails.

First try without the child panels and make sure that the parent window redraws correctly. Then add the panel, try commenting out parts of the paining process to see where it fails.
the parent is ok...but when I use wxBufferedPaintDC in Child, the background is black, it seems that the bitmap draws on a blackground buffer.....

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

Re: How make wxPanel transparent on Windows and Linux?

Post by doublemax » Wed Aug 27, 2014 9:31 am

the parent is ok...but when I use wxBufferedPaintDC in Child, the background is black, it seems that the bitmap draws on a blackground buffer.....
Yes, by default it's empty (=black). You shouldn't rely on any content there, the rendering code from the parent would usually draw a background bitmap there or clear it with the background color.
Use the source, Luke!

gpu
Experienced Solver
Experienced Solver
Posts: 92
Joined: Tue Aug 12, 2014 3:46 am

Re: How make wxPanel transparent on Windows and Linux?

Post by gpu » Wed Aug 27, 2014 9:49 am

the parent is just set a background color, and draw some simple geometry,
the child panel have a semi-transparent bitmap...but the background is still black, is there any API should be called else?

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

Re: How make wxPanel transparent on Windows and Linux?

Post by doublemax » Wed Aug 27, 2014 9:57 am

Maybe show a screenshot and/or more real code. Ideally a minimal compilable sample that shows the problem.
Use the source, Luke!

gpu
Experienced Solver
Experienced Solver
Posts: 92
Joined: Tue Aug 12, 2014 3:46 am

Re: How make wxPanel transparent on Windows and Linux?

Post by gpu » Wed Aug 27, 2014 9:59 am

Code: Select all

Parent::paintEvent(wxPaintEvent&)
{
wxPaintDC(this);  //
Render(dc, GetRect());
}
Parent::Render(wxDC& dc, const wxRect&)
{
//dc.setPen();
//HERE, just draw some simple line for test now
//dc.SetPen(wxNullPen);
}
//////////////////////////////////////
Child::paintEvent(wxPaintEvent&)
{
wxBufferedPaintDC(this);  //
Render(dc, GetRect());
}
Child::Render(wxDC& dc, const wxRect&)
{
//..getParent
parent->Render(dc, GetRect());
dc.DrawBitmap;  //draw the child's semi-transparent background
}
the code is simply like this....sry, that PC can't link to Internet.
And i don't use the rect got from GetRect(), is there problem?

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

Re: How make wxPanel transparent on Windows and Linux?

Post by doublemax » Wed Aug 27, 2014 10:26 am

Code: Select all

Parent::Render(wxDC& dc, const wxRect&)
{
//dc.setPen();
//HERE, just draw some simple line for test now
//dc.SetPen(wxNullPen);
}
The question is what exactly you're drawing here, i.e. do you clear the background before drawing any lines?

Code: Select all

Parent::Render(wxDC& dc, const wxRect&)
{
   dc.SetBrush( *wxWHITE_BRUSH);
   dc.SetPen( *wxWHITE_PEN);
   dc.DrawRectangle( rect );

   dc.SetPen( *wxBLACK_PEN );
   ...
   // more drawing here
}
And i don't use the rect got from GetRect(), is there problem?
This will definitely be a problem in the final code, because the background of the panel will contain the same drawing as the upper left corner of the parent.
Use the source, Luke!

gpu
Experienced Solver
Experienced Solver
Posts: 92
Joined: Tue Aug 12, 2014 3:46 am

Re: How make wxPanel transparent on Windows and Linux?

Post by gpu » Wed Aug 27, 2014 11:07 am

This will definitely be a problem in the final code, because the background of the panel will contain the same drawing as the upper left corner of the parent.
But where should I use it ?

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

Re: How make wxPanel transparent on Windows and Linux?

Post by doublemax » Wed Aug 27, 2014 11:52 am

Use wxDC::SetDeviceOrigin(...) so that you're drawing the correct portion of the parent background into the panel's background.
Use the source, Luke!

gpu
Experienced Solver
Experienced Solver
Posts: 92
Joined: Tue Aug 12, 2014 3:46 am

Re: How make wxPanel transparent on Windows and Linux?

Post by gpu » Wed Aug 27, 2014 1:42 pm

doublemax wrote: Parent::Render(wxDC& dc, const wxRect&)
{
//dc.setPen();
//HERE, just draw some simple line for test now
//dc.SetPen(wxNullPen);
}

The question is what exactly you're drawing here, i.e. do you clear the background before drawing any lines?
I don't clear the background...I just use dc.DrawLine draw some lines, that's all i did, the only API i call.

gpu
Experienced Solver
Experienced Solver
Posts: 92
Joined: Tue Aug 12, 2014 3:46 am

Re: How make wxPanel transparent on Windows and Linux?

Post by gpu » Wed Aug 27, 2014 2:05 pm

the problem is solved, it's the SetdeviceOrigin problem
and I used wxPaintDC for both the parent and child, cause if use wxBufferedDC for the Child , the Child's background is always black!
thanks!

Post Reply