|
Option Explicit |
|
|
|
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long |
|
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long |
|
Declare Function GetDesktopWindow Lib "user32" () As Long |
|
Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long |
|
Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long |
|
Declare Function GetWindowLW Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long |
|
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long |
|
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwprocessid As Long) As Long |
|
Declare Function SetFocusAPI Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As Long) As Long |
|
|
|
Declare Function GetCurrentProcessId Lib "kernel32" () As Long |
|
|
|
Public Const GWL_ID = (-12) |
|
Public Const GW_HWNDNEXT = 2 |
|
Public Const GW_CHILD = 5 |
|
|
|
'---------------------------------------------------------------------- |
|
'[How To Get a Window Handle Without Specifying an Exact Title](http://support.microsoft.com/kb/147659/ja) |
|
' |
|
'FindWindowLike |
|
' - Finds the window handles of the windows matching the specified parameters |
|
' |
|
'PlngWindowHandles() |
|
' - An integer array used to return the window handles |
|
' |
|
'PlngProcessIDs() |
|
' - process id of the window |
|
' |
|
'PlngHandleStart |
|
' - The handle of the window to search under. |
|
' - The routine searches through all of this window's children and their |
|
' children recursively. |
|
' - If PlngHandleStart = 0 then the routine searches through all windows. |
|
' |
|
'PstrWindowText |
|
' - The pattern used with the Like operator to compare window's text. |
|
' |
|
'Returns |
|
' - The number of windows that matched the parameters. |
|
' - Also returns the window handles in PlngWindowHandles() |
|
' and the window text in PstrWindowTexts() |
|
' and the process id in PlngProcessIDs() |
|
' |
|
'---------------------------------------------------------------------- |
|
Function FindWindowLike(ByRef PlngWindowHandles() As Long, _ |
|
ByRef PstrWindowTexts() As String, _ |
|
ByRef PlngProcessIDs() As Long, _ |
|
ByVal PlngHandleStart As Long, _ |
|
ByVal PstrWindowText As String) As Long |
|
|
|
Dim LlngWindowHandle As Long |
|
Dim LlngResult As Long |
|
' Hold the level of recursion: |
|
Static LlngLevel As Long |
|
' Hold the number of matching windows: |
|
Static LlngFound As Long |
|
Dim LstrWindowText As String |
|
Dim LlngProcessId As Long |
|
|
|
' Initialize if necessary: |
|
If PlngHandleStart = 0 Then |
|
LlngLevel = 0 |
|
ReDim PlngWindowHandles(0 To 0) |
|
ReDim PstrWindowTexts(0 To 0) |
|
ReDim PlngProcessIDs(0 To 0) |
|
If PlngHandleStart = 0 Then |
|
PlngHandleStart = GetDesktopWindow() |
|
End If |
|
End If |
|
|
|
' Increase recursion counter: |
|
LlngLevel = LlngLevel + 1 |
|
' Get first child window: |
|
LlngWindowHandle = GetWindow(PlngHandleStart, GW_CHILD) |
|
|
|
Do Until LlngWindowHandle = 0 |
|
' Search children by recursion: |
|
LlngResult = FindWindowLike(PlngWindowHandles, _ |
|
PstrWindowTexts, _ |
|
PlngProcessIDs, _ |
|
LlngWindowHandle, _ |
|
PstrWindowText) |
|
|
|
' Get the window text: |
|
LstrWindowText = Space(255) |
|
LlngResult = GetWindowText(LlngWindowHandle, LstrWindowText, 255) |
|
LstrWindowText = Left(LstrWindowText, LlngResult) |
|
|
|
' Get ProcessID |
|
LlngProcessId = ProcIDFromWnd(LlngWindowHandle) |
|
|
|
' Check that window matches the search parameters: |
|
If LstrWindowText Like PstrWindowText Then |
|
LlngLevel = LlngLevel + 1 |
|
ReDim Preserve PlngWindowHandles(0 To LlngLevel) |
|
ReDim Preserve PstrWindowTexts(0 To LlngLevel) |
|
ReDim Preserve PlngProcessIDs(0 To LlngLevel) |
|
PlngWindowHandles(LlngLevel) = LlngWindowHandle |
|
PstrWindowTexts(LlngLevel) = LstrWindowText |
|
PlngProcessIDs(LlngLevel) = LlngProcessId |
|
|
|
'Debug.Print "Window Found: " |
|
'Debug.Print " Window Text : " & LstrWindowText |
|
'Debug.Print " Window Handle: " & CStr(LlngWindowHandle) |
|
'Debug.Print " Process ID: " & CStr(LlngProcessID) |
|
End If |
|
' Get next child window: |
|
LlngWindowHandle = GetWindow(LlngWindowHandle, GW_HWNDNEXT) |
|
Loop |
|
' Decrement recursion counter: |
|
LlngLevel = LlngLevel - 1 |
|
' Return the number of windows found: |
|
FindWindowLike = LlngLevel |
|
End Function |
|
|
|
'------------------------------------------------------------------------------- |
|
'[How To Find a Window Handle from an Instance Handle](http://support.microsoft.com/kb/242308/ja) |
|
'ProcIDFromWnd |
|
'- Get Process ID from window handle |
|
'------------------------------------------------------------------------------- |
|
Private Function ProcIDFromWnd(ByVal hWnd As Long) As Long |
|
Dim idProc As Long |
|
|
|
' Get PID for this HWnd |
|
GetWindowThreadProcessId hWnd, idProc |
|
|
|
' Return PID |
|
ProcIDFromWnd = idProc |
|
End Function |
|
|
|
|
|
'------------------------------------------------------------------------------- |
|
'GetWindowTitle |
|
'- 自分自身が実行されているWindowのWindowTextを取得 |
|
'------------------------------------------------------------------------------- |
|
Function GetWindowTitle() As String |
|
Dim LlngMyProcessId As Long |
|
Dim LlngWndHandls() As Long |
|
Dim LstrWndTexts() As String |
|
Dim LlngPrcIDs() As Long |
|
Dim LlngResult As Long |
|
Dim LlngIndex As Long |
|
|
|
' ProcessIDを取得 |
|
LlngMyProcessId = GetCurrentProcessId() |
|
|
|
' Windowをすべて取得 |
|
LlngResult = FindWindowLike(LlngWndHandls, LstrWndTexts, LlngPrcIDs, 0, "F*") |
|
|
|
' ProcessIDが一致するWindowTextを取得 |
|
For LlngIndex = 0 To LlngResult |
|
If LlngPrcIDs(LlngIndex) = LlngMyProcessId Then |
|
GetWindowTitle = LstrWndTexts(LlngIndex) |
|
Exit For |
|
End If |
|
Next |
|
|
|
End Function |