Results 1 to 9 of 9

Thread: get class hwnd

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2000
    Location
    LewZer-LanD
    Posts
    120

    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]

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Yes, using the FindWindow API, but there may be a lot of windows with the same classname(button,edit...).
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    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"

  4. #4
    Megatron
    Guest
    Since many applications share the same ClassName, it's a good idea to supply the window name as well.

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Yeah, But isn't it harder for your code to determine the title of an active Word Document which is the title...

  6. #6
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    ^bump^

    have you checked out this code yet, DA404LEWZER?

  7. #7
    Megatron
    Guest
    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.

  8. #8
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    so could you use:

    Code:
    nHWND = FindWindow(vbNullString, Like("* Microsoft Word"))
    or what is the syntax?

  9. #9
    Megatron
    Guest
    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
  •  



Click Here to Expand Forum to Full Width