Transparency support on wxDC

118 views
Skip to first unread message

Chris Spencer

unread,
Sep 8, 2009, 4:40:41 PM9/8/09
to wx-u...@googlegroups.com
Hi all,

I need support for an alpha value on a wxBrush when drawing a
rectangle on a wxDC object (so you can still what's behind the
rectangle, just obscured). The manual for wxDC says it works for Mac
OS X when using Core Graphics, but doesn't say for other wx ports.
Does anyone know about the others? I'd rather find out beforehand
before I discover I've wasted my effort..

Chris.

Bryan Petty

unread,
Sep 8, 2009, 5:20:45 PM9/8/09
to wx-u...@googlegroups.com

It _only_ works on OS X when using Core Graphics. You can however
accomplish this task easily on all platforms by using the newer
wxGraphicsContext API instead of wxDC.

Regards,
Bryan Petty

Chris Spencer

unread,
Sep 8, 2009, 5:25:58 PM9/8/09
to wx-u...@googlegroups.com
2009/9/8 Bryan Petty <br...@ibaku.net>:

> It _only_ works on OS X when using Core Graphics. You can however
> accomplish this task easily on all platforms by using the newer
> wxGraphicsContext API instead of wxDC.
>
> Regards,
> Bryan Petty

Perfect. Thanks Bryan.

Chris.

Robin Dunn

unread,
Sep 8, 2009, 7:29:13 PM9/8/09
to wx-u...@googlegroups.com

Or for an easier transition you can use the wxGCDC, which implements the
wxDC API using the wxGraphicsContext. (Not always a perfect match, but
it's close.)


--
Robin Dunn
Software Craftsman
http://wxPython.org

Uli Hertlein

unread,
Sep 9, 2009, 4:16:37 AM9/9/09
to wx-u...@googlegroups.com
Robin Dunn wrote:
> Or for an easier transition you can use the wxGCDC, which implements the
> wxDC API using the wxGraphicsContext. (Not always a perfect match, but
> it's close.)

Is there a particular reason why wxGCDC isn't in the online docs yet? Is it
deprecated?

Also, is it possible to use wxGraphicsContext to draw into a bitmap?

I thought I've seen something related to this but couldn't find it anymore in
2.8.10. The wxMemoryDC constructor is MSW-only.

Cheers,
/uli

--
Uli Hertlein
Research and Development email: u...@xdt.com.au
XDT Pty Ltd Web: www.xdt.com.au

Chris Spencer

unread,
Sep 9, 2009, 10:06:30 AM9/9/09
to wx-u...@googlegroups.com
Ok I've started using wxGraphicsContext and it does exactly what I
want, but on Windows it flickers a bit. Here is the paint event of my
custom control:

wxPaintDC dc(this);
wxGraphicsContext *gc = wxGraphicsContext::Create(dc);
gc->SetBrush(wxBrush(wxColour(255, 204, 229, 63)));
gc->DrawBitmap(label_, 0, 0, label_.GetWidth(), label_.GetHeight());
gc->DrawRectangle(0, lines_, label_.GetWidth(), label_.GetHeight() - lines_);

This is called every half second or so using Refresh(). Quite
frequently there will be a very noticable flicker where the bitmap is
shown before the rectangle is drawn, or even a white background is
shown before even the bitmap is drawn. I tried using Freeze() and
Thaw() but it didn't make a difference. I works as intended in wxGTK.

First, am I using wxGraphicsContext correctly, and if so, any ideas
how to fix this?

On a related note, is it necessary to delete the graphics context at
the end of the function?

Chris.

Robin Dunn

unread,
Sep 9, 2009, 11:52:44 AM9/9/09
to wx-u...@googlegroups.com
Uli Hertlein wrote:
> Robin Dunn wrote:
>> Or for an easier transition you can use the wxGCDC, which implements the
>> wxDC API using the wxGraphicsContext. (Not always a perfect match, but
>> it's close.)
>
> Is there a particular reason why wxGCDC isn't in the online docs yet? Is it
> deprecated?
>
> Also, is it possible to use wxGraphicsContext to draw into a bitmap?

Yes.

>
> I thought I've seen something related to this but couldn't find it anymore in
> 2.8.10. The wxMemoryDC constructor is MSW-only.

I think it still works on the other platforms though because the wxDC
inheritance hierarchy is slightly different.

Robin Dunn

unread,
Sep 9, 2009, 11:52:55 AM9/9/09
to wx-u...@googlegroups.com
Chris Spencer wrote:
> Ok I've started using wxGraphicsContext and it does exactly what I
> want, but on Windows it flickers a bit. Here is the paint event of my
> custom control:
>
> wxPaintDC dc(this);
> wxGraphicsContext *gc = wxGraphicsContext::Create(dc);
> gc->SetBrush(wxBrush(wxColour(255, 204, 229, 63)));
> gc->DrawBitmap(label_, 0, 0, label_.GetWidth(), label_.GetHeight());
> gc->DrawRectangle(0, lines_, label_.GetWidth(), label_.GetHeight() - lines_);
>
> This is called every half second or so using Refresh(). Quite
> frequently there will be a very noticable flicker where the bitmap is
> shown before the rectangle is drawn, or even a white background is
> shown before even the bitmap is drawn. I tried using Freeze() and
> Thaw() but it didn't make a difference.

Freeze/Thaw simply suspend the sending of Paint events, but since you
are already in the handler for a paint event it doesn't really do
anything. In a nutshell the issue is that on Windows the intermediate
steps in a set of drawing operations can be flushed to the screen at any
time, and so it's possible to see steps along the way.

> First, am I using wxGraphicsContext correctly, and if so, any ideas
> how to fix this?

Use a wxBufferedPaintDC on Windows instead of the wxPaintDC.


>
> On a related note, is it necessary to delete the graphics context at
> the end of the function?

Yes.

Chris Spencer

unread,
Sep 9, 2009, 12:09:22 PM9/9/09
to wx-u...@googlegroups.com
2009/9/9 Robin Dunn <ro...@alldunn.com>:

> Use a wxBufferedPaintDC on Windows instead of the wxPaintDC.

That's sorted it, thanks for that Robin.

Reply all
Reply to author
Forward
0 new messages