wxpython 之 GDI 畫刷Brush(三)

來源:互聯網
上載者:User

畫筆是在地區中填充的繪圖工具。它用於繪製矩形、 橢圓等。 它有一種顏色和樣式屬性。

一. 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()

 

運行效果:

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.