Results 1 to 28 of 28

Thread: Find Window handle by Partial Caption

Hybrid View

  1. #1

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

    Find Window handle by Partial Caption

    I didnt see any code in here yet for this so here it is.
    It will find the first window handle that contains the string passed.

    For ex. if you want to find a window handle of Outlook but you dont know
    which folder is currently being viewed (the folder name preceds ' - Microsoft
    Outlook') you can go and pass " - Microsoft Outlook" and it will find Outlooks
    handle if it is running.

    Code:
    Option Explicit
    '<VB/OUTLOOK GURU 01/30/2004 - CODE TO FIND WINDOW HANDLE BASED ON PARTIAL WINDOW CAPTION>
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, _
    ByVal cch As Long) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    
    Private Const GW_HWNDNEXT = 2
    
    Private Sub Command1_Click()
        Dim lhWndP As Long
        If GetHandleFromPartialCaption(lhWndP, " - Microsoft Outlook") = True Then
            MsgBox "Found Window Handle: " & lhWndP, vbOKOnly + vbInformation
        Else
            MsgBox "Window ' - Microsoft Outlook' not found!", vbOKOnly + vbExclamation
        End If
    End Sub
    
    Private Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
        Dim lhWndP As Long
        Dim sStr As String
        GetHandleFromPartialCaption = False
        lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
        Do While lhWndP <> 0
            sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
            GetWindowText lhWndP, sStr, Len(sStr)
            sStr = Left$(sStr, Len(sStr) - 1)
            If InStr(1, sStr, sCaption) > 0 Then
                GetHandleFromPartialCaption = True
                lWnd = lhWndP
                Exit Do
            End If
            lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
        Loop
    End Function
    Last edited by RobDog888; May 3rd, 2007 at 10:47 AM. Reason: Fixed [highlight] tags
    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

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

    Re: Find Window handle by Partial Caption

    Don't know if it's the same or similar but http://www.vbforums.com/showpost.php...7&postcount=36


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

  3. #3

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

    Re: Find Window handle by Partial Caption

    I hadn't seen that code yet. I will check it out later when I get some time.
    Probably similar logic though, oh well.
    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

  4. #4
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    488

    Re: Find Window handle by Partial Caption

    Hi RobDog888,

    The solution you suggested works #1 for me - Thanks. I have a question for you now tho. Once I've found the window I want, I am now interested in doing the same procedure, but now I want to find CONTROLS handle inside that window, based on a partial caption.

    Ex.: The window in question has 4 buttons, always with different restaurant names as captions. I'd need to find the button that has, for exemple, "Burger" in the button caption. I used WinInspector to watch the window and I can see the buttons captions correctly.

    Do you have an idea on how I could achieve that?

    Thanks a lot.
    Chris

  5. #5

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

    Re: Find Window handle by Partial Caption

    There is a property called a Control ID. This is the ID of the control and doesnt change. If you can see it with WinInspector then there is an API call to get it - GetDlgCtrlID.
    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

  6. #6
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    488

    Re: Find Window handle by Partial Caption

    If I got that right, I need to use the GetDlgCtrlID API to get the control ID? If so, then I won't need to use it since Winspector Spy gives me that ID number, and you were right, it never changes - thanks for the tip.

    But now, I guess I'd need some of your professionnal help on how to use that ID number. I will need to check the caption of that button (that I will extract and use in a variable).

    Afterward, when all of this is working well, I will need to click that button. I normally use mouse_event MOUSEEVENTF_MOVE (considering I have the X,Y coordinates) to click those buttons, but it'd be great if an API could click it for me. That way it wouldn't move my mouse on the screen and I could work at the same time.

    The Control ID I want to extract the caption and click is "4001".

    Thanks for your very useful help
    Chris

  7. #7

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

    Re: Find Window handle by Partial Caption

    Yes, you can SendMessage with the BM_CLICK parameter to do the button click without moving your mouse at all.

    Not exactly, but the way it works is to enumerate the child windows checking each's DlgCtlID for a match of "4001". When you find the match then you use that handle to GetWindowText to retrieve the button text. Then that same handle can be used to SendMessage to click it.


    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

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

    Re: Find Window handle by Partial Caption

    I played with this quite a bit and things are progressing - thanks.

    I got a small problem.
    I am pretty sure you can easily point me in the right direction again on that one. You suggested to use SendMessage and BM_CLICK to click the buttons. That worked well, but only on SOME controls. Winspector detects some controls with the "Button" class name and these are working pretty good.

    BUT there are some other buttons I want to click that are using the "AfxWnd42" class name. BM_CLICK doesn't work on those. Any idea on how to click those?

    btw, thank you for replying my posts so quickly - I'm progressing a whole lot more faster.
    Chris

  9. #9

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

    Re: Find Window handle by Partial Caption

    Sometimes I find that I have to send the bm_click message 2x but dont try on some buttons that do an action other then a close or cancle.

    I came across this
    Those Afx window classes were custom classes distributed with early versions of MFC, before the "Common Controls" concept was introduced.

    Many people have tried to "read" text from them but you CAN'T, not with messages anyway!

    They're almost certainly owner-drawn - the text is not delivered by messages, it's "hand-drawn" into the window's DC
    And that may well be the reason.

    About the only thing I see here is with subclassing the pother program which is aloot of work. Here i a good article on subclassing that may lead you to either dispare or guidence to a possible solution.

    http://www.codeproject.com/system/ap..._unleashed.asp

    Good luck as this is about as much help on it that I think I can give.
    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

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

    Re: Find Window handle by Partial Caption

    Strange because I WAS able to read the button caption of a Afx window class using GetText. ...And clicking the control twice or more didn't help neither.

    So if there's no other solution I have no choice but to stick with those mouse_event MOUSEEVENTF_MOVE & CLICK methods. Sure it would have been better to use SendMessage/BM_CLICK and free my mouse mouvements. In that matter, would there be any api to "fake/simulate" a click to X,Y coords without affecting my mouse?
    Chris

  11. #11

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

    Re: Find Window handle by Partial Caption

    You could also send a WM_LBUTTONDOWN and WM_LBUTTONUP on the button to simulate a left mouse click without moving the mouse.
    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

  12. #12
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    488

    Re: Find Window handle by Partial Caption

    SendMessage wasn't working - PostMessage made the trick.

    With winspector, along with all properties (ID, name, etc) there's a "Class specific" property which is set to "checked" or "unchecked". Do you have an idea on how to retrieve that information?

    Thanks.
    Chris

  13. #13

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

    Re: Find Window handle by Partial Caption

    Sorry but nope. Maybe a new thread in the API forum on this will get more input as members may think your just having issues with this code when its more about this special case.

    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

  14. #14
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: Find Window handle by Partial Caption

    Rob thank u for sharing this code. coul u tell me how to bring the window that we suppli its caption to front . I mean bring it to frong if it is behind windows explorers or other windows .thank

  15. #15
    New Member
    Join Date
    Nov 2006
    Posts
    13

    Re: Find Window handle by Partial Caption

    How would i go about doing this in .NET 2005 ? this is what iv got so far having some trouble though.

    VB Code:
    1. Option Strict Off
    2. Option Explicit On
    3. Public Class Form1
    4.  
    5.     '<VB/OUTLOOK GURU 01/30/2004 - CODE TO FIND WINDOW HANDLE BASED ON PARTIAL WINDOW CAPTION>
    6.     Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
    7.     ByVal lpWindowName As String) As Long
    8.     Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, _
    9.     ByVal cch As Long) As Long
    10.     Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    11.     Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    12.  
    13.     Private Const GW_HWNDNEXT = 2
    14.  
    15.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    16.         Dim lhWndP As Long
    17.         If GetHandleFromPartialCaption(lhWndP, " - Microsoft Outlook") = True Then
    18.             MsgBox("Found Window Handle: " & lhWndP, vbOKOnly + vbInformation)
    19.         Else
    20.             MsgBox("Window ' - Microsoft Outlook' not found!", vbOKOnly + vbExclamation)
    21.         End If
    22.  
    23.     End Sub
    24.     Private Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
    25.         Dim lhWndP As Long
    26.         Dim sStr As String
    27.         GetHandleFromPartialCaption = False
    28.         lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
    29.         Do While lhWndP <> 0
    30.         sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
    31.             GetWindowText(lhWndP, sStr, Len(sStr))
    32.             sStr = Left$(sStr, Len(sStr) - 1)
    33.             If InStr(1, sStr, sCaption) > 0 Then
    34.                 GetHandleFromPartialCaption = True
    35.                 lWnd = lhWndP
    36.                 Exit Do
    37.             End If
    38.             lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
    39.         Loop
    40.     End Function
    41. End Class

  16. #16
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find Window handle by Partial Caption

    whats the error?

  17. #17
    New Member
    Join Date
    Nov 2006
    Posts
    13

    Re: Find Window handle by Partial Caption

    this is what it looks like on my end



    seem to be having trouble with these 2 lines


    VB Code:
    1. sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
    2.  
    3. sStr = Left$(sStr, Len(sStr) - 1)

  18. #18

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

    Re: Find Window handle by Partial Caption

    VB Code:
    1. sStr = sStr.PadRight(GetWindowTextLength(lhWndP) + 1, Chr(0))
    VB Code:
    1. sStr = sStr.Length - 1
    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

  19. #19
    New Member
    Join Date
    Nov 2006
    Posts
    13

    Re: Find Window handle by Partial Caption

    ok almost there i think but im getting this now on the new code that you gave me
    "A call to PInvoke function 'WindowsApplication1!WindowsApplication1.Module1::GetWindowTextLength' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."




    Module
    VB Code:
    1. Module Module1
    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.     Public Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
    12.         Dim lhWndP As Long
    13.         Dim sStr As String
    14.         GetHandleFromPartialCaption = False
    15.         lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
    16.         Do While lhWndP <> 0
    17.             sStr = sStr.PadRight(GetWindowTextLength(lhWndP) + 1, Chr(0))
    18.             GetWindowText(lhWndP, sStr, Len(sStr))
    19.             sStr = sStr.Length - 1
    20.             If InStr(1, sStr, sCaption) > 0 Then
    21.                 GetHandleFromPartialCaption = True
    22.                 lWnd = lhWndP
    23.                 Exit Do
    24.             End If
    25.             lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
    26.         Loop
    27.     End Function
    28. End Module


    call function

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim lhWndP As Long
    3.         If GetHandleFromPartialCaption(lhWndP, "window looking") = True Then
    4.             MsgBox("Found Window Handle: " & lhWndP, vbOKOnly + vbInformation)
    5.         Else
    6.             MsgBox("Window ' - Microsoft Outlook' not found!", vbOKOnly + vbExclamation)
    7.         End If
    8.  
    9.     End Sub

  20. #20

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

    Re: Find Window handle by Partial Caption

    Ok, just noticed that you are using the VB 6 version of the API calls. You need to use the VB.NET syntax.
    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Imports System.Runtime.InteropServices
    5.  
    6. Module Module1
    7.  
    8. Inherits System.Windows.Forms.Form
    9.  
    10. <DllImport("user32", EntryPoint:="GetWindowTextLength")> _
    11. Private Shared Function GetWindowTextLength  ( _
    12.                       ByVal hwnd As Int32) As Int32
    13. End Function
    14.  
    15. <DllImport("user32", EntryPoint:="FindWindow")> _
    16. Private Shared Function FindWindow ( _
    17.                       ByVal lpClassName As String, _
    18.                       ByVal lpWindowName As String) As Int32
    19. End Function
    20.  
    21. <DllImport("user32", EntryPoint:="GetWindowText")> _
    22. Private Shared Function GetWindowText ( _
    23.                       ByVal hwnd As Int32, _
    24.                       ByVal lpString As String, _
    25.                       ByVal cch As Int32) As Int32
    26. End Function
    27.  
    28. <DllImport("user32", EntryPoint:="GetWindow")> _
    29. Private Shared Function GetWindow ( _
    30.                       ByVal hwnd As Int32, _
    31.                       ByVal wCmd As Int32) As Int32
    32. End Function
    33.  
    34. Private Const GW_HWNDNEXT As Int32 = 2
    35.  
    36. '...
    Last edited by RobDog888; Nov 20th, 2006 at 01:06 PM.
    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

  21. #21
    New Member
    Join Date
    Nov 2006
    Posts
    13

    Re: Find Window handle by Partial Caption

    ok this is what im getting now

    thanks so much for your help





    VB Code:
    1. Module Module1
    2.     Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" ( _
    3.                           ByVal hwnd As Int32) As Int32
    4.  
    5.     Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
    6.                           ByVal lpClassName As String, _
    7.                           ByVal lpWindowName As String) As Int32
    8.  
    9.     Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" ( _
    10.                           ByVal hwnd As Int32, _
    11.                           ByVal lpString As String, _
    12.                           ByVal cch As Int32) As Int32
    13.  
    14.     Private Declare Function GetWindow Lib "user32.dll" ( _
    15.                           ByVal hwnd As Int32, _
    16.                           ByVal wCmd As Int32) As Int32
    17.  
    18.     Private Const GW_HWNDNEXT As Int32 = 2
    19.  
    20.  
    21.     Public Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
    22.         Dim lhWndP As Long
    23.         Dim sStr As String
    24.         GetHandleFromPartialCaption = False
    25.         lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
    26.         Do While lhWndP <> 0
    27.             sStr = sStr.PadRight(GetWindowTextLength(lhWndP) + 1, Chr(0))
    28.             GetWindowText(lhWndP, sStr, Len(sStr))
    29.             sStr = sStr.Length - 1
    30.             If InStr(1, sStr, sCaption) > 0 Then
    31.                 GetHandleFromPartialCaption = True
    32.                 lWnd = lhWndP
    33.                 Exit Do
    34.             End If
    35.             lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
    36.         Loop
    37.     End Function
    38. End Module

  22. #22
    New Member
    Join Date
    Nov 2006
    Posts
    13

    Re: Find Window handle by Partial Caption

    lol just saw that you updated the code sorry let me try that now see if it helps thanks so much for your time

  23. #23

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

    Re: Find Window handle by Partial Caption

    Change the dimensioning of the sStr var like so.

    VB Code:
    1. Dim sStr As String = String.Empty
    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

  24. #24
    New Member
    Join Date
    Nov 2006
    Posts
    13

    Re: Find Window handle by Partial Caption

    ok i got the code to run in debug but its not picking up my window i know the partial caption is right its same as the one i have for VB6 and it works..... its doing the else line like if it didnt find the window at all and im sure its there.


    VB Code:
    1. Option Explicit On
    2. Option Strict Off
    3.  
    4. Imports System.Runtime.InteropServices
    5.  
    6.  
    7.  
    8. Module Module1
    9.  
    10.  
    11.     Private Const GW_HWNDNEXT As Int32 = 2
    12.     <DllImport("user32", EntryPoint:="GetWindowTextLength")> _
    13.     Public Function GetWindowTextLength( _
    14.                           ByVal hwnd As Int32) As Int32
    15.     End Function
    16.  
    17.     <DllImport("user32", EntryPoint:="FindWindow")> _
    18.     Public Function FindWindow( _
    19.                           ByVal lpClassName As String, _
    20.                           ByVal lpWindowName As String) As Int32
    21.     End Function
    22.  
    23.     <DllImport("user32", EntryPoint:="GetWindowText")> _
    24.     Public Function GetWindowText( _
    25.                           ByVal hwnd As Int32, _
    26.                           ByVal lpString As String, _
    27.                           ByVal cch As Int32) As Int32
    28.     End Function
    29.  
    30.     <DllImport("user32", EntryPoint:="GetWindow")> _
    31.     Public Function GetWindow( _
    32.                           ByVal hwnd As Int32, _
    33.                           ByVal wCmd As Int32) As Int32
    34.     End Function
    35.  
    36.  
    37.  
    38.  
    39.     Public Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
    40.         Dim lhWndP As Long
    41.         Dim sStr As String = String.Empty
    42.         GetHandleFromPartialCaption = False
    43.         lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
    44.         Do While lhWndP <> 0
    45.             sStr = sStr.PadRight(GetWindowTextLength(lhWndP) + 1, Chr(0))
    46.             GetWindowText(lhWndP, sStr, Len(sStr))
    47.             sStr = sStr.Length - 1
    48.             If InStr(1, sStr, sCaption) > 0 Then
    49.                 GetHandleFromPartialCaption = True
    50.                 lWnd = lhWndP
    51.                 Exit Do
    52.             End If
    53.             lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
    54.         Loop
    55.     End Function
    56. End Module

    VB Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim lhWndP As Long
    5.         If GetHandleFromPartialCaption(lhWndP, "Play money") = True Then
    6.             MsgBox("Found Window Handle: " & lhWndP, vbOKOnly + vbInformation)
    7.         Else
    8.             MsgBox("Window ' - Microsoft Outlook' not found!", vbOKOnly + vbExclamation)
    9.         End If
    10.  
    11.     End Sub
    12. End Class
    Last edited by _JiMMiE_; Nov 20th, 2006 at 03:02 PM.

  25. #25

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

    Re: Find Window handle by Partial Caption

    Step through the "GetHandleFromPartialCaption" procedure checking the values of each var.
    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

  26. #26
    New Member
    Join Date
    Nov 2006
    Posts
    13

    Re: Find Window handle by Partial Caption

    ok iv'e run over and over this and cant get it to work the same function in VB6 works fine the code runs in debug fine but GetHandleFromPartialCaption is always false here is what i have so far i put it all in the form thinkin the module was messing it up some how but it still does the same thing im pulling my hair out ok here it is tell me what you think


    VB Code:
    1. Option Explicit On
    2. Option Strict Off
    3.  
    4. Imports System.Runtime.InteropServices
    5. Public Class Form1
    6.     Inherits System.Windows.Forms.Form
    7.  
    8.     <DllImport("user32", EntryPoint:="GetWindowTextLength")> _
    9.     Private Shared Function GetWindowTextLength( _
    10.                           ByVal hwnd As Int32) As Int32
    11.     End Function
    12.  
    13.     <DllImport("user32", EntryPoint:="FindWindow")> _
    14.     Private Shared Function FindWindow( _
    15.                           ByVal lpClassName As String, _
    16.                           ByVal lpWindowName As String) As Int32
    17.     End Function
    18.  
    19.     <DllImport("user32", EntryPoint:="GetWindowText")> _
    20.     Private Shared Function GetWindowText( _
    21.                           ByVal hwnd As Int32, _
    22.                           ByVal lpString As String, _
    23.                           ByVal cch As Int32) As Int32
    24.     End Function
    25.  
    26.     <DllImport("user32", EntryPoint:="GetWindow")> _
    27.     Private Shared Function GetWindow( _
    28.                           ByVal hwnd As Int32, _
    29.                           ByVal wCmd As Int32) As Int32
    30.     End Function
    31.  
    32.     Private Const GW_HWNDNEXT As Int32 = 2
    33.  
    34.  
    35.  
    36.     Private Function GetHandleFromPartialCaption(ByRef lWnd As Int32, ByVal sCaption As String) As Boolean
    37.         Dim lhWndP As Long
    38.         Dim sStr As String = String.Empty
    39.         GetHandleFromPartialCaption = False
    40.         lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
    41.         Do While lhWndP <> 0
    42.             sStr = sStr.PadRight(GetWindowTextLength(lhWndP) + 1, Chr(0))
    43.             GetWindowText(lhWndP, sStr, Len(sStr))
    44.             sStr = sStr.Length - 1
    45.             If InStr(1, sStr, sCaption) > 0 Then
    46.                 GetHandleFromPartialCaption = True
    47.                 lWnd = lhWndP
    48.                 Exit Do
    49.             End If
    50.             lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
    51.         Loop
    52.     End Function
    53.  
    54.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    55.         Dim lhWndP As Long
    56.         GetHandleFromPartialCaption(lhWndP, "look window")
    57.         MsgBox("HANDLE.. " & lhWndP, vbOKOnly + vbInformation)
    58.     End Sub
    59.  
    60. End Class

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

    Re: Find Window handle by Partial Caption

    Are you sure sStr.PadRight is the right way to go? It seems like the only change that was made, and reading the description (and not knowing .Net) it seems like it doesn't do the same as the VB6 code...


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

  28. #28
    New Member
    Join Date
    Nov 2006
    Posts
    13

    Re: Find Window handle by Partial Caption

    Thanks for the reply..... im not real sure of what im doing wrong i was hoping maybe robdog could help maybe he has been busy or just has not come up with a fix just yet, either way i dont have a problem waiting

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