|
-
Aug 18th, 2000, 05:50 AM
#1
Thread Starter
Lively Member
How do I get the hWnd for a window that i know the name of?
I.e. to activate the Calculator window.
-
Aug 18th, 2000, 05:58 AM
#2
Guru
First of ALL put this in the general declarations area:
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Next.
If you know a window by its caption (for example, Calculator's caption is "Calculator"), use this:
Code:
hWndOfTheWindow = FindWindow(vbNullstring, "Calculator")
If you know a window by its ClassName (for example, Calculator's ClassName is "SciCalc"), use this:
Code:
hWndOfTheWindow = FindWindow("SciCalc", vbNullString)
Finding the window by the ClassName is better, because on non-English computers, the caption of Calculator can be different (the word "Calculator" in another language).
The ClassName is always "SciCalc" regardless of the operating system language.
To find ClassNames, use Microsoft's program Spy++ (comes with Visual Studio).
Of course you can just guess random numbers for the hWnd but it's not quite as efficient.
-
Aug 18th, 2000, 06:09 AM
#3
Thread Starter
Lively Member
Thanks. It worked. A sub-question. If one only knows the windows name starts with "Calcu" is there a way to get the 1st matching window without doing a enumeration of all the windows?
-
Aug 18th, 2000, 06:29 AM
#4
Guru
I always complain that Microsoft could have made this thing (finding windows by the beginning of the caption) easier.
Well, they could! 
Anyways, put this code in a Module and nowhere else:
Code:
Option Explicit
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam 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
Dim sStart As String
Dim hWndResult As Long
Dim bSensitive As Boolean
Sub TrimNulls(sString As String)
Dim lPos As Long
lPos = InStr(sString, vbNullChar)
If lPos > 0 Then sString = Left(sString, lPos - 1)
End Sub
Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
Dim sCaption As String
sCaption = String(256, vbNullChar)
Call GetWindowText(hWnd, sCaption, 256)
Call TrimNulls(sCaption)
If (bSensitive And (sCaption Like sStart & "*")) Or (Not bSensitive And (LCase(sCaption) Like LCase(sStart) & "*")) Then
hWndResult = hWnd
Exit Function
End If
EnumWindowsProc = True
End Function
Function FindWindowByBeginning(ByVal sBeginning As String, Optional ByVal bCaseSensitive As Boolean = False) As Long
sStart = sBeginning
bSensitive = bCaseSensitive
Call EnumWindows(AddressOf EnumWindowsProc, 0)
FindWindowByBeginning = hWndResult
End Function
Then: (The following code can go anywhere, not just in a module)
Code:
' This performs a Case-Insensitive search:
' (Will also find things beginning with CALCU and cAlCu, etc.)
hWndTheWindow = FindWindowByBeginning("Calcu")
' This performs a Case-Sensitive search:
' (Will only find things beginning with Calcu in the exact case)
hWndTheWindow = FindWindowByBeginning("Calcu", True)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|