zoukankan      html  css  js  c++  java
  • 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()

    运行效果:

  • 相关阅读:
    VS2008 环境中完美搭建 Qt 4.7.4 静态编译的调试与发布 Inchroy's Blog 博客频道 CSDN.NET
    编写可丢弃的代码
    c++ using namespace std; 海明威 博客园
    解决MySQL server has gone away
    nginx upstream 调度策略
    (2006, 'MySQL server has gone away') 错误解决 dba007的空间 51CTO技术博客
    Linux IO模型漫谈(2) 轩脉刃 博客园
    redis源码笔记 initServer 刘浩de技术博客 博客园
    MySQLdb批量插入数据
    词库的扩充百度百科的抓取你知道这些热词吗? rabbit9898 ITeye技术网站
  • 原文地址:https://www.cnblogs.com/ankier/p/2867289.html
Copyright © 2011-2022 走看看