本文介绍了如何在Excel用户窗体上的命令按钮添加悬停效果,通过向用户窗体添加代码,改善用户体验。悬停效果由两个类模块控制,一个是clFormHoverEffect,用于控制用户窗体和命令按钮的交互;另一个是clHoverButton,用于实现命令按钮的悬停效果。
文章中提到,通过在用户窗体上的命令按钮添加悬停效果,可以在用户鼠标移动到按钮上时触发特定效果,从而提高用户体验。
clFormHoverEffect模块用于控制用户窗体和命令按钮的交互,而clHoverButton模块则实现命令按钮的悬停效果。
用户在使用该功能时需要注意,只有启用且未锁定的命令按钮才能应用悬停效果。
Excel中的用户窗体是静态的,但如果用户窗体上的命令按钮具有悬停效果,就会改善用户体验。例如,当鼠标移动到命令按钮上时,会发生变化,如下图1所示。本文介绍了一种非常灵活的方法,通过向用户窗体中添加一些代码行,可以将悬停效果添加到用户窗体上的所有命令按钮中,而不需要对用户窗体进行进一步的更改,不需要添加其他控件等,只是多添加几行代码就足够了。悬停效果由两个类模块控制。类模块中,实现对相关用户窗体进行引用,并自动从该类引用用户窗体上的所有命令按钮。悬停效果主要使用命令按钮和用户窗体的MouseMove事件。这两个类模块控制命令按钮的悬停效果。首先在VBE中插入一个类模块,将其命名为clFormHoverEffect,添加以下代码:Private WithEvents frmHover As MSForms.UserForm
Private oColButtons As Collection, sHoveredButton As String
Private Sub Class_Initialize()
Set oColButtons = New Collection
End Sub
Friend Property
Set HoverForm(ByRef oFrm As Object)
Set frmHover = oFrm
End Property
Friend Sub AddButtons()
Dim ctl As Control, oHoverButton As clHoverButton
For Each ctl In frmHover.Controls
If TypeName(ctl) = "CommandButton" Then
Set oHoverButton = New
clHoverButton
With oHoverButton
Set .ParentForm = Me
Set .HoverButton = ctl
.Initialize
End With
oColButtons.Add oHoverButton, ctl.Name
Set oHoverButton = Nothing
End If
Next
End Sub
Private Sub frmHover_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
RemoveHoverEffect
End Sub
Friend Sub ApplyHoverEffect(ByVal sCtlName As String)
If sHoveredButton = vbNullString Or sHoveredButton <> sCtlName Then
If sHoveredButton <> sCtlName Then RemoveHoverEffect
frmHover.Controls(sCtlName).BackColor = &H85E8FF
sHoveredButton = sCtlName
End If
End Sub
Friend Sub RemoveHoverEffect()
If sHoveredButton <> vbNullString Then
frmHover.Controls(sHoveredButton).BackColor = oColButtons.Item(sHoveredButton).InitialBackColor
sHoveredButton = vbNullString
End If
End Sub
Friend Sub TerminateButtonHover()
Dim i As Integer
For i = 1 To oColButtons.Count
oColButtons.Remove 1
Next
Set oColButtons = Nothing
End Sub
Private Sub Class_Terminate()
Set frmHover = Nothing
End Sub
再插入一个类模块,将其命名为clHoverButton,添加以下代码:Private oParentForm As clFormHoverEffect, iInitialBackColor As Long
Private WithEvents oCmdBtn As MSForms.CommandButton
Friend Property Set ParentForm(ByRef oValue As clFormHoverEffect)
Set oParentForm = oValue
End Property
Friend Property Set HoverButton(ByRef oValue As MSForms.CommandButton)
Set oCmdBtn = oValue
End Property
Private Property Let InitialBackColor(ByVal iValue As Long)
iInitialBackColor = iValue
End Property
Public Property Get InitialBackColor() As Long
InitialBackColor = iInitialBackColor
End Property
Friend Sub Initialize()
With oCmdBtn
InitialBackColor = .BackColor
.MousePointer = fmMousePointerCustom
If Len(Dir(ThisWorkbook.Path & "\HandCursor.ico")) Then
.MouseIcon = LoadPicture(ThisWorkbook.Path & "\HandCursor.ico")
End If
End With
End Sub
Private Sub oCmdBtn_Click()
oParentForm.RemoveHoverEffect
End Sub
Private Sub oCmdBtn_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Not oCmdBtn.Locked Then
If X > 2 And X 2 And Y > 2 And Y 2 Then
oParentForm.ApplyHoverEffect oCmdBtn.Name
Else
oParentForm.RemoveHoverEffect
End If
End If
End Sub
Private Sub Class_Terminate()
Set oCmdBtn = Nothing
Set oParentForm = Nothing
End Sub
Private oHoverForm As New clFormHoverEffect
Private Sub UserForm_Initialize()
Set oHoverForm = New clFormHoverEffect
With oHoverForm
Set .HoverForm = Me
.AddButtons
End With
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
oHoverForm.TerminateButtonHover
Set oHoverForm = Nothing
End Sub
注:本文学习整理自worksheetsvba.com,供参考。欢迎在下面留言,完善本文内容,让更多的人学到更完美的知识。
欢迎到知识星球:完美Excel社群,进行技术交流和提问,获取更多电子资料,并通过社群加入专门的微信讨论群,更方便交流。