python 透明窗体2

QQ群:476842922(欢迎加群讨论学习
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 27 19:22:11 2018

@author: Administrator
"""
import wx

class AppFrame( wx.Frame )  :

    def __init__( self )  :

        wx.Frame.__init__( self, None, title="Am I transparent?",
                           style=wx.DEFAULT_FRAME_STYLE | wx.STAY_ON_TOP )
        self.SetClientSize( (300, 300) )

        self.alphaValue = 255
        self.alphaIncrement = -4

        pnl = wx.Panel( self )
        self.stTxt = wx.StaticText( pnl, -1, str( self.alphaValue ), (25, 25) )
        self.stTxt.SetFont( wx.Font( 18, wx.SWISS, wx.NORMAL, wx.NORMAL ) )

        self.changeAlpha_timer = wx.Timer( self )
        self.changeAlpha_timer.Start( 50 )       # 20 changes per second
        self.Bind( wx.EVT_TIMER, self.ChangeAlpha )

        self.Bind( wx.EVT_CLOSE, self.OnCloseWindow )

    #end AppFrame class

    #--------------------------------------------------------

    def ChangeAlpha( self, evt )  :
        """ The term "alpha" means variable transparency
              as opposed to a "mask" which is binary transparency.
              alpha == 255 :  fully opaque
              alpha ==   0 :  fully transparent (mouse is ineffective!)

            Only top-level controls can be transparent; no other controls can.
            This is because they are implemented by the OS, not wx.
        """

        self.alphaValue += self.alphaIncrement
        if (self.alphaValue) <= 0 or (self.alphaValue >= 255) :

            # Reverse the increment direction.
            self.alphaIncrement = -self.alphaIncrement

            if self.alphaValue <= 0 :
                self.alphaValue = 0

            if self.alphaValue > 255 :
                self.alphaValue = 255
        #end if

        self.stTxt.SetLabel( str( self.alphaValue ) )

        # Note that we no longer need to use ctypes or win32api to
        # make transparent windows, however I'm not removing the
        # MakeTransparent code from this sample as it may be helpful
        # to someone for other uses, someday.

        #self.MakeTransparent( self.alphaValue )

        # Instead, just call the SetTransparent() method
        self.SetTransparent( self.alphaValue )      # Easy !

    #end ChangeAlpha def

    #--------------------------------------------------------

    def OnCloseWindow( self, evt ) :

        self.changeAlpha_timer.Stop()
        del self.changeAlpha_timer       # avoid a memory leak
        self.Destroy()

    #-----------------------------------------------------

    def MakeTransparent( self, amount ) :
        """
        This is how the method SetTransparent() is implemented
            on all MS Windows platforms.
        """
        import os
        if os.name == 'nt' :  # could substitute: sys.platform == 'win32'

            hwnd = self.GetHandle()
            try :
                import ctypes   # DLL library interface constants' definitions
                _winlib = ctypes.windll.user32    # create object to access DLL file user32.dll
                style = _winlib.GetWindowLongA( hwnd, 0xffffffec )
                style |= 0x00080000
                _winlib.SetWindowLongA( hwnd, 0xffffffec, style )
                _winlib.SetLayeredWindowAttributes( hwnd, 0, amount, 2 )

            except ImportError :

                import win32api, win32con, winxpgui
                _winlib = win32api.LoadLibrary( "user32" )
                pSetLayeredWindowAttributes = win32api.GetProcAddress(
                    _winlib, "SetLayeredWindowAttributes" )
                if pSetLayeredWindowAttributes == None :
                    return
                exstyle = win32api.GetWindowLong( hwnd, win32con.GWL_EXSTYLE )
                if 0 == ( exstyle & 0x80000 ) :
                    win32api.SetWindowLong( hwnd,
                                           win32con.GWL_EXSTYLE,
                                           exstyle | 0x80000 )
                winxpgui.SetLayeredWindowAttributes( hwnd, 0, amount, 2 )
        else :
            print('####  OS Platform must be MS Windows')
            self.Destroy()
        #end if
    #end MakeTransparent def
#end AppFrame class
#=======================================================

if __name__ == '__main__' :

    app = wx.App( False )
    frm = AppFrame()
    frm.Show()
    app.MainLoop()

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值