制作半透明窗体
使用SetLayeredWindowAttributes 函数,可以
轻松的实现半透明窗体。按照微软的明窗体窗体
在创建时应使用WS- EX-LAYERED
参数(用CreateWindowEx) ,或者在创建后设置
该参数(用SetWindowLong) ,我选用后者。全部
函数、常量声明如下:
Private Declare Function GetWindowLong Lib
“user32”Alias“GetWindowLongA”(ByVal hwnd
As Long , ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib
“user32”Alias“SetWindowLongA”(ByVal hwnd
As Long , ByVal nIndex As Long , ByVal
dwNewLong As Long) As Long
Private Declare Function SetLayeredWindow
Attributes Lib“user32”(ByVal hwnd As Long , By2
Val crKey As Long , ByVal bAlpha As Byte , ByVal
dwFlags As Long) As Long 其中hwnd 是透明窗体
的句柄,crKey 为颜色值,bAlpha 是透明度,取值
范围是[0 ,255 ] ,dwFlags 是透明方式,可以取两
个值: 当取值为LWA-ALPHA 时,crKey 参数无
效,bAlpha 参数有效;当取值为LWA COLOR2
KEY时,bAlpha 参数有效而窗体中的所有颜色
为crKey 的地方将变为透明———这个功能很有
用:我们不必再为建立不规则形状的窗体而调
用一大堆区域分析、创建、合并函数了,只需指
定透明处的颜色值即可。具体代码如下:
Private Const WS EXLAYERED = &H80000
Private Const GWL EXSTYLE = ( - 20)
Private Const LWA ALPHA = &H2
Private Const LWA COLORKEY = &H1
Private Sub Form Load ()
Dim rtn As Long
rtn = GetWindowLong ( hwnd , GWL EX2
STYLE)
rtn = rtn Or WS EXLAYERED
SetWindowLong hwnd , GWL EXSTYLE , rtn
SetLayeredWindowAttributes hwnd , 0 , 200 ,
LWA ALPHA
End Sub