Results 1 to 13 of 13

Thread: Handle from Part Caption

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    70

    Handle from Part Caption

    How is this done? I have a sub for it but it just returns true or false.. it doesn't give the handle.

  2. #2
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: Handle from Part Caption

    Please post your code.

    We can't answer your question without codes.

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Handle from Part Caption

    If you're using RobDog's Code then the handle is being passed By Reference:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim lhWndP As Long
    3.     ' lhWndP is equal to 0
    4.     If GetHandleFromPartialCaption(lhWndP, " - Microsoft Outlook") = True Then
    5.         ' lhWndP is modified by function and is now the value for the handle of the window
    6.         MsgBox "Found Window Handle: " & lhWndP, vbOKOnly + vbInformation
    7.     Else
    8.         ' handle wasn't found so lhWndP is equal to 0
    9.         MsgBox "Window ' - Microsoft Outlook' not found!", vbOKOnly + vbExclamation
    10.     End If
    11. End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    70

    Re: Handle from Part Caption

    VB Code:
    1. Private Function AIM_Username()
    2. Dim Handle as long, Username as String
    3. Handle = GetHandleFromPartialCaption(vbNull, "Buddy List Window")
    4. Username = GetWindowCaption(Handle)
    5. Username = Username - Right$(Username, "20")
    6. AIM_Username = Username
    7. End Function
    8.  
    9. Private Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
    10.     Dim lhWndP As Long
    11.     Dim sStr As String
    12.     GetHandleFromPartialCaption = False
    13.     lhWndP = FindWindow(vbNullString, vbNullString)
    14.     Do While lhWndP <> 0
    15.         sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
    16.         GetWindowText lhWndP, sStr, Len(sStr)
    17.         sStr = Left$(sStr, Len(sStr) - 1)
    18.         If InStr(1, sStr, sCaption) > 0 Then
    19.             GetHandleFromPartialCaption = True
    20.             lWnd = lhWndP
    21.             Exit Do
    22.         End If
    23.         handle = GetWindow(lhWndP, GW_HWNDNEXT)
    24.     Loop
    25. End Function
    26.  
    27. Public Function GetWindowCaption(wHWnd As Long) As String
    28.     Dim TxtLen  As Long
    29.     Dim WinTxt  As String * 255
    30.     TxtLen = GetWindowTextLength(wHWnd) + 1
    31.     Call GetWindowText(wHWnd, WinTxt, TxtLen)
    32.     GetWindowCaption = WinTxt
    33. End Function

    I'm essentially trying to create a variable as the person's AIM Screen name which I would like to grab from the title of their Buddy list.
    Last edited by SC I VeNoM I; Apr 19th, 2006 at 09:23 PM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    70

    Re: Handle from Part Caption

    VB Code:
    1. Private Function AIM_Username()
    2. Dim AIMHandle As Long, AIMName As String
    3. AIMHandle = FindWindow(vbNullString, "MedinaSoccaStar's Buddy List Window")
    4. AIMName = GetWindowCaption(AIMHandle)
    5. AIMName = AIMName - Right$(AIMName, "20")
    6. AIM_Username = AIMName
    7. End Function

    That how I'm trying to do it with my screen name directly, but It can vary so I would like to just use "Buddy List Window"

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Handle from Part Caption

    in that case you can modify the function a little:

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim sUsername As String
    3.     sUsername = GetAIMUserName("Buddy List Window")
    4. End Sub
    5.  
    6. Private Function GetAIMUserName(ByVal sCaption As String) As String
    7.     Dim lhWndP As Long, Dim sStr As String
    8.  
    9.     lhWndP = FindWindow(vbNullString, vbNullString)
    10.     Do While lhWndP <> 0
    11.         sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
    12.         GetWindowText lhWndP, sStr, Len(sStr)
    13.         sStr = Left$(sStr, Len(sStr) - 1)
    14.         If InStr(1, sStr, sCaption) > 0 Then
    15.             GetUserName = Trim$(Left$(sStr, Len(sStr) - Len(sCaption)))
    16.             Exit Do
    17.         End If
    18.         lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
    19.     Loop
    20. End Function

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    70

    Re: Handle from Part Caption

    Would that function just grab the handle to it? It returns "" for me.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    70

    Re: Handle from Part Caption

    VB Code:
    1. Option Explicit
    2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
    3. ByVal lpWindowName As String) As Long
    4. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, _
    5. ByVal cch As Long) As Long
    6. Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    7. Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    8.  
    9. Private Const GW_HWNDNEXT = 2
    10.  
    11. Private Sub Command1_Click()
    12. Dim Handle As Long, Username As String
    13. Handle = GetAIMUserName("Buddy List Window")
    14. Username = GetWindowCaption(Handle)
    15. Username = Username - Right$(Username, "20")
    16. MsgBox Username
    17. End Sub
    18.  
    19. Private Function GetAIMUserName(ByVal sCaption As String) As String
    20.     Dim lhWndP As Long, sStr As String, GetUserName As String
    21.  
    22.     lhWndP = FindWindow(vbNullString, vbNullString)
    23.     Do While lhWndP <> 0
    24.         sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
    25.         GetWindowText lhWndP, sStr, Len(sStr)
    26.         sStr = Left$(sStr, Len(sStr) - 1)
    27.         If InStr(1, sStr, sCaption) > 0 Then
    28.             GetUserName = Trim$(Left$(sStr, Len(sStr) - Len(sCaption)))
    29.             Exit Do
    30.         End If
    31.         lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
    32.     Loop
    33. End Function
    34.  
    35. Public Function GetWindowCaption(wHWnd As Long) As String
    36.     Dim TxtLen  As Long
    37.     Dim WinTxt  As String * 255
    38.     TxtLen = GetWindowTextLength(wHWnd) + 1
    39.     Call GetWindowText(wHWnd, WinTxt, TxtLen)
    40.     GetWindowCaption = WinTxt
    41. End Function

    There is all my code. It is not working.

    Thank you for the help!

  9. #9
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Handle from Part Caption

    I gave you an example of how to use the function. It 'should' just return the persons name all by itself:

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim sUsername As String
    3.     sUsername = GetAIMUserName("Buddy List Window")
    4. End Sub
    I've tested it with skype and it works

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    4.                 ByVal lpClassName As String, _
    5.                 ByVal lpWindowName As String) As Long
    6.                
    7. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
    8.                 ByVal hwnd As Long, _
    9.                 ByVal lpString As String, _
    10.                 ByVal cch As Long) As Long
    11.                
    12. Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" ( _
    13.                 ByVal hwnd As Long) As Long
    14.                
    15. Private Declare Function GetWindow Lib "user32" ( _
    16.                 ByVal hwnd As Long, _
    17.                 ByVal wCmd As Long) As Long
    18.  
    19. Private Const GW_HWNDNEXT = 2
    20.  
    21. Private Sub Command1_Click()
    22.     Dim sUsername As String
    23.     sUsername = GetAIMUserName("Buddy List Window")
    24.     Debug.Print sUsername
    25. End Sub
    26.  
    27. Private Function GetAIMUserName(ByVal sCaption As String) As String
    28.     Dim lhWndP As Long, sStr As String, GetUserName As String
    29.  
    30.     lhWndP = FindWindow(vbNullString, vbNullString)
    31.     Do While lhWndP <> 0
    32.         sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
    33.         GetWindowText lhWndP, sStr, Len(sStr)
    34.         sStr = Left$(sStr, Len(sStr) - 1)
    35.         If InStr(1, sStr, sCaption) > 0 Then
    36.             GetUserName = Trim$(Left$(sStr, Len(sStr) - Len(sCaption)))
    37.             Exit Do
    38.         End If
    39.         lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
    40.     Loop
    41. End Function

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    70

    Re: Handle from Part Caption

    How would I tell it to grab the whole title minus the last 20 charachters? Cuz with AIM it has Usernames 's Buddy List Window and i want 's Buddy List Window gone

    EDIT:: Your code still isn't working. Here is a screenshot of what happens with AIM. Do you have AIM 5.9?

    http://i3.tinypic.com/vyxr7s.jpg
    Last edited by SC I VeNoM I; Apr 19th, 2006 at 09:53 PM.

  11. #11
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Handle from Part Caption

    that's what it does.

    if the window's caption is "Bushmobile's Buddy List Window" then GetAIMUserName("Buddy List Window") will return "Bushmobile's"

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    70

    Re: Handle from Part Caption


  13. #13
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Handle from Part Caption

    Does the window caption contain the exact text "Buddy List Window".

    Try sUsername = GetAIMUserName("Buddy List")

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