如何在鼠标仍处于关闭状态时检测Userform Frame上的mouse_down

时间:2011-09-19 04:12:49

标签: excel vba userform mousedown

我希望在鼠标仍处于运行状态时检测到Form上任何Frame上有mouse_down的时间。我知道如何为Click做这件事,但我想在mouse_up之前抓住它。

由于

1 个答案:

答案 0 :(得分:2)

您可以为表单上的每个帧创建一个_MouseDown事件处理程序,或者如果您有多个框架,则可以创建一个通用事件处理程序类

创建一个类模块(例如名为cUserFormEvents

Public WithEvents Frme As MSForms.frame
Public frm As UserForm

Private Sub Frme_MouseDown( _
 ByVal Button As Integer, _
 ByVal Shift As Integer, _
 ByVal X As Single, _
 ByVal Y As Single)

    ' Put your event code here
    MsgBox Frme.Caption

End Sub

为您的Frames声明一个集合

Dim mcolFrames As New Collection

在表单初始化中包含此代码

Private Sub UserForm_Initialize()
    Dim ctl As MSForms.Control
    Dim clsEvents As cUserFormEvents

    'Loop through all controls on userform
    For Each ctl In Me.Controls
        'Only process Frames
        If TypeOf ctl Is MSForms.frame Then
            'Instantiate class module and assign properties
            Set clsEvents = New cUserFormEvents
            Set clsEvents.Frme = ctl
            Set clsEvents.frm = Me

            'Add instance to collection
            mcolFrames.Add clsEvents

        End If

    Next ctl

End Sub

现在,Frme_MouseDown将在表单上的任何Frame上的MouseDown上执行。使用Frme

访问特定的框架