2016-11-15 10 views
5

Tôi đã có một ứng dụng cơ bản trực quan cần tìm Microsoft Access có hộp thông báo và sau đó Send Enter to the message box.vb.net tìm hộp thư trong một ứng dụng khác bằng FindWindowex

Tôi đã theo dõi bài đăng này (FindWindow FindWindowEx).

Nó tìm Access và mang nó vào foreground nhưng nó không muốn tìm ra hộp thông báo và mang nó vào phía trước:

enter image description here

Public Class Form1 

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Long 

Private Declare Auto Function FindWindow Lib "user32.dll" (_ 
ByVal lpClassName As String, _ 
ByVal lpWindowName As String _ 
) As IntPtr 

Private Declare Auto Function FindWindowEx Lib "user32.dll" (_ 
ByVal hwndParent As IntPtr, _ 
ByVal hwndChildAfter As IntPtr, _ 
ByVal lpszClass As String, _ 
ByVal lpszWindow As String _ 
) As IntPtr 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Dim hWnd As IntPtr 
    hWnd = FindWindow("OMain", Nothing) 

    MsgBox(hWnd) 'FINDS 1640402 

    Dim hWndChild1 As IntPtr = _ 
    FindWindowEx(hWnd, IntPtr.Zero, "#32770 (Dialog)", "Microsoft Access") 

    MsgBox(hWndChild1) 'FIRST PROBLEM IT FINDS ZERO HERE 

    Dim hWndChild1Button As IntPtr = _ 
    FindWindowEx(hWndChild1, IntPtr.Zero, "Button", "OK") 

    MsgBox(hWndChild1Button) 'ALSO FINDS ZERO HERE 

    If hWndChild1Button <> IntPtr.Zero Then 
    SetForegroundWindow(hWndChild1Button) 
    SendKeys.SendWait("{Enter}") 
    End If 

End Sub 
End Class 

enter image description here enter image description here

enter image description here enter image description here

Trả lời

6

Mã không sử dụng đúng chức năng winapi. FindWindowEx() có thể tìm thấy các cửa sổ con, nhưng cửa sổ được hiển thị bởi MsgBox() không phải là một cửa sổ con. Nó là một cửa sổ cấp cao nhất, loại mà bạn có thể tìm thấy với FindWindow().

Nhưng chức năng đó không đủ tốt để tìm hộp thư cụ thể bạn muốn đóng. Một cách tiếp cận tốt hơn là cần thiết, một trong những bạn có thể sử dụng là liệt kê các cửa sổ được sở hữu bởi cùng một luồng với EnumThreadWindows(). Điều tốt đẹp về MsgBox() là sẽ chỉ có một cửa sổ như vậy kể từ khi hộp thoại là phương thức.

SendKeys() cũng không đủ tốt, nó chỉ hoạt động bình thường nếu hộp thông báo ở phía trước. Một cách tiếp cận tốt hơn là thực sự nhấp vào nút bằng cách gửi cho nó thông báo BM_CLICK. Mã thử nghiệm, sử dụng biểu mẫu Truy cập:

Imports System.Runtime.InteropServices 
Imports System.ComponentModel 
Imports System.Text 

Module Module1 
    Sub Main() 
     '' Find the MS-Access host window 
     Dim access = FindWindow("OMain", Nothing) 
     If access = IntPtr.Zero Then Throw New Win32Exception() 
     '' Enumerate the windows owned by the same thread 
     Dim pid As Integer 
     Dim tid = GetWindowThreadProcessId(access, pid) 
     If tid = 0 Then Throw New Win32Exception() 
     EnumThreadWindows(tid, AddressOf ClickOkButton, Nothing) 
    End Sub 

    Private Function ClickOkButton(hWnd As IntPtr, lp As IntPtr) As Boolean 
     '' Verify the class name is #32770 
     Dim buf As New StringBuilder(256) 
     GetClassName(hWnd, buf, 256) 
     If buf.ToString <> "#32770" Then Return True 
     '' Find the OK button (control ID 2) 
     Dim okbutton = GetDlgItem(hWnd, 2) 
     If okbutton = IntPtr.Zero Then Return True 
     '' Activate the dialog, just in case 
     SetActiveWindow(hWnd) 
     '' Click the button 
     SendMessage(okbutton, BM_CLICK, IntPtr.Zero, IntPtr.Zero) 
     '' Done, no need to continue enumerating windows 
     Return False 
    End Function 
End Module 

Friend Module NativeMethods 
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> 
    Friend Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr 
    End Function 

    <DllImport("user32.dll", SetLastError:=True)> 
    Friend Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer 
    End Function 

    Friend Delegate Function EnumThreadDelegate(hWnd As IntPtr, lParam As IntPtr) As Boolean 

    <DllImport("user32.dll", SetLastError:=True)> 
    Friend Function EnumThreadWindows(dwThreadId As Int32, lpfn As EnumThreadDelegate, lParam As IntPtr) As Boolean 
    End Function 

    <DllImport("user32.dll", CharSet:=CharSet.Auto)> 
    Friend Function GetClassName(ByVal hWnd As System.IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer 
    End Function 

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> 
    Friend Function GetDlgItem(ByVal hDlg As IntPtr, id As Integer) As IntPtr 
    End Function 

    <DllImport("user32.dll", SetLastError:=True)> 
    Friend Function SetActiveWindow(ByVal hWnd As IntPtr) As IntPtr 
    End Function 

    <DllImport("user32.dll")> 
    Friend Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr 
    End Function 

    Friend Const BM_CLICK As Integer = &HF5 
End Module 

Lời khuyên thông thường là favor UI Automation.

+0

Cảm ơn bạn! Tôi sẽ kiểm tra ngay bây giờ và đưa ra phản hồi :) – Wilest

+0

Tôi đang tiết kiệm điều này ... thần biết tại sao. – Jaxedin

Các vấn đề liên quan