|
-
Apr 28th, 2001, 01:49 AM
#1
Thread Starter
Lively Member
get class hwnd
Hi all..
Is there any way to find the hwnd of a program by knowing the classname?
Thanx in advance!
Kid A
18 Year Old Programmer
Visual Basic 6 & .NET Enterprise, ASP, WinXP (Advanced Server) Administration, HTML, Graphic Arts, Winsock, Learning VC++ and now maybe C#.. heh
[vbcode]
'back in the day vb6 code
Private Sub My_Life()
If Hour(Now) > 3 And Hour(Now) < 13 Then
Status = "Sleeping"
Else
Status = "Computing"
End If
End Sub
[/vbcode]
-
Apr 28th, 2001, 04:03 AM
#2
Frenzied Member
Yes, using the FindWindow API, but there may be a lot of windows with the same classname(button,edit...).
-
Apr 28th, 2001, 05:26 AM
#3
Conquistador
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"
-
Apr 28th, 2001, 06:30 AM
#4
Since many applications share the same ClassName, it's a good idea to supply the window name as well.
-
Apr 28th, 2001, 11:38 PM
#5
Conquistador
Yeah, But isn't it harder for your code to determine the title of an active Word Document which is the title...
-
May 5th, 2001, 01:11 AM
#6
Conquistador
^bump^
have you checked out this code yet, DA404LEWZER?
-
May 5th, 2001, 11:33 AM
#7
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.
-
May 5th, 2001, 07:39 PM
#8
Conquistador
so could you use:
Code:
nHWND = FindWindow(vbNullString, Like("* Microsoft Word"))
or what is the syntax?
-
May 6th, 2001, 11:21 AM
#9
You need to use the EnumWindows function to loop through each window.
Add to a Module.
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
Use this to trigger the above routine.
Code:
EnumWindows AddressOf EnumWindowsProc, 0
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
|