# Transparent line in Frame

**URL:** <https://www.ruby-forum.com/t/transparent-line-in-frame/135356>\
**Category:** wxRuby\
**Created:** [April 21, 2008, 2:39pm UTC](https://www.ruby-forum.com/t/transparent-line-in-frame/135356 "2008-04-21T14:39:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![awarberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5ac83/32.png) [@awarberg](https://www.ruby-forum.com/u/awarberg)\
**Post date:** [April 21, 2008, 2:39pm UTC](https://www.ruby-forum.com/t/transparent-line-in-frame/135356/1 "2008-04-21T14:39:36Z")

</div>

Hi

Thanks for a great GUI toolkit!

I want to draw transparent lines in a Frame. I have the following setup  
but my lines are not transparent.

My colour object is created using:  
transparent\_blue = Colour.new(0,0,255,100)

and I draw lines using a pen with this colour:  
@pen = Pen.new(transparent\_blue)

In evt\_paint of frame I have:  
dc.set\_pen(@pen)  
dc.draw\_line(x1,y1,x2,y2)

The line is blue but there is no transparency.

I set the alpha level to 100 where 255 is supposed to be completely  
opaque.

I’m on Windows XP SP2 and

C:\>ruby --version  
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

Please help!

Regards, Andreas

---

<div class="post-metadata">

**Author:** ![awarberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5ac83/32.png) [@awarberg](https://www.ruby-forum.com/u/awarberg)\
**Post date:** [April 21, 2008, 3:13pm UTC](https://www.ruby-forum.com/t/transparent-line-in-frame/135356/2 "2008-04-21T15:13:57Z")

</div>

Andreas W. wrote:

> I want to draw transparent lines in a Frame. I have the following setup  
> but my lines are not transparent.  
> The standard DC doesn’t support transparency (except on OS X I think).  
> But you can instead use wxGraphicsContext which fully supports  
> transparency, antialiasing and other advanced drawing tools like splines  
> and transforms. You create one from an ordinary DC like this:

paint.do | dc |  
gdc = Wx::GraphicsContext.create(dc)  
gdc.stroke\_line(0, 0, 500, 500)

# etc

end

Unfortunately the wxWidgets developers decided to make the API to  
GraphicsContext different to DC, but the results are worth it for  
graphical apps.

alex

---

<div class="post-metadata">

**Author:** ![awarberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5ac83/32.png) [@awarberg](https://www.ruby-forum.com/u/awarberg)\
**Post date:** [April 21, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/transparent-line-in-frame/135356/3 "2008-04-21T16:20:40Z")

</div>

Alex F. wrote:

> The standard DC doesn’t support transparency (except on OS X I think).  
> But you can instead use wxGraphicsContext which fully supports  
> transparency, antialiasing and other advanced drawing tools like splines  
> and transforms. You create one from an ordinary DC like this:
> 
> paint.do | dc |  
> gdc = Wx::GraphicsContext.create(dc)  
> gdc.stroke\_line(0, 0, 500, 500)
> 
> # etc
> 
> end

Thanks! I’m having a bit of trouble, though. Here is a small example I  
use:

require ‘wx’  
include Wx

App.run do  
frame = Frame.new(nil, :title =\> ‘GraphicsContext example’, :width =\>  
[300,400])

frame.evt\_paint do  
frame.paint do |dc|  
gdc = GraphicsContext.create(dc)  
siz = dc.size  
h, w = siz.height, siz.width  
#dc.draw\_line(0,0,w,h)  
gdc.stroke\_line(0,0,w,h)  
end  
end

frame.show  
end

It should draw / stroke a diagonal line in the frame. This works if I  
use draw\_line.

What am I missing? 🙂

Andreas

---

<div class="post-metadata">

**Author:** ![awarberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5ac83/32.png) [@awarberg](https://www.ruby-forum.com/u/awarberg)\
**Post date:** [April 21, 2008, 4:35pm UTC](https://www.ruby-forum.com/t/transparent-line-in-frame/135356/4 "2008-04-21T16:35:44Z")

</div>

Andreas W. wrote:

> frame.evt\_paint do  
> frame.paint do |dc|  
> gdc = GraphicsContext.create(dc)  
> siz = dc.size  
> h, w = siz.height, siz.width  
> #dc.draw\_line(0,0,w,h)

```
  gdc.pen = Wx::Pen.new( Wx::Colour.new(255, 0, 0, 128), 20)

```

> What am I missing? 🙂  
> It seems with GraphicsContext, you need to set some sort of pen before  
> starting drawing - there is no default pen when it’s first created.

alex

---

<div class="post-metadata">

**Author:** ![awarberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5ac83/32.png) [@awarberg](https://www.ruby-forum.com/u/awarberg)\
**Post date:** [April 21, 2008, 4:47pm UTC](https://www.ruby-forum.com/t/transparent-line-in-frame/135356/5 "2008-04-21T16:47:05Z")

</div>

Alex F. wrote:

> Andreas W. wrote:
> 
> > frame.evt\_paint do  
> > frame.paint do |dc|  
> > gdc = GraphicsContext.create(dc)  
> > siz = dc.size  
> > h, w = siz.height, siz.width  
> > #dc.draw\_line(0,0,w,h)
> 
> ```
> gdc.pen = Wx::Pen.new( Wx::Colour.new(255, 0, 0, 128), 20)
> 
> ```
> 
> > What am I missing? 🙂  
> > It seems with GraphicsContext, you need to set some sort of pen before  
> > starting drawing - there is no default pen when it’s first created.
> 
> alex

Thanks!
