65.9K
CodeProject 正在变化。 阅读更多。
Home

CLayeredWindowHelperST v1.0

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.64/5 (9投票s)

2002年2月18日

1分钟阅读

viewsIcon

95211

downloadIcon

2931

透明窗口的包装类。

Sample Image - CLayeredWindowHelperST.gif

摘要

CLayeredWindowHelperST 是一个包装类,包含了所有必需的 API,用于为您的应用程序添加对透明(分层)窗口的支持。
这是一个可扩展的类。 如果在 Windows 2000/XP 下运行,您的窗口
将具有选定的透明效果,如果在 Windows 9x/ME/NT 下运行,则会无错误地运行。

如何在您的应用程序中集成 CLayeredWindowHelperST

在你的项目中包含以下文件

  • LayeredWindowHelperST.h
  • LayeredWindowHelperST.cpp
创建 CLayeredWindowHelperST 的一个实例。 该类封装了所有必需的 API 来支持
透明窗口,并且如果在不支持此功能的旧 Windows 版本下运行,则可以无错误地运行。
您只需要一个实例,因此它可以是应用程序的全局变量。
CLayeredWindowHelperST G_Layered;
修改窗口的样式,然后设置透明效果。 对于基于对话框的应用程序,请在您的 OnInitDialog
// Call the base-class method
CDialog::OnInitDialog();

// Modify the style
G_Layered.AddLayeredStyle(m_hWnd);

// Set the trasparency effect to 70%
G_Layered.SetTransparentPercentage(m_hWnd, 70);
  -or-
G_Layered.SetLayeredWindowAttributes(m_hWnd, 0, 210, LWA_ALPHA);

类方法

AddLayeredStyle
WS_EX_LAYERED 样式添加到指定的窗口。
要获得透明效果,此样式是必需的

// Parameters:
//     [IN]   Handle to the window and, indirectly, the class to which the window belongs. 
//            Windows 95/98/Me: The AddLayeredStyle function may fail if the window 
//            specified by the hWnd parameter does not belong to the same process 
//            as the calling thread. 
//
// Return value:
//     Non zero
//            Function executed successfully.
//     Zero
//            Function failed. To get extended error information, call ::GetLastError().
//
LONG AddLayeredStyle(HWND hWnd)
SetLayeredWindowAttributes
设置透明(分层)窗口的不透明度和透明颜色键。
// Parameters:
//     [IN]   hWnd
//            Handle to the layered window.
//     [IN]   crKey
//            A COLORREF value that specifies the transparency color key to be used when
//            composing the layered window. All pixels painted by the window in this color will be transparent.
//            To generate a COLORREF, use the RGB() macro.
//     [IN]   bAlpha
//            Alpha value used to describe the opacity of the layered window.
//            When bAlpha is 0, the window is completely transparent.
//            When bAlpha is 255, the window is opaque. 
//     [IN]   dwFlags 
//            Specifies an action to take. This parameter can be one or more of the following values:
//              LWA_COLORKEY  Use crKey as the transparency color.  
//              LWA_ALPHA     Use bAlpha to determine the opacity of the layered window.
//
// Return value:
//     TRUE
//            Function executed successfully.
//     FALSE
//            Function failed. To get extended error information, call ::GetLastError().
//
BOOL SetLayeredWindowAttributes(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags)
SetTransparentPercentage
设置分层窗口的不透明度或透明度百分比。
// Parameters:
//     [IN]   hWnd
//            Handle to the layered window.
//     [IN]   byPercentage
//            Percentage (from 0 to 100)
//
// Return value:
//     Non zero
//            Function executed successfully.
//     Zero
//            Function failed. To get extended error information, call ::GetLastError().
//
BOOL SetTransparentPercentage(HWND hWnd, BYTE byPercentage)
GetVersionI
以短数值形式返回类版本。
// Return value:
//     Class version. Divide by 10 to get actual version.
//
static short GetVersionI()
GetVersionC
以字符串值的形式返回类版本。
// Return value:
//     Pointer to a null-terminated string containing the class version.
//
static LPCTSTR GetVersionC()

备注

要编译 CLayeredWindowHelperST,您需要 Platform SDK(2001 年 8 月或更高版本)。 这不是
强制性的,因为如果在编译时未找到它,它将被模拟。 在运行时,您不需要任何
安装了额外的 SDK 或库。

历史

  • v1.0 (17/January/2002)
    首次发布

免责声明

本软件和随附的文件按“原样”分发,不提供任何明示或暗示的保证。 不对可能的损害甚至功能承担任何责任。 用户必须承担使用本软件的全部风险。

© . All rights reserved.