Attribute VB_Name = "mod_ctMenuA_ButtonHelper"
Option Explicit

#If (ctMenuA_FlagA And &H2&) = &H2& Then
  Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type
  Private Declare Function DrawFrameControl Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal un1 As Long, ByVal un2 As Long) As Long
  Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
  Private Declare Function DefWindowProcA Lib "user32" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Private Declare Function GetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
  'Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
  'Private Declare Function CallWindowProcA Lib "user32" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

  Private xType1 As RECT
  
  'Private Type PAINTSTRUCT
  '  hdc As Long
  '  fErase As Long
  '  rcPaint As RECT
  '  fRestore As Long
  '  fIncUpdate As Long
  '  rgbReserved(32) As Byte
  '  End Type
  'Private xType2 As PAINTSTRUCT
  'Private Declare Function BeginPaint Lib "user32" (ByVal hwnd As Long, lpPaint As PAINTSTRUCT) As Long
  'Private Declare Function EndPaint Lib "user32" (ByVal hwnd As Long, lpPaint As PAINTSTRUCT) As Long
  'Private Declare Function PostMessageA Lib "user32" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Private Declare Function SendMessageA Lib "user32" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  Public ctMenuA_PressedButtonHWnd As Long
  Private ButtonDownHWndKey As Long
  Private ButtonDownHWndTime As Long
  Private ButtonDownHWndWaitTime As Long
  Public Const ctMenuA_ButtonDownTimerInterval As Long = 50&
  Private Const ButtonDownHWndWaitTimeDef1 As Long = 400&
  Private Const ButtonDownHWndWaitTimeDef2 As Long = 200&
  Public ctMenuA_TimerHWnd As Long
  Public ctMenuA_TimerCount As Long
  Private Declare Function GetTickCount Lib "kernel32" () As Long
  
  'Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long

  'private
  
  'Const WM_MOUSEMOVE = &H200
  'Const WM_LBUTTONDOWN = &H201
  'Const WM_LBUTTONUP = &H202
  Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

  Public Function ctMenuA_ButtonHookProc(ByVal hWnd As Long, ByVal message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    'Dim xLong1 As Long
    
    'Debug.Print "Hook: ", message
    Select Case message
      Case 15& ', &H201&, &H202& 'wmpain/lmouseup
        'xLong1 = GetWindowLongA(hwnd, -21&)
        'If Not xLong1 = 0& Then
        '  ctMenuA_ButtonHookProc = CallWindowProcA(xLong1, hwnd, message, wParam, lParam)
        '  End If
        'Exit Function
        Debug.Print "15"
        
        xType1.Bottom = 16&
        xType1.Right = 16&
        'If wParam = 0& Then wParam = GetDC(hWnd)
        'Debug.Print "Draw", lParam
        If Not lParam = &H4000& Then lParam = 0&
        #If (ctMenuA_FlagB And &H8&) = &H8& Then
          Select Case GetWindowLongA(hWnd, -16) And &H8C00&
            Case &H8400&
            'Call DrawFrameControl(wParam, xType1, 3&, &H4000&)
            lParam = lParam + &H4000&
            Case &H8800&
            'Call DrawFrameControl(wParam, xType1, 3&, &H4001&)
            lParam = lParam + &H4001&
            Case &H400&
            'Call DrawFrameControl(wParam, xType1, 3&, 0&)
            'lParam = lParam0&
            Case Else '&h800&
            'Call DrawFrameControl(wParam, xType1, 3&, 1&)
            lParam = lParam + 1&
            End Select
          #Else
          If (GetWindowLongA(hWnd, -16) And &HC00&) = &H400& Then
            'Call DrawFrameControl(wParam, xType1, 3&, 0&)
            lParam = 0&
            Else
            'Call DrawFrameControl(wParam, xType1, 3&, 1&)
            lParam = 1&
            End If
          #End If
        'If message = 15& Then
          If wParam = 0& Then
            Call DrawFrameControl(GetDC(hWnd), xType1, 3&, lParam)
            Else
            Call DrawFrameControl(wParam, xType1, 3&, lParam)
            End If
          
          Debug.Print " - ", lParam
          'Else
          'If message = &H201& Then lParam = lParam + &H4000&
          'Call BeginPaint(hwnd, xType2)
          'Debug.Print "Begin"
          'Call DrawFrameControl(xType2.hdc, xType1, 3&, lParam)
          'Debug.Print "End"
          'Call EndPaint(hwnd, xType2)
          'End If
        lParam = 0&
        'Call DefWindowProcA(hwnd, message, wParam, lParam)
      Case &H201&, &H203 'lmousedown, dblclick
        Debug.Print message
        Call SendMessageA(hWnd, 15&, 0&, ByVal &H4000&)
        '16-23
        'Call SendMessageA(GetParent(hwnd), &H100&, 0&, 40& * &H1000&)
        'Debug.Print GetParent(hwnd)
        ctMenuA_PressedButtonHWnd = hWnd
        'Debug.Print Hex(GetWindowLongA(hwnd, -16&))
        If (GetWindowLongA(hWnd, -16&) And &HC00&) = &H400& Then
          ButtonDownHWndKey = 33& '38&
          Else
          ButtonDownHWndKey = 34& '40&
          End If
        'Debug.Print ButtonDownHWndKey
        ButtonDownHWndTime = (GetTickCount And Not &HC0000000)
        ButtonDownHWndWaitTime = ButtonDownHWndWaitTimeDef1
        Call keybd_event(ButtonDownHWndKey, 0&, 0&, 0&)
        
        'ButtonDownHWnd = hwnd
      Case &H202& 'lmouseup
        Debug.Print message
        ctMenuA_PressedButtonHWnd = 0&
        Call SendMessageA(hWnd, 15&, 0&, ByVal 0&)
      Case Else
      'Debug.Print hwnd, Hex(message)
      'ctMenuA_ButtonHookProc = DefWindowProcA(hwnd, message, wParam, lParam)
      End Select
    ctMenuA_ButtonHookProc = DefWindowProcA(hWnd, message, wParam, lParam)
    End Function
  
  Public Sub ctMenuA_TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
'Form1.Caption = dwTime
    'Debug.Print "Timer: ", Timer
    
    If Not ctMenuA_PressedButtonHWnd = 0& Then
      'Debug.Print ButtonDownHWndTime
      If DoEventsEx_IsTickCountElapsed(ButtonDownHWndTime, ButtonDownHWndWaitTime) Then
        'Debug.Print "Elapsed"
        ButtonDownHWndTime = (GetTickCount And Not &HC0000000)
        ButtonDownHWndWaitTime = ButtonDownHWndWaitTimeDef2
        Call keybd_event(ButtonDownHWndKey, 0&, 0&, 0&)
        'Else
        'Debug.Print "NotElapsed"
        End If
      End If
    End Sub
  
  
  #If (DoEventsEx_FlagA And &H20) = 0& Then
    Public Function DoEventsEx_IsTickCountElapsed(ByVal FirstTickCountValue As Long, ByVal TimeIntervalMs As Long) As Boolean
      Static xLong1 As Long
      xLong1 = GetTickCount And Not &HC0000000
      If xLong1 < FirstTickCountValue Then
        DoEventsEx_IsTickCountElapsed = (xLong1 + &H40000000 > FirstTickCountValue + TimeIntervalMs)
        Else
        DoEventsEx_IsTickCountElapsed = (xLong1 > FirstTickCountValue + TimeIntervalMs)
        End If
      End Function
    #End If
  
  
  #End If
