Vb.net 检查特定窗口名是否打开

Vb.net 检查特定窗口名是否打开,vb.net,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, "

我试图通过单击循环中的按钮来检查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

下面是我用来捕捉窗口打开并更改其中文本字段的代码

首先循环到方法
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

您正在将
FindWindow()
的返回值分配给
字符串
,然后检查一个数字:
WinHwnd=0
。循环不是必需的。设置Option Strict On。设置循环时,您有什么想法?知道这一点可能会帮助我理解我可以如何提供帮助。循环只是我程序的一部分,我正在等待VeraCrypt打开,以便在它基本上出现后我可以进行其他操作。在此之前,我使用过延迟函数,但它没有按照我希望的方式工作。使用设置为
WindowPattern
模式的自动化事件处理程序。每次打开新窗口(任何窗口)时都会引发此事件。此处的示例代码:。你只需要在那里设置自动化处理程序。我的理论是,你会得到误报,因为你在WinHwnd中有一个不可预见的结果。这个不可预见的结果可能非常简单,比如WinHwnd等于零。在上使用
选项可以帮助您避免这种情况。理解误报的最佳方法是在调试模式下查看WinHwnd,并相应地修改If语句。