vb实现透明窗体

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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"() Decl are Function GetWindowLong Lib "user32"Alias "GetWindowLongA"(ByVal hwn d As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32"Alias "SetWindowLongA"() Decl are Function SetWindowLong Lib "user32"Alias "SetWindowLongA"(ByVal hwn d 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 A s 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 "GetWind owLongA"() GetWindowLong Lib "user32"Alias "GetWindowLongA"(ByVal hwn d As Long, ByVal nIndex As Long) As Long
SetWindowLong Lib "user32"Alias "SetWindowLongA"() SetWindowLong Lib "u ser32"Alias "SetWindowLongA"( SetWindowLong Lib "user32"Alias "SetWindo wLongA"() SetWindowLong Lib "user32"Alias "SetWindowLongA"(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long SetLayeredWindowAttributes Lib "user32"() SetLayeredWindowAttributes Lib "u ser32"( SetLayeredWindowAttributes Lib "user32"() SetLayeredWindowAttribut es 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_COLORKE
Y '将扣去窗口中的蓝色
End Sub
运行结果如下所示:。

相关文档
最新文档