Hi all..
Is there any way to find the hwnd of a program by knowing the classname?
Thanx in advance!
Printable View
Hi all..
Is there any way to find the hwnd of a program by knowing the classname?
Thanx in advance!
Yes, using the FindWindow API, but there may be a lot of windows with the same classname(button,edit...).
:)Code:'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"
Since many applications share the same ClassName, it's a good idea to supply the window name as well.
Yeah, But isn't it harder for your code to determine the title of an active Word Document which is the title...
^bump^
have you checked out this code yet, DA404LEWZER?
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.Quote:
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...
so could you use:
or what is the syntax?Code:
nHWND = FindWindow(vbNullString, Like("* Microsoft Word"))
You need to use the EnumWindows function to loop through each window.
Add to a Module.
Use this to trigger the above routine.Code: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
Code:EnumWindows AddressOf EnumWindowsProc, 0