PDA

Click to See Complete Forum and Search --> : get class hwnd


da404LewZer
Apr 28th, 2001, 01:49 AM
Hi all..

Is there any way to find the hwnd of a program by knowing the classname?

Thanx in advance!

Vlatko
Apr 28th, 2001, 04:03 AM
Yes, using the FindWindow API, but there may be a lot of windows with the same classname(button,edit...).

da_silvy
Apr 28th, 2001, 05:26 AM
'In a module

Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function SetWindowText Lib "user32.dll" Alias "SetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String) As Long



'In a form


'Example 1
'Finds Microsoft Word Via ClassName (OpusApp) And changes
'Its title to FindWindow Example
Dim nHWND As Long
nHWND = FindWindow("OpusApp", vbNullString)
SetWindowText nHWND, "FindWindow Example"

'Example 2
'Finds Calculator Via Title (Calculator :)) and Changes
'Its title to FindWindow Example
Dim nHWND As Long
nHWND = FindWindow(vbNullString, "Calculator")
SetWindowText nHWND, "FindWindow Example"



:)

Megatron
Apr 28th, 2001, 06:30 AM
Since many applications share the same ClassName, it's a good idea to supply the window name as well.

da_silvy
Apr 28th, 2001, 11:38 PM
Yeah, But isn't it harder for your code to determine the title of an active Word Document which is the title...

da_silvy
May 5th, 2001, 01:11 AM
^bump^

have you checked out this code yet, DA404LEWZER?

Megatron
May 5th, 2001, 11:33 AM
Originally posted by da_silvy
Yeah, But isn't it harder for your code to determine the title of an active Word Document which is the title...

Not really. You can easily use the Like operator to search the title, for whatever matches "* - Micorosft Word Document" (or whatever the constant title is). The astrike stands for a wild card character which means any string.

da_silvy
May 5th, 2001, 07:39 PM
so could you use:



nHWND = FindWindow(vbNullString, Like("* Microsoft Word"))



or what is the syntax?

Megatron
May 6th, 2001, 11:21 AM
You need to use the EnumWindows function to loop through each window.

Add to a Module.

Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim Length As Long
Dim sName As String
Dim Temp As String

Length = GetWindowTextLength(hwnd) + 1
If Length > 1 Then
sName = Space(Length)
GetWindowText hwnd, sName, Length
If Left(sName, Length - 1) Like "*Microsoft Word" Then MsgBox "Word is Open"
End If

EnumWindowsProc = 1
End Function


Use this to trigger the above routine.

EnumWindows AddressOf EnumWindowsProc, 0