Gradients as Backgrounds

BlitzMax Modules Forums/Brucey's Modules/Gradients as Backgrounds

Hello,
I don't think this is possible but i thought i would ask the wiser people.
I have a bmp image which shows a gradient effect of light at the top down to a slightly darker shade at the bottom.
I want to use it as a background to a window and then put my listboxes and listctrls on top of it. ideally each list control will use the same gradient as its background. It can have quite a nice subtle effect when done properly.

Here is the code
'
' BlitzMax code generated with wxCodeGen v1.06 : 04 Aug 2008 23:19:04
' 
' 
' PLEASE DO "NOT" EDIT THIS FILE!
' 
SuperStrict
Framework wx.wxApp

Import wx.wxFrame
Import wx.wxPanel
Import wx.wxStaticBitmap
Import wx.wxWindow

New MyApp.run()

Type MyApp Extends wxApp

	Field frame:MyFrame2Base
	
	Method OnInit:Int()
?macos
		' make the default Mac font for controls not so big
		wxSystemOptions.SetOption(wxWINDOW_DEFAULT_VARIANT, wxWINDOW_VARIANT_SMALL)
?
		' needed so my graphics show up
		wxInitAllImageHandlers()
		' for Mac/Win32 (it is default on Linux)
		wxImage.AddHandler( New wxXPMHandler )
		
		' create the frame
		Local frame:MyFrame2Base = MyFrame2Base(New MyFrame2Base.Create(, , "C2D TaskList Manager"))
		' frames are created hidden. You need to manually show it.
		frame.show()
		' make sure it is brought to the front
		SetTopWindow(frame)
		Return True
	End Method

End Type


Type MyFrame2Base Extends wxFrame

	Field m_panel2:wxPanel
	Field m_bitmap3:wxStaticBitmap


	Method Create:MyFrame2Base(parent:wxWindow = Null,id:Int = wxID_ANY, title:String = "", x:Int = -1, y:Int = -1, w:Int = 624, h:Int = 513, style:Int = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL)
		Return MyFrame2Base(Super.Create(parent, id, title, x, y, w, h, style))
	End Method

	Method OnInit()

		Local bSizer14:wxBoxSizer
		bSizer14 = New wxBoxSizer.Create(wxVERTICAL)
		m_panel2 = New wxPanel.Create(Self, wxID_ANY,,,,, wxTAB_TRAVERSAL)

		Local bSizer15:wxBoxSizer
		bSizer15 = New wxBoxSizer.Create(wxVERTICAL)
		m_bitmap3 = New wxStaticBitmap.Create(m_panel2, wxID_ANY, wxBitmap.CreateFromFile("C:/Images/C2D_Gradient_LightUp.bmp", wxBITMAP_TYPE_ANY))
		bSizer15.Add(m_bitmap3, 1, wxALL, 5)

		m_panel2.SetSizer(bSizer15)
		m_panel2.Layout()
		bSizer15.Fit(m_panel2)
		bSizer14.Add(m_panel2, 1, wxEXPAND | wxALL, 5)

		SetSizer(bSizer14)
		Layout()

	End Method

End Type



I don't have a site to upload the graphic too but it is a 300 by 300 bmp.
If someone wants to upload it a simple email will get it sent to you.


So far i have the image being displayed but it doesn't stretch with the window.

In my main program i can set the panel and control backgrounds to custom colours but the effect is not as nice as the gradient.

Any suggestions appreciated.

Glenn

Here's a slightly modified version.

'
' BlitzMax code generated with wxCodeGen v1.06 : 04 Aug 2008 23:19:04
' 
' 
' PLEASE DO "NOT" EDIT THIS FILE!
' 
SuperStrict
Framework wx.wxApp

Import wx.wxFrame
Import wx.wxPanel
Import wx.wxStaticBitmap
Import wx.wxWindow
Import wx.wxAutoBufferedPaintDC

New MyApp.run()

Type MyApp Extends wxApp

	Field frame:MyFrame2Base
	
	Method OnInit:Int()
?macos
		' make the default Mac font for controls not so big
		wxSystemOptions.SetOption(wxWINDOW_DEFAULT_VARIANT, wxWINDOW_VARIANT_SMALL)
?
		' needed so my graphics show up
		wxInitAllImageHandlers()
		' for Mac/Win32 (it is default on Linux)
		wxImage.AddHandler( New wxXPMHandler )
		
		' create the frame
		Local frame:MyFrame2Base = MyFrame2Base(New MyFrame2Base.Create(, , "C2D TaskList Manager"))
		' frames are created hidden. You need to manually show it.
		frame.show()
		' make sure it is brought to the front
		SetTopWindow(frame)
		Return True
	End Method

End Type


Type MyFrame2Base Extends wxFrame

	Field m_panel2:wxPanel
	Field m_bitmap3:wxStaticBitmap


	Method Create:MyFrame2Base(parent:wxWindow = Null,id:Int = wxID_ANY, title:String = "", x:Int = -1, y:Int = -1, w:Int = 624, h:Int = 513, style:Int = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL)
		Return MyFrame2Base(Super.Create(parent, id, title, x, y, w, h, style))
	End Method

	Method OnInit()

		Local bSizer14:wxBoxSizer
		bSizer14 = New wxBoxSizer.Create(wxVERTICAL)
		m_panel2 = TGradientPanel(New TGradientPanel.Create(Self, wxID_ANY,,,,, wxTAB_TRAVERSAL))

		bSizer14.Add(m_panel2, 1, wxEXPAND | wxALL, 0)

		SetSizer(bSizer14)

		Layout()

	End Method

End Type

Type TGradientPanel Extends wxPanel

	Field bitmap:wxBitmap
	Field image:wxImage
	
	Method OnInit()
	
		SetBackgroundStyle(wxBG_STYLE_CUSTOM)
	
		image = wxImage.CreateFromFile("C:/gradient.bmp", wxBITMAP_TYPE_ANY)

		DoSize()

		ConnectAny(wxEVT_PAINT, OnPaint)
		ConnectAny(wxEVT_SIZE, OnSize)

	End Method
	
	' creates a new scaled bitmap to fit.
	Method DoSize()
		Local width:Int, height:Int
		GetSize(width, height)

		bitmap = wxBitmap.CreateFromImage(image.Scale(width, height))
	End Method
	
	Function OnSize(event:wxEvent)
		Local panel:TGradientPanel = TGradientPanel(event.parent)

		panel.DoSize()
		panel.Refresh()
	End Function

	Function OnPaint(event:wxEvent)

		Local panel:TGradientPanel = TGradientPanel(event.parent)

		Local dc:wxAutoBufferedPaintDC = New wxAutoBufferedPaintDC.CreatePaintDC(panel)
		
		dc.DrawBitmap(panel.bitmap, 0, 0, False)

		dc.free()
	End Function


End Type


Note how I subclassed a wxPanel, and have connected to the OnPaint event - this way I can do what I like when it needs to refresh itself.

I had made a more simple version, but it didn't look nice when the window was resized. This version (at least on Win32) appears to scale nicely, without flicker (it uses a double-buffered DC).

Is that what you're after?

Perfect.

Many thanks
Glenn

np :-)