wxStaticText - transparent backgrounds in XP

BlitzMax Modules Forums/Brucey's Modules/wxStaticText - transparent backgrounds in XP

So EraseBackground wasn't needed in the end at all! I'm amazed at how many different solutions were out there - but I didn't see that one!

Thanks so much Brucey :-)

[Edit] I've cleaned up the above code and posted it below for the benefit of those also stuck on this issue.

SuperStrict

Framework wx.wxApp
Import wx.wxFrame
Import wx.wxStaticText
Import wx.wxPanel

'	You'll need to download this from <img src="http://www.gibbongames.com/temp/bitmap_panel.png">
Global s_BackgroundImage:String = "media/bitmap_panel.png"
'	You'd probably store these in a bitmap class rather than pure globals
Global BackgroundImage:wxBitmap

New MyApp.run()

' ----------------------------------------------------------------
Rem
	bbdoc:   This is an XP-only work around to enable transparent static text control backgrounds
	about:   OS X (and I presume Vista) don't need this 
End Rem	
Type MyApp Extends wxApp

	Field Window:wxFrame
	Field Panel:TBitmapPanel
	Field Label:TLabel[6]
	
	' ----------------------------------------------------------------
	Rem
		bbdoc:   
		about:    
	End Rem		
	Method OnInit:Int()
		
		wxInitAllImageHandlers()

		'	Create the window
		Window = New wxFrame.Create(Null, , "How to draw transparent static text controls in XP", , , 600, 600)
		Window.Centre()	
		
		'	Both label and panel need access to this
		If Not BackgroundImage
									
			'	Does image file exist?
			If Not FileType(s_BackgroundImage) Then Throw "MyApp.OnInit Image does Not exist? Invalid path: " + s_BackgroundImage
			
			'	Load in the image. No need to free it manually GC will do this for us
			BackgroundImage = wxBitmap.CreateFromFile(s_BackgroundImage, wxBITMAP_TYPE_PNG)
			
			'	Safety check
			If Not BackgroundImage.IsOK() Then Throw "MyApp.OnInit Invalid image (we require PNG format)  " + s_BackgroundImage
		
		EndIf
	
		'	Create the textured panel
		Panel = TBitmapPanel(New TBitmapPanel.Create(Window,wxID_ANY, 1,1,598,598))
			
		'	Create 6 labels	
		Local y:Int = 25
		
		Local s_text:String[] = ["Label 1 (one)","Label 2 (two)","Label 3 (three)", "Label 4 (four)", "Label 5 (five)", "Label 6 (six)"]
							
		For Local label_id:Int = 0 Until s_text.length
							
			Label[label_id] 		= TLabel(New TLabel.Create(Panel, wxID_ANY,s_text[label_id], 50,y, 450,60))
			Label[label_id].Font 	= New wxFont.CreateWithAttribs(16 +2*label_id, wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL ,wxFONTWEIGHT_NORMAL)
			Label[label_id].SetFont(Label[label_id].Font)
			y:+ 80
		
		Next
			
		Window.Show(True)
					
		Return True
	
	End Method

End Type

' ----------------------------------------------------------------
Rem
	bbdoc:   
	about:    
End Rem	
Type TLabel Extends wxStaticText

	Field Font:wxFont
	Field SubBitmap:wxBitmap
		
	' ----------------------------------------------------------------
	Rem
		bbdoc:   
		about:    
	End Rem	
	Method OnInit()

		Connect( GetId(),wxEVT_PAINT,_OnPaint)
		
	End Method
	
	' ----------------------------------------------------------------
	Rem
		bbdoc:   
		about:    
	End Rem		
	Method OnPaint(_event:wxPaintEvent)
	
		Local dc:wxPaintDC = New wxPaintDC.Create(Self)

		If BackgroundImage 

			'	Grab the texture immediately beneath this static text label
			If Not SubBitmap 

				Local x:Int
				Local y:Int
				Local w:Int
				Local h:Int
				
				getRect(x,y,w,h)
			
				SubBitmap = BackgroundImage.GetSubBitmap(x, y, w, h)
			EndIf
			
			' Plaster the label background with the same texture as the panel beneath us
			dc.DrawBitmap(SubBitmap, 0, 0, True)
		End If
		
		dc.SetFont(GetFont())
		
		dc.DrawText(GetLabel(),0, 0)
		
		DebugLog "OnPaint " +GetLabel() 
		
		dc.Free()
		
	End Method

	' ----------------------------------------------------------------
	Rem
		bbdoc:   
		about:    
	End Rem	
	Function _OnPaint(_event:wxEvent)
		TLabel(_event.parent).OnPaint(wxPaintEvent(_event))
	End Function

End Type

' ----------------------------------------------------------------
Rem
	bbdoc:   
	about:    
End Rem	
Type TBitmapPanel Extends wxPanel
	
	' ----------------------------------------------------------------
	Rem
		bbdoc:   
		about:    
	End Rem	
	Method OnInit()
		ConnectAny(wxEVT_PAINT, _OnPaint)		
	End Method

	' ----------------------------------------------------------------
	Rem
		bbdoc:   
		about:    
	End Rem	
	Method OnPaint(_event:wxPaintEvent)
		
		Local paint_dc:wxPaintDC
		
		'	If we desire an image background
		If BackgroundImage 
			
			paint_dc = New wxPaintDC.Create(Self)
	
			'	Draw the background image
			paint_dc.DrawBitmap(BackgroundImage, 0, 0, True)
		
		EndIf	
			
		' Manual free as we can't afford to rely on timely auto garbage collection 
		If paint_dc 
			paint_dc.Free()
		EndIf
								
		_event.Skip()
		
	End Method
	
	Rem
		bbdoc:   
		about:    
	End Rem	
	Function _OnPaint(_event:wxEvent)
		TBitmapPanel(_event.parent).OnPaint(wxPaintEvent(_event))
	End Function

End Type