zoukankan      html  css  js  c++  java
  • 设置窗口透明效果

    设置窗口透明一般都能用分层窗体来实现,这样的代码到网上一搜一大把的,我在这里简单地封装一下以供以后自己方便地使用。
    #ifndef   _WINDOW_TRANSPARENT_H_
    #define  _WINDOW_TRANSPARENT_H_

    #ifndef WS_EX_LAYERED
        
    #define LWA_COLORKEY            0x00000001
        
    #define LWA_ALPHA                   0x00000002
        
    #define WS_EX_LAYERED           0x00080000
    #endif 

    //分层窗口只在WIndows 2000 及以后版本有效,故用 _WIN32_WINNT >= 0x0500做判断

    class CWindowTransparent
    {
        typedef BOOL (WINAPI 
    *LPFNSETLAYEREDWINDOWATTRIBUTES)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags); 
    private:
        HMODULE   m_hModUser32;
        LPFNSETLAYEREDWINDOWATTRIBUTES  lpfnSetLayeredWindowAttributes;

    public:
        CWindowTransparent( )
        {
    #if _WIN32_WINNT >= 0x0500
            
    //获取user32.dll模块
            m_hModUser32  = ::GetModuleHandle( _T("user32.dll") ); 
            ATLASSERT( ( m_hModUser32 
    != NULL ) && ( _T("Get user32.dll Module Failed!")) );

            
    //获取SetLayeredWindowAttributes函数地址
            lpfnSetLayeredWindowAttributes  =  (LPFNSETLAYEREDWINDOWATTRIBUTES)::GetProcAddress( m_hModUser32, "SetLayeredWindowAttributes" ); 
            ATLASSERT( ( lpfnSetLayeredWindowAttributes 
    != NULL ) && ( _T("Get SetLayeredWindowAttributes Function Failed!")) );
    #endif
        }

        
    /*
        * 通过RGB值设置窗口透明
        * hWnd: 要设置透明的窗口句柄
        * clrTran: 透明色
        
    */
        
    void SetWindowTransparentRGBStyle( HWND  hWnd,  COLORREF clrTran = RGB(255,0,255))
        {
            ATLASSERT( ::IsWindow( hWnd ) );
            
    //设置窗口的扩展属性
            LONG lStyle = ::GetWindowLong( hWnd, GWL_EXSTYLE );
            ::SetWindowLong( hWnd, GWL_EXSTYLE, lStyle
    |WS_EX_LAYERED ); 

    #if _WIN32_WINNT >= 0x0500
            lpfnSetLayeredWindowAttributes( hWnd, clrTran, 
    255, LWA_COLORKEY); 
    #endif
        }

        
    /*
        * 通过Alpha值设置窗口透明
        * hWnd: 要设置透明的窗口句柄
        * bAlpha: Alpha透明值
        
    */
        
    void SetWindowTransparentAlphaStyle( HWND  hWnd,  BYTE  bAlpha = 255 )
        {
            ATLASSERT( ::IsWindow( hWnd ) );
            
    //设置窗口的扩展属性
            LONG lStyle = ::GetWindowLong( hWnd, GWL_EXSTYLE );
            ::SetWindowLong( hWnd, GWL_EXSTYLE, lStyle
    |WS_EX_LAYERED ); 

    #if _WIN32_WINNT >= 0x0500
            lpfnSetLayeredWindowAttributes( hWnd, RGB(
    0,0,0), bAlpha, LWA_ALPHA ); 
    #endif
        }
    };


    #endif
  • 相关阅读:
    SpringBoot RequestBody ajax提交对象
    微信小程序常用样式汇总
    微信小程序常用控件汇总
    java多线程实现多客户端socket通信
    客户端连接Codis集群
    crontab 解析
    在 RHEL/CentOS 7 上配置NTP时间服务器
    tomcat的bin目录中startup.bat/tomcat.6.exe/tomcat6w.exe区别
    Windows 下tomcat安装及将多个tomcat注册为Windows服务
    Oracle 数据库排错之 ORA-00600
  • 原文地址:https://www.cnblogs.com/fangkm/p/1434321.html
Copyright © 2011-2022 走看看