wxPy半透明窗体

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值