Excel Chart_MouseUp事件在由相应的MouseDown事件捕获的相同情况下没有捕获

问题描述 投票:0回答:1

我对问题Excel Chart_MouseUp event not trapped有完全相同的问题,但没有给出答案

我对序列按下shift-ctrl-左鼠标(MouseDown事件被触发),向左移动鼠标(MouseMove事件被触发),释放shift-ctrl-左鼠标(不触发MouseUp事件)感兴趣>

在相同情况下,有可能触发MouseDown事件而没有触发相应的MouseUp事件!!

我该如何解决这个问题?

先谢谢您能给我的帮助

excel vba mouseevent
1个回答
0
投票

即使我在此处未收到有关此主题的答案,希望对其他人也有用,我还是附上了Peter Thornton给我的解决方案,对此我深表谢意。

有一种不同的方法,不确定为什么以前没想到->用计时器检查鼠标左键的状态

在我写的目的下面]

Private Declare PtrSafe Function SetTimer Lib "User32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
Private Declare PtrSafe Function KillTimer Lib "User32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
Private Declare PtrSafe Function GetAsyncKeyState Lib "User32" (ByVal vKey As Long) As Integer
Private Declare PtrSafe Function GetSystemMetrics32 Lib "User32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long

Private Const HWND_ZERO As LongPtr = 0
Private Const TIMER_MILLISECS = 150&

Private Const VK_LBUTTON = &H1 'Left mouse button
Private Const VK_RBUTTON = &H2 'Right mouse button
Private Const SM_SWAPBUTTON = 23& 'Left and Right mouse buttons are logically swapped (Left mouse button is the default Primary button)

Private mTimerID As LongPtr

Public Sub StartTimer()
' call StartTimer in the chart's MouseDown event
    If mTimerID Then
        Call EndTimer
    End If

    mTimerID = SetTimer(HWND_ZERO, mTimerID, TIMER_MILLISECS, AddressOf TimerProcedure)
End Sub

Private Function TimerProcedure() As LongPtr
    Dim Ch As Chart
    Dim obj As Variant

    Dim ret As Integer

    On Error Resume Next

    If mTimerID = 0 Then
        Call EndTimer
    Else
        'map the mouse logical Primary Button to the mouse phisical Left or Right Buttons
        If Not GetSystemMetrics32(SM_SWAPBUTTON) Then ret = GetAsyncKeyState(VK_LBUTTON) Else ret = GetAsyncKeyState(VK_RBUTTON)

        'the condition is satisfied if the mouse Left Button state is correctly trapped and the Left Button is released
        If Not ret And 32768 Then
            Call EndTimer
            mTimerID = 0

            Call UserDefinedChartMouseUp() procedure

        End If
    End If
End Function

Private Sub EndTimer()
    On Error Resume Next

    Call KillTimer(HWND_ZERO, mTimerID)
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.