关于浮动按钮的实现思路不少,多是采取多图片重叠显示来实现。这种方法代码量多,实现起来较繁琐。因为,一个按钮还好,如果有十个按钮呢?一个按钮三个图片,十个就要三十个图片,可不是闹着玩的。
我的思路是:
舍弃CommandButton控件,每个按钮用4条Line控件和一个Label控件替代。4条Line围住Label的边缘,调入窗体时,置显示属性为False,并将左、上直线的颜色设为白色,右、下直线的颜色设为黑色。当鼠标移到Label上时,4条Line的显示属性置True;当鼠标离开按钮时,将4条Line的显示属性设置为False。这样在视觉上就完全得到立体浮动的效果。另外,VB的Line控件还支持直线倾斜,以此类推,完全可以做出更加美观的倾斜按钮。篇幅所限,下面仅给出一个按钮实现浮动效果的源代码。
Option Explicit
Private Sub Form_Load()
'初始Form与Label
Form1.Caption = “浮动按钮"
Form1.KeyPreview = False
label1.Caption = “确定"
'初始4条Line的显示属性为False
Line1.Visible = False
Line2.Visible = False
Line3.Visible = False
Line4.Visible = False
'初始4条Line的颜色
Line1.BorderColor = &HE0E0E0
Line2.BorderColor = &HE0E0E0
Line3.BorderColor = &H808080
Line4.BorderColor = &H808080
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'鼠标指针在窗体上(不在按钮上)时,置4条Line的显示属性为False
Line1.Visible = False
Line2.Visible = False
Line3.Visible = False
Line4.Visible = False
End Sub
Private Sub label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'鼠标指针在按钮上时,置4条Line的显示属性为True
Line1.Visible = True
Line2.Visible = True
Line3.Visible = True
Line4.Visible = True
End Sub
文章来源于领测软件测试网 https://www.ltesting.net/