畫筆是在地區中填充的繪圖工具。它用於繪製矩形、 橢圓等。 它有一種顏色和樣式屬性。
一. 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()
運行效果: