Results 1 to 7 of 7

Thread: [RESOLVED] Using FindWindow to access External Windows, need help with my process

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Resolved [RESOLVED] Using FindWindow to access External Windows, need help with my process

    I am using consecutive loops to search for a a sequence of windows and automate clicking command buttons but sometimes some random windows pop up in between the normal windows and my program gets stuck in a loop until it finds the window it's looking for.

    This is my code, is there a better way of doing this?

    Code:
    Private Sub cmdYes_Click()
        Dim hWndPriceWindow As Long
        Dim hWndPriceBtn As Long
        Dim hWndOkayBtn As Long
        Dim hWndDispenseWindow As Long
        Dim hWndDispenseBtn As Long
        
        'Find first popup window
        Do
            DoEvents
    
            If frmSettings.chkWildcard.Value = 0 Then
                hWndPriceWindow = FindWindow(vbNullString, frmSettings.txtS3.Text)
            Else
                hWndPriceWindow = FindWindowWild(frmSettings.txtS3.Text)
            End If
       
        Loop Until hWndPriceWindow <> 0
        
        Do
            DoEvents
            hWndPriceBtn = FindWindowEx(hWndPriceWindow, 0&, "Button", "&Yes")
        Loop Until hWndPriceBtn <> 0
    
        Do
            DoEvents
            PostMessage hWndPriceBtn, BM_CLICK, 0, ByVal 0&
        Loop Until IsWindow(hWndPriceWindow) = 0
    
        Do
            DoEvents
            hWndOkayBtn = FindWindowEx(hWndParent, 0&, "Button", "Okay")
        Loop Until hWndOkayBtn <> 0
    
        PostMessage hWndOkayBtn, BM_CLICK, 0, ByVal 0&
    
        ' Find second popup window
        Do
            DoEvents
            hWndDispenseWindow = FindWindow(vbNullString, "Dispense")
        Loop Until hWndDispenseWindow <> 0
    
        Do
            DoEvents
            hWndDispenseBtn = FindWindowEx(hWndDispenseWindow, 0&, "Button", "&Yes")
        Loop Until hWndDispenseBtn <> 0
    
        Do
            DoEvents
            PostMessage hWndDispenseBtn, BM_CLICK, 0, ByVal 0&
        Loop Until IsWindow(hWndDispenseWindow) = 0
    
    End Sub
    Last edited by koushi; Jan 14th, 2013 at 05:32 PM. Reason: Inserting Code snippet

  2. #2
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: Using FindWindow to access External Windows, need help with my process

    i dont like reading to much ,,,,,,,, 1 way i do search window by there caption

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Re: Using FindWindow to access External Windows, need help with my process

    Quote Originally Posted by ladoo View Post
    i dont like reading to much ,,,,,,,, 1 way i do search window by there caption
    I simplified my post, and I already am using FindWindow to get the window handles. Thanks for trying to help
    Last edited by koushi; Jan 15th, 2013 at 03:34 AM.

  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Using FindWindow to access External Windows, need help with my process

    Try this:

    Code:
    Private Sub cmdYes_Click()
        Dim bClicked                As Boolean
        Dim IsPriceWindowClosed     As Boolean
        Dim hWndDispenseBtn         As Long
        Dim hWndDispenseWindow      As Long
        Dim hWndOkayBtn             As Long
        Dim hWndPriceBtn            As Long
        Dim hWndPriceWindow         As Long
        Dim frmSettings_txtS3       As String
        Dim frmSettings_chkWildcard As CheckBoxConstants
    
        frmSettings_chkWildcard = frmSettings.chkWildcard   'Properties are slow to access
        frmSettings_txtS3 = frmSettings.txtS3
    
        While DoEvents
            If hWndPriceWindow Then
                If hWndPriceBtn Then
                    If IsPriceWindowClosed Then
                        If hWndOkayBtn Then
                            If hWndDispenseWindow Then
                                If hWndDispenseBtn Then
                                    If IsWindow(hWndDispenseWindow) Then
                                        PostMessage hWndDispenseBtn, BM_CLICK, 0&, ByVal 0&
                                    Else
                                        Exit Sub
                                    End If
                                Else
                                    hWndDispenseBtn = FindWindowEx(hWndDispenseWindow, 0&, "Button", "&Yes")
                                End If
                            Else
                                If Not bClicked Then
                                    PostMessage hWndOkayBtn, BM_CLICK, 0&, ByVal 0&
                                    bClicked = True
                                End If
                                hWndDispenseWindow = FindWindow(vbNullString, "Dispense")
                            End If                  'Find second popup window
                        Else
                            hWndOkayBtn = FindWindowEx(hWndParent, 0&, "Button", "Okay")
                        End If
                    Else
                        PostMessage hWndPriceBtn, BM_CLICK, 0&, ByVal 0&
                        IsPriceWindowClosed = (IsWindow(hWndPriceWindow) = 0&)
                    End If
                Else
                    hWndPriceBtn = FindWindowEx(hWndPriceWindow, 0&, "Button", "&Yes")
                End If
            Else
                If frmSettings_chkWildcard = vbUnchecked Then
                    hWndPriceWindow = FindWindow(vbNullString, frmSettings_txtS3)
                Else                 'Find first popup window
                    hWndPriceWindow = FindWindowWild(frmSettings_txtS3)
                End If
            End If
        Wend
    End Sub
    Last edited by Bonnie West; Jan 15th, 2013 at 03:59 AM. Reason: Swapped Do..Loop for the faster While..Wend
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Re: Using FindWindow to access External Windows, need help with my process

    Quote Originally Posted by Bonnie West View Post
    Try this:
    Wow, Bonnie West thank you so much! That tidies it up a lot.

    Just say if during one of those expected windows shown in the code, another window pops up that it isn't expecting, what would be the best way about closing it? Should I just have an additional timer somewhere searching for the "unwanted" popup windows and send a WM_CLOSE message as soon as they appear? These additional windows don't need to have any buttons pressed as they are just Information msgboxes and can be closed without any additional actions. Thanks again!!

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Using FindWindow to access External Windows, need help with my process

    Well, you could include another window search code inside the loop, just before the Wend or Loop keywords, and see if the messagebox belongs to the target process. BTW, you should specify the classname of the window you're interested in to make the search more specific. You can obtain the classname through any Window Spy utility.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Re: Using FindWindow to access External Windows, need help with my process

    Thanks very much, I'll fiddle with what you've given me until it's running smoothly, thanks so much!

    And of course I'll update those class names, they are "#32770" although I didn't have them initially as I was writing the program from home and testing it at work so I wanted to minimalise mistakes so I didn't have to keep running back and forth thread resolved thanks again!
    Last edited by koushi; Jan 15th, 2013 at 06:44 PM. Reason: Spelling mistakes

Tags for this Thread

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