如何通过标签模仿旋转按钮?

时间:2012-08-29 07:51:49

标签: excel vba

我在表单上有两个标签

lab01_Click:
lab02.Caption = lab02.Caption + 1

有没有办法将lab01用作旋转按钮?
如果我一直按下它 - 那么lab02.Caption应该不断变化吗?

1 个答案:

答案 0 :(得分:1)

您也可以在Tag属性中存储数据

例如

Private Sub Label1_Click()
If Label1.Tag = "" Then Label1.Tag = 0
 Label1.Tag = CLng(Label1.Tag) + 1
 Label1.Caption = "Increment: " & Label1.Tag
End Sub

Private Sub Label2_Click()
If Label1.Tag = "" Then Label1.Tag = 0
 Label1.Tag = CLng(Label1.Tag) - 1
 Label1.Caption = "Increment: " & Label1.Tag
End Sub

更新:好的我看到你想在按下点击时继续增加它。我能想到的唯一方法是一个讨厌的“黑客”,它会重新触发一个计时器事件。 您需要更新对象名称Userform1Label1Label2,并且可能需要针对64位将Private Declare Function调整为Private Declare PtrSafe Function

MODULE

中试试
Option Explicit

Private Declare Function SetTimer Lib "user32" _
(ByVal hWnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long

Private Declare Function KillTimer Lib "user32" _
(ByVal hWnd As Long, _
ByVal nIDEvent As Long) As Long

Private m_TimerID As Long

 'Note:  The duration is measured in milliseconds.
 '         1,000 milliseconds = 1 second
Public Sub StartTimer(ByVal Duration As Long)
     'If the timer isn't already running, start it.
    If m_TimerID = 0 Then
        If Duration > 0 Then
            m_TimerID = SetTimer(0, 0, Duration, AddressOf TimerEvent)
            If m_TimerID = 0 Then
                MsgBox "Timer initialization failed!"
            End If
        Else
            MsgBox "The duration must be greater than zero."
        End If
    Else
        MsgBox "Timer already started."
    End If
End Sub

Public Sub StopTimer()
     'If the timer is already running, shut it off.
    If m_TimerID <> 0 Then
        KillTimer 0, m_TimerID
        m_TimerID = 0
    Else
        MsgBox "Timer is not active."
    End If
End Sub

Public Property Get TimerIsActive() As Boolean
     'A non-zero timer ID indicates that it's turned on.
    TimerIsActive = (m_TimerID <> 0)
End Property

Private Sub TimerEvent()
    If UserForm1.Label2.Tag = "" Then UserForm1.Label2.Tag = 0
    UserForm1.Label2.Tag = CLng(UserForm1.Label2.Tag) + 1
    UserForm1.Label2.Caption = "Increment : " & UserForm1.Label2.Tag
End Sub

并在USERFORM

Option Explicit

Private Sub Label1_MouseDown(ByVal Button As Integer, _
    ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    StartTimer 200 'millisecond update
End Sub

Private Sub Label1_MouseUp(ByVal Button As Integer, _
    ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    StopTimer
End Sub