VB3 How to Get Window Handle Without Specifying Exact TitleID: Q113475
|
The Visual Basic AppActivate command can only activate a window if you know
the exact window title. Similarly, the Windows FindWindow function can only
find a window handle if you know the exact window title.
This article demonstrates how to search for a window that has a title that
is like the title you specify -- but not an exact match. The sample code
searches through the available windows, comparing the window titles to a
pattern by using the Visual Basic Like operator. You can also use the
sample code to find a window based on its class name or ID. This can be
extremely helpful when you need to send keystrokes to other Applications.
Q112649 : How to Get a Window's Class Name and Other AttributesFindWindowLike uses the Visual Basic Like operator to when comparing class names.
Q112649 : How to Get a Window's Class Name and Other AttributesThe ID parameter can be a decimal number or a hexadecimal string. If you use a hexadecimal string, prefix the hexadecimal number with &H. If you don't want to search for a specific ID, specify Null for the ID parameter.
r = FindWindowLike(hWnds(), 0, "PIF Editor*", "Pif", Null)
' Assuming the previous returned at least one handle
r = FindWindowLike(hWnds(), hWnds(1), "*", "*", 103)
Sub Form_Click ()
' Used to return window handles.
Static hWnds() As Integer
' Find window with title "Form1":
r = FindWindowLike(hWnds(), 0, "Form1", "*", Null)
If r = 1 Then
Print "Found "; Hex(hWnds(1))
' Find a child window of "Form1" with ID=2:
' Notice that the handle from the first search is used.
r = FindWindowLike(hWnds(), hWnds(1), "*", "*", "&H2")
If r = 1 Then
Print "Found child "; Hex(hWnds(1))
Print "Setting focus to child ..."
' Set the focus to the child window with ID=2:
r = SetFocusAPI(hWnds(1))
ElseIf r > 1 Then
' This should not happen.
Print "Found more than one child ID=2"
Else
Print "Did not find child ID=2"
End If
ElseIf r > 1 Then
Print "Found "; r; " Windows"
End If
End Sub
Option Explicit
' The following Declares and constants were taken from WIN30API.TXT
' that ships with the Visual Basic Professional Edition.
' Enter each of the following Declare statements on one, single line:
Declare Function SetFocusAPI Lib "User" Alias "SetFocus"
(ByVal hwnd As Integer) As Integer
Declare Function GetDeskTopWindow Lib "User" () As Integer
Declare Function GetWindow Lib "User" (ByVal hwnd As Integer,
ByVal wCmd As Integer) As Integer
Declare Function GetWindowText Lib "User" (ByVal hwnd As Integer,
ByVal lpString As String, ByVal aint As Integer) As Integer
Declare Function GetClassName Lib "User" (ByVal hwnd As Integer,
ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
Declare Function GetParent Lib "User" (ByVal hwnd As Integer) As Integer
Declare Function GetWindowWord Lib "User" (ByVal hwnd As Integer,
ByVal nIndex As Integer) As Integer
Global Const GW_HWNDNEXT = 2
Global Const GW_CHILD = 5
Global Const GWW_ID = (-12)
'-----------------------------------------------------------------------
'FindWindowLike
' - Finds the window handles of the windows matching the specified
' parameters
'
'hwndArray()
' - An integer array used to return the window handles
'
'hWndStart
' - The handle of the window to search under.
' - The routine searches through all of this window's children and their
' children recursively.
' - If hWndStart = 0 then the routine searches through all windows.
'
'WindowText
' - The pattern used with the Like operator to compare window's text.
'
'ClassName
' - The pattern used with the Like operator to compare window's class
' name.
'
'ID
' - A child ID number used to identify a window.
' - Can be a decimal number or a hex string.
' - Prefix hex strings with "&H" or an error will occur.
' - To ignore the ID pass the Visual Basic Null function.
'
'Returns
' - The number of windows that matched the parameters.
' - Also returns the window handles in hWndArray()
'
'----------------------------------------------------------------------
' Enter the following two lines as one, single line:
Function FindWindowLike (hWndArray() As Integer, ByVal hWndStart As
Integer, WindowText As String, Classname As String, ID) As Integer
Dim hwnd As Integer
Dim sWindowText As String
Dim sClassname As String
Dim sID
Dim r As Integer
' Hold the level of recursion:
Static level As Integer
' Hold the number of matching windows:
Static iFound As Integer
' Initialize if necessary:
If level = 0 Then
iFound = 0
ReDim hWndArray(0 To 0)
If hWndStart = 0 Then hWndStart = GetDeskTopWindow()
End If
' Increase recursion counter:
level = level + 1
' Get first child window:
hwnd = GetWindow(hWndStart, GW_CHILD)
Do Until hwnd = 0
DoEvents ' Not necessary
' Search children by recursion:
r = FindWindowLike(hWndArray(), hwnd, WindowText, Classname, ID)
' Get the window text and class name:
sWindowText = Space(255)
r = GetWindowText(hwnd, sWindowText, 255)
sWindowText = Left(sWindowText, r)
sClassname = Space(255)
r = GetClassName(hwnd, sClassname, 255)
sClassname = Left(sClassname, r)
' If window is a child get the ID:
If GetParent(hwnd) <> 0 Then
r = GetWindowWord(hwnd, GWW_ID)
sID = CLng("&H" & Hex(r))
Else
sID = Null
End If
' Check that window matches the search parameters:
If sWindowText Like WindowText And sClassname Like Classname Then
If IsNull(ID) Then
' If find a match, increment counter and add handle to array:
iFound = iFound + 1
ReDim Preserve hWndArray(0 To iFound)
hWndArray(iFound) = hwnd
ElseIf Not IsNull(sID) Then
If sID = CLng(ID) Then
' If find a match increment counter and add handle to array:
iFound = iFound + 1
ReDim Preserve hWndArray(0 To iFound)
hWndArray(iFound) = hwnd
End If
End If
End If
' Get next child window:
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
Loop
' Decrement recursion counter:
level = level - 1
' Return the number of windows found:
FindWindowLike = iFound
End Function
Microsoft Knowledge Base articles Q72918,
Q78001, and Q112649
The Windows SDK Help file that shipped with the Professional Editions of
Microsoft Visual Basic versions 2.O and 3.0 for Windows.
"Microsoft Windows Software Development Kit: Programmer's Reference"
"Programming Windows 3.1: The Microsoft Guide to Writing Applications for
Windows 3.1," Charles Petzold, Microsoft Press, 1992
"Visual Basic Programmer's Guide to the Windows API," Daniel Appleman,
Ziff-Davis Press, 1993
Additional query words: 1.00 2.00 3.00
Keywords : kbcode kbWndw
Version : 1.00 2.00 3.00
Platform : WINDOWS
Issue type :
Last Reviewed: June 2, 1999