检查特定的窗口名称是否打开

时间:2018-10-31 17:39:13

标签: vb.net

我试图通过单击循环内的按钮来检查VB.net中是否打开了特定的窗口名称。

现在的问题是,即使我没有打开,每次我单击按钮时它都说找到了窗口

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim WinHwnd As String
    Dim loopUntil As Integer = 0

    WinHwnd = FindWindow(vbNullString, "VeraCrypt")

    Do Until loopUntil = 1

        If WinHwnd = 0 Then ' Window is close
            MsgBox("Not found", MsgBoxStyle.OkOnly, "not found")
        Else
            ' Window is found
            MsgBox(" found", MsgBoxStyle.OkOnly, "found")

            loopUntil = 1
        End If
    Loop
End Sub

1 个答案:

答案 0 :(得分:-1)

这是我用来在打开窗口并更改其中的文本字段时捕获的代码。

首先循环到方法autoFillFileSelection

    Dim timeout As Integer = WAITING_TIMEOUT_MS / 1000
    While autoFillFileSelection(curFile) = False
        Threading.Thread.Sleep(100)

        If timeout = nbLoop Then
            enterFileIntoFormTimeout = True
            Exit While
        End If
        nbLoop += 1
    End While

第二,方法本身:

    Protected Function autoFillFileSelection(ByVal file As String) As Boolean
        ' Find program/dialog handle; 32770 je za dialog
        Dim hWnd As IntPtr
        Dim englishTitle As String = "Choose File to Upload"
        Dim frenchTitle As String = "Choisir un fichier à télécharger"
        Dim frenchTitle2 As String = "Choisir un fichier à charger"
        Dim englishButton As String = "&Open"
        Dim frenchButton As String = "&Ouvrir"
        Dim frenchButton2 As String = "Ou&vrir"

        Dim button As String = englishButton

        Dim windowClass As String = Nothing ' "#32770"
        hWnd = FindWindow(windowClass, englishTitle)
        If hWnd = 0 Then
            hWnd = FindWindow(windowClass, frenchTitle)
            If hWnd = 0 Then hWnd = FindWindow(windowClass, frenchTitle2)
            button = frenchButton
        End If

        If hWnd = 0 Then
            Return False
        End If

        'Ensure window has focus, outerwise, would wait for user to click on window (and potentially choosing another file, NO GOOD !)
        'CType(Me.ParentForm, Object).focus()
        selectWindow() ' --> Surelly useless for you as my window what coming from my software, but an IE component
        SetFocus(hWnd)

        'Write file into dialog and submit it
        Dim hWndEdit As IntPtr
        hWndEdit = apiFindWindowEx(hWnd, IntPtr.Zero, "ComboBoxEx32", "")
        Dim hWndEdit1 As IntPtr
        hWndEdit1 = apiFindWindowEx(hWndEdit, IntPtr.Zero, "ComboBox", "")
        Dim hWndEdit2 As IntPtr
        hWndEdit2 = apiFindWindowEx(hWndEdit1, IntPtr.Zero, "Edit", "")
        Dim sb As New System.Text.StringBuilder(file)
        Dim WM_SETTEXT As Integer = 12 '&HC 'decimalno 12
        SendMessage(hWndEdit2, WM_SETTEXT, 0, sb)
        Dim hWndTextbox As IntPtr = apiFindWindowEx(hWnd, IntPtr.Zero, "Button", button)
        If hWndTextbox = 0 Then hWndTextbox = apiFindWindowEx(hWnd, IntPtr.Zero, "Button", frenchButton2)

        Dim BN_CLICKED As Integer = 245
        SendMessage(hWndTextbox, BN_CLICKED, 0, IntPtr.Zero)

        Return True
    End Function