• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Window title (API) Please help

I'm trying to create the function which gives me window whole title
even know I have only a part of the title. And also this function should wait until window will be open

here are the code

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 GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hWnd As Long, ByVal wFlag As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public Function GetCaption(lhWnd As Long) As String
    Dim sA As String, lLen As Long
   
    lLen& = GetWindowTextLength(lhWnd&)
    sA$ = String(lLen&, 0&)
   
    Call GetWindowText(lhWnd&, sA$, lLen& + 1)
    GetCaption$ = sA$
End Function

Public Function DLHFindWin(frm As Form, WinTitle As String, CaseSensitive As Boolean) As Long
Dim lhWnd As Long, sA As String
       
        lhWnd& = frm.hWnd
            Do
                DoEvents
                    If lhWnd& = 0 Then Exit Do
                        If CaseSensitive = False Then
                            sA$ = LCase$(GetCaption(lhWnd&))
                            WinTitle$ = LCase$(WinTitle$)
                        Else
                            sA$ = GetCaption(lhWnd&)
                        End If
                       
                    If InStr(sA$, WinTitle$) Then
                        DLHFindWin& = lhWnd&
                        Exit Do
                    Else
                        DLHFindWin& = 0
                    End If
                lhWnd& = GetNextWindow(lhWnd&, 2)
            Loop
End Function

Private Sub Command1_Click()
    Dim i
   
   
    Do
       
            i = i + 1
            adobyWinTitle = (GetCaption$(DLHFindWin&(Me, "Adobe", False)))
       
        If adobyWinTitle = "" Then
            Sleep 2000
        Else
            MsgBox ("Title is ") & adobyWinTitle
        End If
       
            Text1.Text = "Try = " & i & " Name " & adobyWinTitle

    Loop
         
End Sub


Problem is:
If Adobe window already open no problem, if app waiting for the window
MsgBox never show up. Why?

Please help.
0
bogdem
Asked:
bogdem
1 Solution
 
edwardiiiCommented:
Hi, bogdem.

It would seem you're not getting a MsbBox popup when the Adobe window isn't open because the code only instructs your app to sleep and nothing else.  Trying changing:

        If adobyWinTitle = "" Then
            Sleep 2000
        Else
            MsgBox ("Title is ") & adobyWinTitle
        End If
to:

       If adobyWinTitle = "" Then
            Msgbox "Adobe window is not open yet"
            Sleep 2000

This will bombard you with a stream of Message boxes; it might be more convenient to set the status in one of your VB form's labels (e.g. label1.caption = "Adobe window is not open yet").
        Else
            MsgBox ("Title is ") & adobyWinTitle
        End If

0
 
ArkCommented:
DoEvents
0
 
wraith821Commented:
original code worked just fine on my machine. I ran the code then opened Adobe. i got the message when Adobe window was opened. then i ran Adobe, then the code. imediatly when i ran the code i got the message. explain more please
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bogdemAuthor Commented:
To edwardiii,
When Adobe isn't open, I don't need to see any messages, that's a point.

To Ark,
DoEvents ??? Can you explain?

To wraith821,
I'm starting app first, then Adobe at this point Adoby takes focus, now I have to take focus back on the form with a click
and in two sec I'm getting the MsgBox.
I want to see the MsgBox without any click, because in real life instant msgBox will be another function.
Thanks.
0
 
Erick37Commented:
It's probably better to search for your window in a timer event instead of a loop.
Here is a different approach to the problem:


Option Explicit

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 FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long


Private Function FindWindowCaptionLike(ByVal LikeString As String) As String
   
    Dim lhWndDesk As Long
    Dim lhWndChild As Long
    Dim sWinText As String
    Dim lLen As Long
   
    lhWndDesk = GetDesktopWindow()
   
    Do
        'get the handles of the Desktop child windows
        lhWndChild = FindWindowEx(lhWndDesk, lhWndChild, vbNullString, vbNullString)
       
        'test to see if there are any more windows to examine
        If lhWndChild <> 0 Then
           
            'get the window caption
            lLen = GetWindowTextLength(lhWndChild)
            sWinText = String(lLen, 0)
            Call GetWindowText(lhWndChild, sWinText, lLen + 1)
       
            Debug.Print Hex(lhWndChild) & ": " & sWinText
           
            If sWinText Like LikeString Then
                'found a match, exit
                FindWindowCaptionLike = sWinText
                Exit Do
            End If
       
        Else
            'No more windows
            FindWindowCaptionLike = ""
            Exit Do
        End If
       
    Loop

End Function

Private Sub Timer1_Timer()
   
    Text1.Text = FindWindowCaptionLike("*Adobe*")
   
End Sub
0
 
bogdemAuthor Commented:
Thanks Erick37
Your code juas what I want. Thanks a lot.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now