?
Solved

Window title (API) Please help

Posted on 2005-03-31
6
Medium Priority
?
219 Views
Last Modified: 2010-05-02
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
Comment
Question by:bogdem
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 10

Expert Comment

by:edwardiii
ID: 13675294
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
 
LVL 28

Expert Comment

by:Ark
ID: 13676121
DoEvents
0
 
LVL 8

Expert Comment

by:wraith821
ID: 13678352
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:bogdem
ID: 13680677
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
 
LVL 32

Accepted Solution

by:
Erick37 earned 1200 total points
ID: 13682120
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
 

Author Comment

by:bogdem
ID: 13682380
Thanks Erick37
Your code juas what I want. Thanks a lot.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question