Option Explicit
#If Win64 Then
Private Const NULL_PTR = 0^
#Else
Private Const NULL_PTR = 0&
#End If
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
#If VBA7 Then
#If Win64 Then
Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrA" (ByVal hwnd As LongLong, ByVal nIndex As Long, ByVal dwNewLong As LongLong) As LongLong
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongPtrA" (ByVal hwnd As LongLong, ByVal nIndex As Long) As LongLong
Private Declare PtrSafe Function PtInRect Lib "user32" (lpRect As RECT, ByVal arg2 As LongPtr) As Long
#Else
Private Declare PtrSafe Function SetWindowLong Lib "USER32.DLL" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare PtrSafe Function GetWindowLong Lib "USER32.DLL" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare PtrSafe Function PtInRect Lib "user32" (lpRect As RECT, ByVal arg1 As Long, ByVal arg2 As Long) As Long
#End If
Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (destination As Any, source As Any, ByVal length As LongPtr)
Private Declare PtrSafe Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hUF As LongPtr) As Long
Private Declare PtrSafe Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
Private Declare PtrSafe Function TranslateColor Lib "oleaut32.dll" Alias "OleTranslateColor" (ByVal clr As OLE_COLOR, ByVal palet As LongPtr, col As Long) As Long
Private Declare PtrSafe Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As LongPtr, ByVal crKey As Long, ByVal bAlpha As Long, ByVal dwFlags As Long) As Long
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 SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As LongPtr) As Long
Private Declare PtrSafe Function DeleteObject Lib "gdi32" (ByVal hObject As LongPtr) As Long
Private Declare PtrSafe Function IIDFromString Lib "ole32.dll" (ByVal lpsz As LongPtr, ByVal lpiid As LongPtr) As LongPtr
Private Declare PtrSafe Function AccessibleObjectFromWindow Lib "OLEACC.DLL" (ByVal hwnd As LongPtr, ByVal dwId As Long, ByVal riid As LongPtr, ppvObject As Any) As Long
Private Declare PtrSafe Function GetPixel Lib "gdi32" (ByVal hDC As LongPtr, ByVal X As Long, ByVal Y As Long) As Long
Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hDC As LongPtr) As Long
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare PtrSafe Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare PtrSafe Function GetCursor Lib "user32" () As LongPtr
Private Declare PtrSafe Function IsWindowEnabled Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function SetProp Lib "user32" Alias "SetPropA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal hData As LongPtr) As Long
Private Declare PtrSafe Function GetProp Lib "user32" Alias "GetPropA" (ByVal hwnd As LongPtr, ByVal lpString As String) As LongPtr
Private Declare PtrSafe Function GetActiveWindow Lib "user32" () As LongPtr
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare PtrSafe Function GetDlgItem Lib "user32" (ByVal hDlg As LongPtr, ByVal nIDDlgItem As Long) As LongPtr
#Else
Private Enum LongPtr
[_]
End Enum
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function PtInRect Lib "user32" (lpRect As RECT, ByVal arg1 As Long, ByVal arg2 As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr)
Private Declare Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hUF As LongPtr) As Long
Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
Private Declare Function TranslateColor Lib "oleaut32.dll" Alias "OleTranslateColor" (ByVal clr As OLE_COLOR, ByVal palet As LongPtr, col As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As LongPtr, ByVal crKey As Long, ByVal bAlpha As Long, ByVal dwFlags As Long) As Long
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As LongPtr) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As LongPtr) As Long
Private Declare Function IIDFromString Lib "ole32.dll" (ByVal lpsz As LongPtr, ByVal lpiid As LongPtr) As LongPtr
Private Declare Function AccessibleObjectFromWindow Lib "OLEACC.DLL" (ByVal hwnd As LongPtr, ByVal dwId As Long, ByVal riid As LongPtr, ppvObject As Any) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As LongPtr, ByVal x As Long, ByVal Y As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hDC As LongPtr) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function GetCursor Lib "user32" () As LongPtr
Private Declare Function IsWindowEnabled Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare Function SetProp Lib "user32" Alias "SetPropA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal hData As LongPtr) As Long
Private Declare Function GetProp Lib "user32" Alias "GetPropA" (ByVal hwnd As LongPtr, ByVal lpString As String) As LongPtr
Private Declare Function GetActiveWindow Lib "user32" () As LongPtr
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetDlgItem Lib "user32" (ByVal hDlg As LongPtr, ByVal nIDDlgItem As Long) As LongPtr
#End If
Public Sub AddIcon(ByVal ObjForm As Object)
Const WM_SETICON = &H80, ICON_SMALL = 0&, ICON_BIG = 1&
Dim hwnd As LongPtr, hIcon As LongPtr
Call IUnknown_GetWindow(ObjForm, VarPtr(hwnd))
hIcon = ObjForm.Image1.Picture.Handle
Call SendMessage(hwnd, WM_SETICON, ICON_SMALL, ByVal hIcon)
Call SendMessage(hwnd, WM_SETICON, ICON_BIG, ByVal hIcon)
Call DeleteObject(hIcon)
End Sub
Public Sub AddContextHelp(ByVal oForm As Object)
Const WS_EX_CONTEXTHELP = &H400, GWL_EXSTYLE = (-20&)
Dim hwnd As LongPtr
Call IUnknown_GetWindow(oForm, VarPtr(hwnd))
Call SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) Or WS_EX_CONTEXTHELP)
End Sub
Public Sub StartContextHelpButtonClickWatcher(ByVal oForm As Object, ByVal bStart As Boolean)
Dim hwnd As LongPtr
Call IUnknown_GetWindow(oForm, VarPtr(hwnd))
If bStart Then
Call SetProp(hwnd, "Formhwnd", hwnd)
Call SetTimer(hwnd, hwnd, 0&, AddressOf ContextHelpMonitor)
Else
Call KillTimer(GetProp(hwnd, "Formhwnd"), GetProp(hwnd, "Formhwnd"))
End If
End Sub
Public Sub MakeTransparent(ByVal oForm As Object, ByVal bTrans As Boolean)
Const GWL_EXSTYLE = (-20&), WS_EX_LAYERED = &H80000, LWA_COLORKEY = &H1, COLOR_BTNFACE = 15&
Dim hwnd As LongPtr, lWindColor As Long
Call IUnknown_GetWindow(oForm, VarPtr(hwnd))
If bTrans Then
Call TranslateColor(GetSysColor(COLOR_BTNFACE), NULL_PTR, lWindColor)
Call SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED)
Call SetLayeredWindowAttributes(hwnd, lWindColor, 0&, LWA_COLORKEY)
Else
Call SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) And Not WS_EX_LAYERED)
End If
End Sub
Public Sub FormatWBrowser(ByVal Wbr As Object)
Const COLOR_BTNFACE = 15&
Dim lWindColor As Long
Call TranslateColor(GetSysColor(COLOR_BTNFACE), NULL_PTR, lWindColor)
With Wbr
.Document.body.Scroll = "no"
.Document.body.bgcolor = lWindColor
.Document.body.Style.Border = "none"
End With
End Sub
Public Sub PaintHighlightLabel(ByVal lbl As MSForms.Label)
Dim tCurPos As POINTAPI, hDC As LongPtr
Call GetCursorPos(tCurPos)
hDC = GetDC(NULL_PTR)
lbl.BackColor = GetPixel(hDC, tCurPos.X, tCurPos.Y)
Call ReleaseDC(NULL_PTR, hDC)
End Sub
' ____________________________________ PRIVATE ROUTINES _________________________________
Private Sub ContextHelpMonitor( _
ByVal hwnd As LongPtr, _
ByVal Msg As Long, _
ByVal idEvent As LongPtr, _
ByVal dwTime As Long _
)
Static lPrevCur As LongPtr
Dim hCur As LongPtr
Dim tHelpButtonRect As RECT, lRet As Long
Dim tCurPos As POINTAPI
Dim oiAccTitleBar As IAccessible
Dim pxLeft As Long, pyTop As Long, pcxWidth As Long, pcyHeight As Long
On Error GoTo errHandler
Application.EnableCancelKey = xlDisabled
If IsVBAError Then
Debug.Print "Error raised by the Akuini Add-in to prevent crashing!"
Debug.Print "API timer was safely relased after an unhandled run-time error or a compile error occurred."
Call KillTimer(idEvent, idEvent): Exit Sub
End If
Set oiAccTitleBar = HwndToAcc(idEvent)
Call oiAccTitleBar.accLocation(pxLeft, pyTop, pcxWidth, pcyHeight, 4&)
Call SetRect(tHelpButtonRect, pxLeft, pyTop, pxLeft + pcxWidth, pyTop + pcyHeight)
Call GetCursorPos(tCurPos)
#If Win64 Then
Dim lPtr As LongLong
Call CopyMemory(lPtr, tCurPos, LenB(tCurPos))
lRet = PtInRect(tHelpButtonRect, lPtr)
#Else
lRet = PtInRect(tHelpButtonRect, tCurPos.X, tCurPos.Y)
#End If
If lRet Then
hCur = GetCursor '
If hCur <> lPrevCur And lPrevCur <> NULL_PTR Then
If IsWindowEnabled(idEvent) Then
UF_Help.Show
With UF_Finder
.CommandButton4.SetFocus
.TextBox1.SetFocus
.TextBox1.SelStart = Len(.TextBox1)
End With
End If
End If
End If
lPrevCur = hCur
Exit Sub
errHandler:
If Err.Number <> &HC472& And Err.Number <> 0& Then
Call KillTimer(idEvent, idEvent)
End If
End Sub
Private Function HwndToAcc(ByVal hwnd As LongPtr) As IAccessible
Const ID_ACCESSIBLE As String = "{618736E0-3C3D-11CF-810C-00AA00389B71}"
Const OBJID_TITLEBAR = &HFFFFFFFE
Const S_OK = &H0&
Dim tGUID(0& To 3&) As Long
Dim oIAc As IAccessible
If IIDFromString(StrPtr(ID_ACCESSIBLE), VarPtr(tGUID(0&))) = S_OK Then
If AccessibleObjectFromWindow(hwnd, OBJID_TITLEBAR, VarPtr(tGUID(0&)), oIAc) = S_OK Then
Set HwndToAcc = oIAc
End If
End If
End Function
Private Function IsVBAError() As Boolean
Const GWL_STYLE = (-16&), WS_SYSMENU = &H80000
Dim sBuffer As String * 256&, sErrPromptText As String, lRet As Long
Dim hPrompt As LongPtr, lStyle As LongPtr
hPrompt = GetDlgItem(GetActiveWindow, &HFFFF&)
lRet = GetWindowText(hPrompt, sBuffer, 256&)
sErrPromptText = VBA.Left(sBuffer, lRet)
If InStr(1&, sErrPromptText, "compil", vbTextCompare) Or InStr(1&, sErrPromptText, "Kompil", vbTextCompare) Then
IsVBAError = True
End If
lStyle = GetWindowLong(GetActiveWindow, GWL_STYLE)
If (lStyle And WS_SYSMENU) = 0& Then
IsVBAError = True
End If
End Function