wxStaticText - transparent backgrounds in XP

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

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

How's about this :

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">
Const s_BITMAP_PANEL_PATH:String = "media/bitmap_panel.png"

New MyApp.run()

' ----------------------------------------------------------------
Rem
	bbdoc:   
	about:    
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, , "Window", , , 600, 600)
		Window.Centre()	
	
		'	Create the p=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)
		'	Label[label_id].Refresh()
			y:+ 80
		
		Next
			
		Window.Show(True)
					
		Return True
	
	End Method

End Type

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

	Field Font:wxFont

	Global BackgroundImage:wxBitmap
	Global s_BackgroundImage:String = s_BITMAP_PANEL_PATH 
	
	' ----------------------------------------------------------------
	Rem
		bbdoc:   
		about:    
	End Rem	
	Method OnEraseBackground(_event:wxEraseEvent)
	
		DebugLog "OnEraseIn"
	
		If Not _event Then Throw "TLabel.OnEraseBackground - null event!"
	
		Local dc:wxDC = _event.GetDC()
		
		If dc 
		
			Local x:Int
			Local y:Int
			Local w:Int
			Local h:Int
			
			getRect(x,y,w,h)
			
			'dc.SetBrush(wxTRANSPARENT_BRUSH())
			'dc.SetBackgroundMode(wxTRANSPARENT)
		
			dc.SetTextForeground(New wxColour.Create(255,255,255))
		
			If BackgroundImage Then
				Local bmp:wxBitmap = BackgroundImage.GetSubBitmap(x, y, w, h)
				dc.DrawBitmap(bmp, x, y, True)
			End If
'			dc.DrawText(GetLabel(),x, y)
			'dc.DrawText("asdasd",x, y)
			
			'DebugLog "OnEraseBackground " +GetLabel() +" x " +x +" y " +y
			
			'RefreshRect(x, y, w, h, False)
		
		EndIf	
		
		'_event.Skip()
		
		DebugLog "OnEraseOut"
			
	End Method
	
	' ----------------------------------------------------------------
	Rem
		bbdoc:   
		about:    
	End Rem	
	Method OnInit()

		If Not BackgroundImage
									
			'	Does image file exist?
			If Not FileType(s_BackgroundImage) Then Throw "TPanel.OnPaint 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 "TPanel.OnPaint Invalid image (we require PNG format)  " + s_BackgroundImage
		EndIf


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

		Local x:Int
		Local y:Int
		Local w:Int
		Local h:Int
		
		getRect(x,y,w,h)

		If BackgroundImage Then
			Local bmp:wxBitmap = BackgroundImage.GetSubBitmap(x, y, w, h)
			dc.DrawBitmap(bmp, 0, 0, True)
		End If
		
		dc.SetFont(GetFont())
		
		dc.DrawText(GetLabel(),0, 0)
		
		DebugLog "OnPaint " +GetLabel() +" x " +x +" y " +y
		
		dc.Free()
		
	End Method
	
	' ----------------------------------------------------------------
	Rem
		bbdoc:   
		about:    
	End Rem	
	Function _OnErase(_event:wxEvent)
		TLabel(_event.parent).OnEraseBackground(wxEraseEvent(_event))
	End Function
	' ----------------------------------------------------------------
	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

	Field BackgroundImage:wxBitmap
	Field s_BackgroundImage:String = s_BITMAP_PANEL_PATH 
		
	' ----------------------------------------------------------------
	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 Or s_BackgroundImage 
			
			paint_dc = New wxPaintDC.Create(Self)
	
			' If we haven't loaded in our image yet
			If Not BackgroundImage
										
				'	Does image file exist?
				If Not FileType(s_BackgroundImage) Then Throw "TPanel.OnPaint 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 "TPanel.OnPaint Invalid image (we require PNG format)  " + s_BackgroundImage
			EndIf
			
			'	Draw the background image
			If BackgroundImage Then 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



Kind of avoids the issue by painting the appropriate bitmap section before drawing text on top of it.