Option Explicit
Option Compare Text
Option Private Module
#If VBA7 Then
#If Win64 Then
Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
Declare PtrSafe Function SetParent Lib "user32" (ByVal hWndChild As LongPtr, ByVal hWndNewParent As LongPtr) As LongPtr
Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongPtrA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
Declare PtrSafe Function GetWindowLongPtrW Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
Declare PtrSafe Function WindowFromPoint Lib "user32" (ByVal Point As LongPtr) As LongPtr
#Else
Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As Long
Declare PtrSafe Function SetParent Lib "user32" (ByVal hWndChild As LongPtr, ByVal hWndNewParent As LongPtr) As Long
Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As Long
Declare PtrSafe Function GetWindowLongPtrW Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As Long
Declare PtrSafe Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
#End If
Declare PtrSafe Function GetDesktopWindow Lib "user32" () As LongPtr
Declare PtrSafe Function GetAncestor Lib "user32.dll" (ByVal hwnd As LongPtr, ByVal gaFlags As Long) As LongPtr
Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal nCmdShow As Long) As Long
Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Declare PtrSafe Function GetActiveWindow Lib "user32" () As Long
Declare PtrSafe Function GetParent Lib "user32" (ByVal hwnd As LongPtr) As Long
Declare PtrSafe Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As LongPtr, ByVal wFlag As Long) As Long
Declare PtrSafe Function GetCurrentThreadId Lib "kernel32" () As Long
Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare PtrSafe Function GetClassNameW Lib "user32" (ByVal hwnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare PtrSafe Function IsWindowUnicode Lib "user32" (ByVal hwnd As LongPtr) As Boolean
Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
Declare PtrSafe Function GetWindowTextW Lib "user32" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
Declare PtrSafe Function GetWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal wCmd As Long) As Long
Declare PtrSafe Function IsWindowVisible Lib "user32" (ByVal hwnd As LongPtr) As Boolean
#Else
Declare Function GetWindowTextW Lib "user32" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetDesktopWindow Lib "USER32" () As Long
Declare Function GetAncestor Lib "user32.dll" (ByVal hwnd As Long, ByVal gaFlags As Long) As Long
Declare Function GetCurrentThreadId Lib "kernel32" ()
Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1$, ByVal lpsz2$) As Long
Declare Function GetActiveWindow Lib "user32" () As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName$, ByVal lpWindowName$) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function GetClassName Lib "User32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName$, ByVal nMaxCount As Long) As Long
Declare Function GetClassNameW Lib "user32" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare Function IsWindowUnicode Lib "user32" (ByVal hwnd As Long) As Boolean
Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString$, ByVal cch As Long) As Long
#End If
#If VBA7 Then
Declare PtrSafe Function RedrawWindow Lib "user32" (ByVal hwnd As LongPtr, lprcUpdate As Any, ByVal hrgnUpdate As Long, ByVal fuRedraw As Long) As Long
Declare PtrSafe Function BeginDeferWindowPos Lib "user32" (ByVal nNumWindows As Long) As Long
Declare PtrSafe Function DeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long, ByVal hwnd As LongPtr, ByVal HwndInsertAfter As LongPtr, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Declare PtrSafe Function EndDeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long) As Long
Declare PtrSafe Function DeleteObject Lib "gdi32" (ByVal hObject As LongPtr) As Long
#Else
Declare Function RedrawWindow Lib "user32" Alias "RedrawWindow" (ByVal hwnd As Long, lprcUpdate As any, ByVal hrgnUpdate As Long, ByVal fuRedraw As Long) As Long
Declare Function BeginDeferWindowPos Lib "user32" (ByVal nNumWindows As Long) As Long
Declare Function DeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long, ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Declare Function EndDeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long) As Long
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
#End If
#If VBA7 Then
Public Declare PtrSafe Function GetForegroundWindow Lib "user32" () As LongPtr
Public Declare PtrSafe Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As LongPtr, lpdwProcessId As Long) As Long
Public Declare PtrSafe Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Public Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
Public Declare PtrSafe Function IsIconic Lib "user32" (ByVal hwnd As LongPtr) As Long
#Else
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function GetWindowThreadProcessId Lib "USER32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function AttachThreadInput Lib "USER32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Public Declare Function SetForegroundWindow Lib "USER32" (ByVal hwnd As Long) As Long
Public Declare Function IsIconic Lib "USER32" (ByVal hwnd As Long) As Long
#End If
Function ChromeShowByTitle(ByVal chromeTitle$) As Boolean
#If VBA7 And Win64 Then
Dim h As LongPtr
#Else
Dim h As Long
#End If
Dim l&, s$, p
For Each p In GetObject("winmgmts:\\.\root\CIMV2") _
.ExecQuery("SELECT * FROM Win32_Process WHERE (Name = ""msedge.exe"" or Name = ""chrome.exe"")", , 48)
h = InstanceToWnd(p.processid, Class:="Chrome_WidgetWin_1")
If h > 0 Then
s = Space(256)
If IsWindowUnicode(h) Then
l = GetWindowTextW(h, s, 256): s = Left(StrConv(s, vbFromUnicode), l)
Else
l = GetWindowText(h, s, 256): s = Left(s, l)
End If
If s Like chromeTitle Then apiSetWindowZOrder h, 0: ChromeShowByTitle = True: Exit For
End If
Next
End Function
#If VBA7 And Win64 Then
Public Sub apiSetWindowZOrder(hwnd As LongPtr, HwndInsertAfter As LongPtr)
Dim v As LongPtr, h As LongPtr, l As Long
#Else
Public Sub apiSetWindowZOrder(hwnd As Long, HwndInsertAfter As Long)
Dim v As Long, h As Long, l As Long
#End If
h = GetAncestor(hwnd, 1)
Call apiGetWindowChilds(h, l)
l = BeginDeferWindowPos(l)
l = DeferWindowPos(l, hwnd, HwndInsertAfter, 0, 0, 0, 0, &H2 Or &H1)
Call EndDeferWindowPos(l)
End Sub
#If VBA7 And Win64 Then
Function InstanceToWnd(ByVal target_pid As Long, Optional ByVal Title$, Optional ByVal Class$) As LongPtr
#Else
Function InstanceToWnd(ByVal target_pid As Long, Optional ByVal Title$, Optional ByVal Class$) As Long
#End If
#If VBA7 And Win64 Then
Dim hwnd As LongPtr
#Else
Dim hwnd As Long
#End If
Dim pid As Long, thread_id As Long
hwnd = FindWindow(Class, Title)
Do While hwnd <> 0
If GetParent(hwnd) = 0 Then
thread_id = GetWindowThreadProcessId(hwnd, pid)
If pid = target_pid Then InstanceToWnd = hwnd: Exit Do
End If
hwnd = GetWindow(hwnd, 2)
Loop
End Function
#If VBA7 And Win64 Then
Private Function apiGetWindowChilds(hParent As LongPtr, Optional childCount&)
Dim h As LongPtr
#Else
Private Function apiGetWindowChilds(hParent As Long, Optional childCount&)
Dim h As Long
#End If
childCount = 0: h = GetWindow(hParent, 5)
While h <> 0: childCount = childCount + 1: h = GetWindow(h, 2): Wend
End Function