Option Explicit
'''窗口半透明声明开始
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()
''''''窗口半透明代码开始
Dim rtn As Long
rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong hwnd, GWL_EXSTYLE, rtn
SetLayeredWindowAttributes hwnd, 0, 180, LWA_ALPHA ' 透明度为 0--255 之间的数
''''''窗口半透明代码结束
With Me
.Width = 1200 + 155
.Height = 4860 + 355
.BorderStyle = 3
.ScaleMode = 1
.BackColor = &H80C0FF
.FillStyle = 1
End With
With Picture1
.Width = 1200 + 60
.Height = 4860
End With
Dim i As Integer
For i = Command1.Count - 1 To 0 Step -1
With Command1(i)
.Width = 1200
.Height = 300
.Top = Picture1.ScaleHeight - 300 * (Command1.Count - i)
.Left = 0
.Caption = "分组 " & i + 1
End With
Next i
Command1(0).Top = 0
End Sub
Private Sub Command1_Click(Index As Integer)
Picture1.SetFocus
'把焦点给Picture1是为了不让按钮出现难看的黑框
Dim i As Integer
For i = 1 To Index
Command1(i).Top = 300 * i
Next i
For i = Command1.Count - 1 To Index + 1 Step -1
Command1(i).Top = Picture1.ScaleHeight - 300 * (Command1.Count - i)
Next i
End Sub
'''窗口半透明声明开始
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()
''''''窗口半透明代码开始
Dim rtn As Long
rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong hwnd, GWL_EXSTYLE, rtn
SetLayeredWindowAttributes hwnd, 0, 180, LWA_ALPHA ' 透明度为 0--255 之间的数
''''''窗口半透明代码结束
With Me
.Width = 1200 + 155
.Height = 4860 + 355
.BorderStyle = 3
.ScaleMode = 1
.BackColor = &H80C0FF
.FillStyle = 1
End With
With Picture1
.Width = 1200 + 60
.Height = 4860
End With
Dim i As Integer
For i = Command1.Count - 1 To 0 Step -1
With Command1(i)
.Width = 1200
.Height = 300
.Top = Picture1.ScaleHeight - 300 * (Command1.Count - i)
.Left = 0
.Caption = "分组 " & i + 1
End With
Next i
Command1(0).Top = 0
End Sub
Private Sub Command1_Click(Index As Integer)
Picture1.SetFocus
'把焦点给Picture1是为了不让按钮出现难看的黑框
Dim i As Integer
For i = 1 To Index
Command1(i).Top = 300 * i
Next i
For i = Command1.Count - 1 To Index + 1 Step -1
Command1(i).Top = Picture1.ScaleHeight - 300 * (Command1.Count - i)
Next i
End Sub
这篇博客介绍如何利用Visual Basic (VB) 实现类似QQ的半透明菜单效果。通过声明并调用Windows API函数,设置窗口的WS_EX_LAYERED样式,调整透明度,实现了窗口的透明化。在窗体加载时,设置了窗体大小、背景颜色,并创建了一系列的命令按钮,模拟QQ的分组菜单。点击不同按钮时,会动态改变菜单项的位置。
193

被折叠的 条评论
为什么被折叠?



