[wxPython-users] wx.StaticText size (12KB attachment)

Robin Dunn robin at alldunn.com
Wed Jan 2 16:57:28 PST 2008


Algirdas Brazas wrote:
> Thank You a lot. At the moment I'have figured out problem with text 
> margins. What
> I'we done is more a workaround, but it works :)
> My problem was that the code:
> 
> font=wx.Font(18,wx.DEFAULT,wx.NORMAL,wx.BOLD)
> memoryDC = wx.MemoryDC()
> memoryDC.SetFont(font)
> w,h=memoryDC.GetTextExtent("Test")
> 
> returned 29 for the height. In the example I'we used preconfigured size 
> font, but in real app font is set by the user. Anyway, I found out, that 
> if I multiply height by 0.6 I'll get average font height. And when 
> drawing with wx.DC there is no more blank borders covering text, so it 
> is working for the moment.

Be careful with the 0.6, you may end up clipping things too much if 
different fonts or different platforms are used.  Try using 
GetFullTextExtent and then you'll also get the descent and leading values.

Also, you should select a bitmap into the DC otherwise you'll get errors 
on other platforms if you try to use it, even for just measuring.


> 
> There is one more question that I google for 3 hourls already. I want to 
> make background transparent. So I use self.SetTransparent(). But then I 
> cannot make drawn text lines opaque. From the other side if I paint 
> frame with transparent color I can see background color. How could I get 
> rid of this background?
> Or should I make frame size 0 and use ScreenDC?

SetTransparent sets an alpha transparency for the frame including it's 
full contents.  I assume that instead you just want the text to be fully 
opaque and the remainder to be fully transparent, right?  If so then on 
Windows you can use the empty EVT_ERASE_BACKGROUND trick.  Otherwise you 
want to use a shaped window, which is shown in the demo.  Essentially 
you use a bitmap, fill it with a known background colour, draw your text 
to the bitmap, set the mask using the known background colour, create a 
region from the mask and then set the shape of the frame using the 
region.  Oh yeah, you need to draw the bitmap in the EVT_PAINT handler.


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!




More information about the wxpython-users mailing list