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

VB无所不能之四:制作透明和半透明窗体
 
——作者:钟声
博客地址:http://useway.blog.51cto.com
 
 
我们经常可以看到这样的窗体,觉得很炫,如图所示:
 
 
        同样,对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()
         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 "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() 
         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_COLORKEY                 '将扣去窗口中的蓝色
End Sub
 
运行结果如下所示:
 
 
Java程序员,上班那点事儿》,前言,目录
卓越网销售链接
China-pub销售链接
当当网销售链接
《Java程序员,上班那点事》纪念帖
 

你可能感兴趣的:(VB无所不能之四:制作透明和半透明窗体)