def OnPaint(self, evt): width, height = self.GetClientSize() text = 'Alpha test' font = self.GetFont() font.SetPointSize(font.GetPointSize() + 16) font = font.MakeBold() font = font.MakeItalic() bmp = wx.Bitmap(width, height) bmp2 = wx.Bitmap(width, height) bmp3 = wx.Bitmap(width, height) dc = wx.MemoryDC() dc.SelectObject(bmp3) dc.SetTextForeground(wx.Colour(255, 255, 255, 255)) dc.SetFont(font) text_width, text_height = dc.GetFullTextExtent(text)[:2] text_x = (width / 2) - (text_width / 2) text_y = (height / 2) - (text_height / 2) dc.DrawText(text, text_x, text_y) dc.SelectObject(bmp2) dc.SetPen(wx.Pen(wx.Colour(255, 255, 255, 255), 6)) dc.SetBrush(wx.Brush(wx.Colour(255, 255, 255, 255))) dc.DrawRoundedRectangle(103, 78, width - 200 - 3, height - 150 - 3, 5) dc.SelectObject(bmp) gc = wx.GraphicsContext.Create(dc) gcdc = wx.GCDC(gc) region1 = wx.Region(bmp) region2 = wx.Region(bmp2, wx.Colour(0, 0, 0, 0)) region1.Subtract(region2) gcdc.SetDeviceClippingRegion(region1) gc.SetPen(wx.Pen(wx.Colour(255, 0, 0, 140), 6)) gc.SetBrush(wx.Brush(wx.Colour(0, 0, 0, 150))) gc.DrawRoundedRectangle(3, 3, width-6, height-6, 5) gcdc.DestroyClippingRegion() gc.SetPen(wx.Pen(wx.Colour(255, 0, 0, 120), 6)) gc.SetBrush(wx.Brush(wx.Colour(0, 0, 0, 0))) gc.DrawRoundedRectangle(103, 78, width - 200 - 3, height - 150 - 3, 5) region1 = wx.Region(bmp) region2 = wx.Region(bmp3, wx.Colour(0, 0, 0, 0)) region1.Subtract(region2) gcdc.SetDeviceClippingRegion(region1) gcdc.SetTextForeground(wx.Colour(0, 0, 0, 130)) gcdc.SetTextBackground(wx.TransparentColour) gcdc.SetFont(font) gc.DrawText(text, text_x - 4, text_y - 4) gcdc.DestroyClippingRegion() gcdc.SetTextForeground(wx.Colour(0, 255, 0, 150)) gcdc.SetTextBackground(wx.TransparentColour) gc.DrawText(text, text_x, text_y) gcdc.Destroy() del gcdc dc.SelectObject(wx.NullBitmap) dc.Destroy() del dc region = wx.Region(bmp) self.SetShape(region) self.draw_alpha(bmp) def draw_alpha(self, bmp): hndl = self.GetHandle() style = GetWindowLongW(HWND(hndl), INT(GWL_EXSTYLE)) SetWindowLongW(HWND(hndl), INT(GWL_EXSTYLE), LONG(style | WS_EX_LAYERED)) hdcDst = GetDC(GetDesktopWindow()) hdcSrc = CreateCompatibleDC(HDC(hdcDst)) pptDst = POINT(*self.GetPosition()) psize = SIZE(*self.GetClientSize()) pptSrc = POINT(0, 0) crKey = RGB(0, 0, 0) pblend = BLENDFUNCTION(AC_SRC_OVER, 0, 255, AC_SRC_ALPHA) SelectObject(HDC(hdcSrc), HGDIOBJ(bmp.GetHandle())) UpdateLayeredWindow( HWND(hndl), HDC(hdcDst), ctypes.byref(pptDst), ctypes.byref(psize), HDC(hdcSrc), ctypes.byref(pptSrc), crKey, ctypes.byref(pblend), DWORD(ULW_ALPHA) )
主要函数:
BLENDFUNCTION(AC_SRC_OVER, 0, 255, AC_SRC_ALPHA)
UpdateLayeredWindow
使用DesktopWindow的hdc