【问题标题】:Excel Chart_MouseUp event not trapped under the same scenario trapped by the corresponding MouseDown eventExcel Chart_MouseUp 事件未在相应 MouseDown 事件捕获的相同场景下捕获
【发布时间】:2019-10-23 20:06:39
【问题描述】:

我也有同样的问题Excel Chart_MouseUp event not trapped,但是没有给出答案

我感兴趣的顺序是按下 shift-ctrl-left 鼠标(触发 MouseDown 事件),移动鼠标左键(触发 MouseMove 事件),释放 shift-ctrl-left 鼠标(不触发 MouseUp 事件)

怎么可能触发了一个 MouseDown 事件而对应的 MouseUp 事件却不是(在相同的情况下)!!!

我该如何解决这个问题?

提前感谢您能给我的帮助

【问题讨论】:

    标签: excel vba mouseevent


    【解决方案1】:

    即使我在这里没有收到有关此主题的答案,希望这对其他人有用,我还是附上了 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-05
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      • 2010-09-19
      • 2022-10-18
      相关资源
      最近更新 更多