【vb】三种透明窗体的写法

本文由多篇[旧博客]组合迁移。文章一文章二文章三

窗体透明一:窗体透明,控件不透明

upload successful

鼠标不穿过空白窗体

这种写法是当鼠标点击空白窗体时,接收的对象还是vb From

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 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
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_ALPHA = &H2
Private Const LWA_COLORKEY = &H1
Private Sub Form_Load()
Me.BackColor = &HFF0000
Dim rtn As Long
Dim BorderStyler
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

鼠标穿过空白窗体

1
2
3
4
5
6
7
8
9
10
11
12
Option Explicit
'--窗体透明,控件不透明
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 SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Const WS_EX_LAYERED = &H80000
Const GWL_EXSTYLE = (-20)
Const LWA_COLORKEY = &H1
Private Sub Form_Load()
BackColor = &HFEFEFE
SetWindowLong hwnd, GWL_EXSTYLE, WS_EX_LAYERED
SetLayeredWindowAttributes hwnd, &HFEFEFE, 0, LWA_COLORKEY '--这里的 0 表示透明度
End Sub

全透明,可以设置透明度

1
2
3
4
5
6
7
8
9
10
11
12
Option Explicit
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 SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Const WS_EX_LAYERED = &H80000
Const GWL_EXSTYLE = (-20)
Const LWA_ALPHA = &H2
Private Sub Form_Load()
Const ALPHA = 150
BackColor = &HFEFEFE
SetWindowLong hwnd, GWL_EXSTYLE, WS_EX_LAYERED
SetLayeredWindowAttributes hwnd, &HFEFEFE, ALPHA, LWA_ALPHA
End Sub

窗体透明二:窗体逐渐变透明(包括控件)

upload successful

首进行以下三个步骤

  • 添加一个shape
  • 添加一个PicturebBox,依它为容器添加一个shape,背景色设为蓝色
  • 添加一个时钟控件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Const WS_EX_LAYERED = &H80000
Const GWL_EXSTYLE = (-20)
Const LWA_ALPHA = &H2
Const LWA_COLORKEY = &H1
Dim tmd As Long
Private Sub Form_Load()
Show
Shape1.BackColor = &H80000002
Shape1.BackStyle = 1
tmd = 255
Timer1.Interval = 50
Shape1.Width = Picture1.Width
SetWindowLong hwnd, GWL_EXSTYLE, WS_EX_LAYERED
SetLayeredWindowAttributes hwnd, 0, tmd, LWA_ALPHA '越少越透明,限制0-255
End Sub

Private Sub Timer1_Timer()
On Error Resume Next
tmd = tmd - 1
SetLayeredWindowAttributes hwnd, 0, tmd, LWA_ALPHA
Shape1.Width = Shape1.Width - Picture1.Width / 255
If tmd < 0 Then
Timer1.Enabled = False
MsgBox "OK!"
End If
End Sub

窗体透明三:窗体、控件变透明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const WS_EX_LAYERED = &H80000
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_ALPHA = &H2
Private Const LWA_COLORKEY = &H1
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Sub SetFormTranslucency(hwnd As Long, crKey As Long, bAlpha As Byte, dwFlags As Long)'实现半透明窗体
Dim rtn As Long
rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong hwnd, GWL_EXSTYLE, rtn
SetLayeredWindowAttributes hwnd, crKey, bAlpha, dwFlags

End Sub
Private Sub Form_Load()
Call SetFormTranslucency(Me.hwnd, 0, 200, 2) '窗体半透明,200为透明值
End Sub

【参考文章】
如何用VB实现半透明控件@huanglenzhi