Hi,
Is it possible to use an Image as a background to a form / frame / panel
and then load other controls (like a text box) on top of it? Can this
be achieved using transparency? Or is there a 'Z' order that can be used?
Capture EVT_PAINT and EVT_SIZE and use those to draw to a panel:
class MyPanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
self.bitmap = wx.Bitmap('bitmap.png')
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.Draw(dc)
def OnSize(self, event):
dc = wx.ClientDC(self)
self.Draw(dc)
def Draw(self, dc):
dc.DrawBitmap(self.bitmap, 0, 0)
I tried fooling around with the ShapedWindow example in the demos but
was unable to load any control on it. I tried putting a button on top
of the penguin, but that didn't work. Perhaps I am doing something wrong.
Perhaps some example code demonstrating what you were attempting? I've
used a wx.ShapedWindow as a regular frame successfully.
I would appreciate it if someone could offer a little advice on how I
could use an image as a background to a form or better still, how I
could load controls on a ShapedWindow.
This is based on some code I wrote using a ShapedWindow, but it's pretty
much straight from the demo. It's untested as is (I stripped a bunch of
app-specific code from it), but it should be pretty close:
class MyFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, '',
style = wx.FRAME_SHAPED | wx.SIMPLE_BORDER | wx.FRAME_NO_TASKBAR | wx.STAY_ON_TOP)
self.image = wx.Bitmap('frame.png')
self.width, self.height = self.image.GetWidth(), self.image.GetHeight()
self.delta = (0, 0)
self.hasShape = False
self.SetClientSize((self.width, self.height))
if wx.Platform == '__WXGTK__':
rect = None
self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
else:
rect = self.SetWindowShape()
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.SetSizer(sizer)
panel = wx.Panel(self, -1)
sizer.Add(panel, 1, wx.EXPAND)
### Add any controls you want to the panel here
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.Bind(wx.EVT_MOTION, self.OnMotion)
self.Bind(wx.EVT_PAINT, self.OnPaint)
if rect:
self.SetSize((rect.width, rect.height))
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.Draw(dc)
def Draw(self, dc):
mdc = wx.MemoryDC()
mdc.SelectObject(self.image)
w, h = self.image.GetWidth(), self.image.GetHeight()
dc.Blit(0, 0, w, h, mdc, 0, 0)
def OnLeftDown(self, event):
self.CaptureMouse()
x, y = self.ClientToScreen(event.GetPosition())
originx, originy = self.GetPosition()
dx = x - originx
dy = y - originy
self.delta = ((dx, dy))
def OnLeftUp(self, event):
if self.HasCapture():
self.ReleaseMouse()
def OnMotion(self, event):
if event.Dragging() and event.LeftIsDown():
x, y = self.ClientToScreen(event.GetPosition())
fp = (x - self.delta[0], y - self.delta[1])
self.Move(fp)
def SetWindowShape(self, event = None):
r = wx.RegionFromBitmap(self.image)
self.hasShape = self.SetShape(r)
return r.GetBox()
Regards,
Cliff
···
On Mon, 2004-09-06 at 17:50, Feroze Arif wrote:
--
Cliff Wells <clifford.wells@comcast.net>