wxpython 之 GDI 画刷Brush(三)

画笔是在区域中填充的绘图工具。它用于绘制矩形、 椭圆等。 它有一种颜色和样式属性。

一. dc.SetBackground(brush) 利用该方法,保持控件的背景色与父窗口被景色一致。

  具体使用方式:

  1. 首先获得被景色的画刷,brush = self.GetBackgroundBrush(dc)

def GetBackgroundBrush(self, dc):
        colBg = self.GetBackgroundColour()
        brush = wx.Brush(colBg, wx.SOLID)        
        myAttr = self.GetDefaultAttributes()
        parAttr = self.GetParent().GetDefaultAttributes()
        myDef = colBg == myAttr.colBg
        parDef = self.GetParent().GetBackgroundColour() == parAttr.colBg
        if myDef and parDef:
            if wx.Platform == "__WXMAC__":
                brush.MacSetTheme(1) # 1 == kThemeBrushDialogBackgroundActive
            elif wx.Platform == "__WXMSW__":
                if self.DoEraseBackground(dc):
                    brush = None
        elif myDef and not parDef:
            colBg = self.GetParent().GetBackgroundColour()
            brush = wx.Brush(colBg, wx.SOLID)            
        return brush

  2. 在OnPaint方法中,绘制一致的被景色。

if brush is not None:
            dc.SetBackground(brush)
            dc.Clear()

二.   利用wx.GCDC绘制半透明效果。普通的DC不带透明效果。

## #details
    #   绘制带透明背景的矩形区域
    def __DrawAlphaRectangle(self, dc):
        
        try:
            gcdc = wx.GCDC(dc)
        except:
            gcdc = dc
        
        alphaColor = wx.Color(0, 0, 0, 128)#半透明
        brush = wx.Brush(alphaColor)
        gcdc.SetBrush(brush)
        gcdc.DrawRectangle(40, 40, 40, 50)
        
    def __DrawSimpleRectangle(self, dc):
        alphaColor = wx.Color(0, 0, 0, 128)#半透明,但是在该dc下不会起效果
        brush = wx.Brush(alphaColor)
        dc.SetBrush(brush)
        dc.DrawRectangle(100, 100, 40, 50) 
运行效果:

三.   DrawBitmp在dc上画透明背景png图像及绘制disable样式的效果。

   def __DrawPNG(self, dc):
        bmp = wx.Bitmap('earth.png') 
        dc.DrawBitmap(bmp, 0, 0)
        
        image = wx.ImageFromBitmap(bmp)
        imageutils.grayOut(image)
        dc.DrawBitmap(wx.BitmapFromImage(image), 240, 0)

运行效果:

 

四. region区域绘图。

 def __DrawInClippedRegion(self, dc):
        region = wx.RegionFromPoints(((20, 20),(40, 20), (40,0), (100, 200), (20,200)))
        dc.SetClippingRegionAsRegion(region)
        
        #在clipregion 里面作图像
        alphaColor = wx.Color(0, 0, 0, 128)#半透明,但是在该dc下不会起效果
        brush = wx.Brush(alphaColor)
        dc.SetBrush(brush)
        dc.DrawRectangle(0, 0, 500, 500)
        
        dc.DestroyClippingRegion()

运行效果:

原文地址:https://www.cnblogs.com/ankier/p/2867289.html