Results 1 to 11 of 11

Thread: Find all buttons or statics of a parten hWnd

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2005
    Posts
    76

    Find all buttons or statics of a parten hWnd

    I am trying to get the caption of a class called static off of another app. There is more than one of static type class on the parent. Is there some way I can cycle through each "static" and get the caption from it? Or maybe is there someway that I can specify a partial caption and class "static" and then just get a single match? Thanks!

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Find all buttons or statics of a parten hWnd

    I think that if you pass the previous hwnd of the object in the FindWindowEx API (I assume you're using it), you get the next one. I'll have to check though to be sure


    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Find all buttons or statics of a parten hWnd

    OK, something along these lines? I tested it with a form and 3 textboxes and it worked :

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. 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
    5. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    6. Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    7.  
    8. Private Sub Command1_Click()
    9.     Dim h As Long
    10.     Dim hChild As Long
    11.     Dim sChildText As String
    12.    
    13.     h = FindWindow(vbNullString, "Form1")
    14.    
    15.     Do
    16.         hChild = FindWindowEx(h, hChild, "ThunderTextBox", vbNullString)
    17.         If hChild <> 0 Then
    18.             sChildText = String(GetWindowTextLength(hChild) + 1, Chr$(0))
    19.             GetWindowText hChild, sChildText, Len(sChildText)
    20.             Debug.Print sChildText
    21.         End If
    22.     Loop While hChild <> 0
    23. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Find all buttons or statics of a parten hWnd

    If you know the control id (you can find it using MS Spy++) you can use this API to get the
    handle of it directly (and thus the caption) instead of looping.

    VB Code:
    1. Private Declare Function GetDlgCtrlID Lib "user32.dll" (ByVal hwnd As Long) As Long
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Find all buttons or statics of a parten hWnd

    Quote Originally Posted by RobDog888
    If you know the control id (you can find it using MS Spy++) you can use this API to get the
    handle of it directly (and thus the caption) instead of looping.

    VB Code:
    1. Private Declare Function GetDlgCtrlID Lib "user32.dll" (ByVal hwnd As Long) As Long
    You wouldn't happen to have an example for that? I couldn't find it in the API Guide


    Has someone helped you? Then you can Rate their helpful post.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Find all buttons or statics of a parten hWnd

    My code ex is at work But I think I made a mistake (I can admit it ). The API works reverse
    of how I described it (I think I have a bit of Dyslexia). You would need to enumerate through
    the static windows checking if the id matches. This API is mostly used for when the caption
    is a nullstring and you have several nullstring captions of the same window class.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Find all buttons or statics of a parten hWnd

    Quote Originally Posted by RobDog888
    My code ex is at work But I think I made a mistake (I can admit it ). The API works reverse
    of how I described it (I think I have a bit of Dyslexia). You would need to enumerate through
    the static windows checking if the id matches. This API is mostly used for when the caption
    is a nullstring and you have several nullstring captions of the same window class.
    That makes more sence since the handle is passed to the function


    Has someone helped you? Then you can Rate their helpful post.

  8. #8
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    489

    Re: Find all buttons or statics of a parten hWnd

    manavo11, I tried your code to list captions of all buttons on a msgbox from another app - worked PERFECTLY - thanks!

    I was able to automatically click the "OK" button. But now what if I need to see the caption/text of the msgbox first, can that be done?

    I tried playing with your line of code:
    VB Code:
    1. hChild = FindWindowEx(h, hChild, "Button", vbNullString)

    ...by replacing "Button" with "Label", "Caption", or whatever... none returned me the value of the msgbox caption.

    Any idea?
    Chris

  9. #9
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Find all buttons or statics of a parten hWnd

    Quote Originally Posted by Krass
    manavo11, I tried your code to list captions of all buttons on a msgbox from another app - worked PERFECTLY - thanks!

    I was able to automatically click the "OK" button. But now what if I need to see the caption/text of the msgbox first, can that be done?

    I tried playing with your line of code:
    VB Code:
    1. hChild = FindWindowEx(h, hChild, "Button", vbNullString)

    ...by replacing "Button" with "Label", "Caption", or whatever... none returned me the value of the msgbox caption.

    Any idea?
    I'm glad you found it useful Now for your specific problem, you need to search for "Static", not label or anything like that :

    VB Code:
    1. hChild = FindWindowEx(h, hChild, "Static", vbNullString)



    Has someone helped you? Then you can Rate their helpful post.

  10. #10
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    489

    Re: Find all buttons or statics of a parten hWnd

    This code works good:
    VB Code:
    1. hChild = FindWindowEx(h, hChild, "Button", vbNullString)

    But how come it traps buttons ONLY from an IE msgbox? ("Proceed with entry submission, OK/CANCEL")... I want to use this method on one my own Vb application, which contains textboxes and buttons... But it doesn't seem to see any of them...

    Any idea?

    (my form has a button called "Command1")

    Thanks
    Chris

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Find all buttons or statics of a parten hWnd

    Do you have the right window title in this line?

    VB Code:
    1. h = FindWindow(vbNullString, "Form1")

    I have actually only tested it with VB messageboxes, so I know for a fact it works


    Has someone helped you? Then you can Rate their helpful post.

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