VB无所不能之四:制作透明和半透明窗体

前端之家收集整理的这篇文章主要介绍了VB无所不能之四:制作透明和半透明窗体前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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" (ByVal hwnd As Long,ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" () Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long,ByVal nIndex As Long,ByVal dwNewLong As Long) As Long
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

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() 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,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 "SetWindowLongA" () SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long,ByVal nIndex As Long,ByVal dwNewLong As Long) As Long
SetLayeredWindowAttributes Lib "user32" () SetLayeredWindowAttributes Lib "user32" ( SetLayeredWindowAttributes Lib "user32" () SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long,ByVal crKey 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() Sub Form_Load()
Dim rtn As Long
BorderStyler = 0
rtn = GetWindowLong(hwnd,GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong hwnd,rtn
SetLayeredWindowAttributes hwnd,&HFF0000,LWA_COLORKEY '将扣去窗口中的蓝色
End Sub
运行结果如下所示:

原文链接http://www.jb51.cc/article/p-olftcswb-qm.html

猜你在找的VB相关文章