PDA

Click to See Complete Forum and Search --> : Finding the Window Handle from the Titlebar Caption


Sep 12th, 2000, 05:45 PM
How do I find a window's handle if I know what it says in the titlebar?

I assume I'd use FindWindow(Ex?) but I just can't crack it...I know I've done it in the past, but now...? :)

Sophtware
Sep 12th, 2000, 06:22 PM
I dont remember for sure..but i think if you want to get a windows handle...you should use the "GetClassName" API or something... try looking for that at http://www.allapi.com i remember seeing it there b4.



Sorry i cant provide code..but like i said.. i forgot.

Sep 12th, 2000, 06:26 PM
He! He! He!

Here's the declare for that function:Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Notice that is asks for the class name and the window handle?! Both things that I ain't got!! Duh! :rolleyes:

I'll have a look at that website tho. :)

Ta.

gwdash
Sep 12th, 2000, 06:47 PM
Use FindWindow:

'place this code in a module
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Function GetWindowHwnd(Caption As String) As Long
If Len(Caption) = 0 Then
GetWindowHwnd = 0
Exit Function
EndIf
GetWindowHwnd = FindWindow(vbNullString,Caption)
End Function

Sep 13th, 2000, 02:31 PM
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Sub Command1_Click()

Dim hApp As Long
hApp = FindWindowEx(vbNullString, "MyAppTitle"

If hApp <> 0 Then MsgBox("The App is open")

End Sub

Sep 14th, 2000, 03:53 AM
I'd sorta half figured out what to do, I just couldn't workout what I should specify for FindWindow's first argument.

I'd tried Null, Empty, "", vbNull...hadn't realised there was a vbNullString I had to use!!!

Why the heck does VB have to have so many ways of saying "Bugger All"?! :)

Well... t'works now, so I'm happy. Ta.

gwdash
Sep 14th, 2000, 07:06 AM
The First Arg is Classname, almost all API's that will be left blank are either vbNullString or 0 (for numbers). You use classname if you want to get any instance, say:

FindWindow("IEFrame",vbNullString)
'vs.
FindWindow(vbNullString,"VB Q and A - Reply to Topic - Microsoft Internet Explorer")

I think that is correct:)

Sep 14th, 2000, 02:40 PM
FindWindowEx's arguments are somewhat out of wack. Here is a proper definition for it.

Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hParent As Long, ByVal hChild As Long, ByVal lpClassName As String, ByVal lpWindowName As String) As Long