用VB实现QQ一样的菜单(半透明窗口)

这篇博客介绍如何利用Visual Basic (VB) 实现类似QQ的半透明菜单效果。通过声明并调用Windows API函数,设置窗口的WS_EX_LAYERED样式,调整透明度,实现了窗口的透明化。在窗体加载时,设置了窗体大小、背景颜色,并创建了一系列的命令按钮,模拟QQ的分组菜单。点击不同按钮时,会动态改变菜单项的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Option Explicit

'''窗口半透明声明开始
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As LongAs Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As LongAs Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As LongAs 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, 
0180, 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

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pzhan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值