2024年7月2日发(作者:)
VB无所不能之四:制作透明和半透明窗体
——作者:钟声
博客地址:
我们经常可以看到这样的窗体,觉得很炫,如图所示:
同样,对Windows系统方面的编程似乎首先想到的绝对不是VB,而大部分程序员
想到的一定是VC。
其实,VB对于这个实现非常方便且简单,用到了“user32”中的
SetLayeredWindowAttributes()函数。
SetLayeredWindowAttributes()函数介绍:
函数声明:
Declare Function SetLayeredWindowAttributes Lib "user32" () Declare
Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal
crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
hwnd是透明窗体的句柄,
crKey为颜色值,
bAlpha是透明度,取值范围是[0,255],
dwFlags是透明方式,可以取两个值:当取值为LWA_ALPHA时,crKey参数无效,
bAlpha参数有效;
当取值为LWA_COLORKEY时,bAlpha参数有效而窗体中的所有颜色为crKey的地
方将变为透明。
下面我们做两个实验:
第一个:做一个半透明窗体
步骤一:打开VB建立一个窗体Form
步骤二:将窗体背景颜色设为:&HFF0000
步骤三:将下面代码粘贴到程序中:
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ()
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (By
Val hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ()
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByV
al hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Lon
g
Declare Function SetLayeredWindowAttributes Lib "user32" () Declare Fun
ction SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal c
rKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Const WS_EX_LAYERED = &H80000
Const GWL_EXSTYLE = () Const GWL_EXSTYLE = (-20)
Private Const LWA_ALPHA = &H2
Private Const LWA_COLORKEY = &H1
Sub Form_Load()
Dim rtn As Long
rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong hwnd, GWL_EXSTYLE, rtn
SetLayeredWindowAttributes hwnd, 0, 100, LWA_ALPHA
End Sub
运行结果如下:
第一个:做一个异型窗体
在之前的窗体上放置一个图片如图所示:
将下面代码粘贴到程序中:
GetWindowLong Lib "user32" Alias "GetWindowLongA" () GetWindowLong
Lib "user32" Alias "GetWindowLongA" ( GetWindowLong Lib "user32" Alias "
GetWindowLongA" () GetWindowLong Lib "user32" Alias "GetWindowLongA"
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
SetWindowLong Lib "user32" Alias "SetWindowLongA" () SetWindowLong
Lib "user32" Alias "SetWindowLongA" ( SetWindowLong Lib "user32" Alias "S
etWindowLongA" () SetWindowLong Lib "user32" Alias "SetWindowLongA" (B
yVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As L
ong
SetLayeredWindowAttributes Lib "user32" () SetLayeredWindowAttributes L
ib "user32" ( SetLayeredWindowAttributes Lib "user32" () SetLayeredWindowA
ttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlp
ha As Byte, ByVal dwFlags As Long) As Long
Private Const WS_EX_LAYERED = &H80000
Const GWL_EXSTYLE = () Const GWL_EXSTYLE = (-20)
Private Const LWA_ALPHA = &H2
Private Const LWA_COLORKEY = &H1
Sub Form_Load()
Dim rtn As Long
BorderStyler = 0
rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong hwnd, GWL_EXSTYLE, rtn
SetLayeredWindowAttributes hwnd, &HFF0000, 0, LWA_COLORKE
Y '将扣去窗口中的蓝色
End Sub
运行结果如下所示:
发布者:admin,转转请注明出处:http://www.yc00.com/news/1719901255a2759145.html
评论列表(0条)