Results 1 to 8 of 8

Thread: Finding the Window Handle from the Titlebar Caption

  1. #1
    Guest
    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...?

  2. #2
    Addicted Member
    Join Date
    May 00
    Posts
    240

    Wink GetClassName API

    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.

  3. #3
    Guest
    He! He! He!

    Here's the declare for that function:
    Code:
    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!

    I'll have a look at that website tho.

    Ta.

  4. #4
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 00
    Location
    Minnesota
    Posts
    666
    Use FindWindow:
    Code:
    '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
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  5. #5
    Guest
    Code:
    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

  6. #6
    Guest

    Cheers, people! :)

    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.

  7. #7
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 00
    Location
    Minnesota
    Posts
    666
    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:
    Code:
    FindWindow("IEFrame",vbNullString)
    'vs.
    FindWindow(vbNullString,"VB Q and A - Reply to Topic - Microsoft Internet Explorer")
    I think that is correct
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  8. #8
    Guest
    FindWindowEx's arguments are somewhat out of wack. Here is a proper definition for it.
    Code:
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •