如何实现VBA UserForm_Initialize与所有Label鼠标悬停效果的合并?
问题描述
我有一个UserForm1,其中包含UserForm_Initialize过程用于定义所有Label控件的属性,另有一段实现单个Label鼠标悬停效果的代码。我尝试将两段代码合并,让所有Label控件都具备鼠标悬停效果,但未能成功。
现有UserForm初始化代码
Sub UserForm1_Initialize() '.... 我的其他代码 Dim ctrl As MSForms.Control Dim index As Integer index = 1 ' Label的起始索引 For Each ctrl In UserForm1.Controls If TypeOf ctrl Is MSForms.Label Then 'And ctrl.Tag = "LabelAlignmentTheme" Then Dim label As MSForms.Label Set label = ctrl Set label.Picture = UserForm1.GIF.Picture ' 从图片控件读取图片 label.PicturePosition = fmPicturePositionLeftCenter End If Next For Each ctrl In UserForm1.Controls If TypeName(ctrl) = "Label" Then With ctrl .FontSize = 10 .FontName = "Calibri" .ForeColor = &H464646 '(深灰色) .BackColor = RGB(255, 255, 255) '(白色) .BorderStyle = fmBorderStyleSingle .BorderColor = &HA9A9A9 '(浅灰色) .TextAlign = fmTextAlignCenter If .Name = "LabelNoData" Then .ForeColor = RGB(255, 0, 0) .FontBold = True .BorderStyle = fmBorderStyleNone .FontSize = 12 .BackColor = &H8000000F .Visible = False End If End With End If Next ctrl End Sub
单个Label的鼠标悬停效果代码
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) If active = False Then Label1.BackColor = RGB(204, 255, 229) '蓝绿色 active = True End If End Sub Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) If active = True Then Label1.BackColor = RGB(255, 255, 255) '白色 active = False End If End Sub
我的尝试(未成功)
我尝试在初始化代码中直接调用悬停相关的过程,但没有效果:
Sub UserForm1_Initialize() '.... 我的其他代码 Dim ctrl As MSForms.Control Dim index As Integer index = 1 ' Label的起始索引 For Each ctrl In UserForm1.Controls If TypeOf ctrl Is MSForms.Label Then 'And ctrl.Tag = "LabelAlignmentTheme" Then Dim label As MSForms.Label Set label = ctrl Set label.Picture = UserForm1.GIF.Picture ' 从图片控件读取图片 label.PicturePosition = fmPicturePositionLeftCenter End If Next For Each ctrl In UserForm1.Controls If TypeName(ctrl) = "Label" Then With ctrl .FontSize = 10 .FontName = "Calibri" .ForeColor = &H464646 '(深灰色) .BackColor = RGB(255, 255, 255) '(白色) .BorderStyle = fmBorderStyleSingle .BorderColor = &HA9A9A9 '(浅灰色) .TextAlign = fmTextAlignCenter UserForm_MouseMove ' 尝试在这里合并 Label_MouseMove ' 尝试在这里合并 If .Name = "LabelNoData" Then .ForeColor = RGB(255, 0, 0) .FontBold = True .BorderStyle = fmBorderStyleNone .FontSize = 12 .BackColor = &H8000000F .Visible = False End If End With End If Next ctrl End Sub
我构思的通用悬停代码
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) If Not activeLabel Is Nothing Then activeLabel.BackColor = RGB(255, 255, 255) '白色 Set activeLabel = Nothing End If End Sub Private Sub Label_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) If TypeName(Me.ActiveControl) = "Label" Then If Not activeLabel Is Nothing Then activeLabel.BackColor = RGB(255, 255, 255) '白色 End If Set activeLabel = Me.ActiveControl activeLabel.BackColor = RGB(204, 255, 229) '蓝绿色 End If End Sub
解决方案
VBA无法直接通过通用的Label_MouseMove事件处理所有Label控件,必须通过类模块实现通用事件绑定,步骤如下:
步骤1:创建类模块
- 插入一个类模块,命名为
LabelHover - 在类模块中添加以下代码:
Public WithEvents HoverLabel As MSForms.Label Private Const HOVER_COLOR As Long = &HCCFFE5 ' RGB(204,255,229) Private Const NORMAL_COLOR As Long = &HFFFFFF ' RGB(255,255,255) Private Sub HoverLabel_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) ' 恢复之前激活的Label颜色 If Not UserForm1.activeLabel Is Nothing Then UserForm1.activeLabel.BackColor = NORMAL_COLOR End If ' 设置当前Label的悬停颜色 Set UserForm1.activeLabel = HoverLabel HoverLabel.BackColor = HOVER_COLOR End Sub
步骤2:在UserForm中声明变量并绑定事件
在UserForm1的代码模块顶部添加全局变量:
Private labelCollection As Collection Public activeLabel As MSForms.Label
修改UserForm1_Initialize过程,添加事件绑定代码:
Sub UserForm1_Initialize() '.... 我的其他代码 Dim ctrl As MSForms.Control Dim index As Integer Dim hoverLabel As LabelHover Set labelCollection = New Collection index = 1 ' Label的起始索引 For Each ctrl In UserForm1.Controls If TypeOf ctrl Is MSForms.Label Then 'And ctrl.Tag = "LabelAlignmentTheme" Then Dim label As MSForms.Label Set label = ctrl Set label.Picture = UserForm1.GIF.Picture ' 从图片控件读取图片 label.PicturePosition = fmPicturePositionLeftCenter End If Next For Each ctrl In UserForm1.Controls If TypeName(ctrl) = "Label" Then With ctrl .FontSize = 10 .FontName = "Calibri" .ForeColor = &H464646 '(深灰色) .BackColor = RGB(255, 255, 255) '(白色) .BorderStyle = fmBorderStyleSingle .BorderColor = &HA9A9A9 '(浅灰色) .TextAlign = fmTextAlignCenter If .Name = "LabelNoData" Then .ForeColor = RGB(255, 0, 0) .FontBold = True .BorderStyle = fmBorderStyleNone .FontSize = 12 .BackColor = &H8000000F .Visible = False Else ' 绑定悬停事件 Set hoverLabel = New LabelHover Set hoverLabel.HoverLabel = ctrl labelCollection.Add hoverLabel End If End With End If Next ctrl End Sub
步骤3:保留UserForm的MouseMove事件
保留你之前写的UserForm_MouseMove代码,用于鼠标离开Label时恢复颜色:
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) If Not activeLabel Is Nothing Then activeLabel.BackColor = RGB(255, 255, 255) '白色 Set activeLabel = Nothing End If End Sub
说明
- 类模块
LabelHover通过WithEvents捕获单个Label的MouseMove事件,实现悬停颜色变化 labelCollection用于保存类实例,防止对象被垃圾回收- 自动排除了
LabelNoData控件,因为它不可见且不需要悬停效果
内容的提问来源于stack exchange,提问作者vbaguru
相关产品推荐
相关产品推荐

