如何使用VB.Net获取弹出消息框中包含的控件的属性

如何使用VB.Net获取弹出消息框中包含的控件的属性,vb.net,popup,window,Vb.net,Popup,Window,我正在从事一个VB.Net项目,其中一部分内容是在显示时捕获一个弹出消息框,并以某种方式对其进行处理。 我的问题是,我必须知道这个弹出窗口中包括哪些按钮(主要是它们的标题)。 这可能吗?有人能告诉我怎么做吗?请提供样品 谢谢 更新: 因为我投了反对票,Ed Cottrell告诉我这是因为没有添加任何与我的问题相关的代码。现在我有了问题的答案,所以我添加了一些代码: 我的应用程序捕捉到另一个windows应用程序显示的弹出窗口,我使用EnumWindowsAPI了解何时显示新的弹出窗口 Publi

我正在从事一个
VB.Net
项目,其中一部分内容是在显示时捕获一个弹出消息框,并以某种方式对其进行处理。 我的问题是,我必须知道这个弹出窗口中包括哪些按钮(主要是它们的标题)。 这可能吗?有人能告诉我怎么做吗?请提供样品

谢谢

更新: 因为我投了反对票,Ed Cottrell告诉我这是因为没有添加任何与我的问题相关的代码。现在我有了问题的答案,所以我添加了一些代码:

我的应用程序捕捉到另一个windows应用程序显示的弹出窗口,我使用
EnumWindows
API了解何时显示新的弹出窗口

Public Declare Function EnumWindows Lib "User32.dll" (ByVal WNDENUMPROC As EnumWindowDelegate, ByVal lparam As IntPtr) As Boolean
Delegate Function EnumWindowDelegate(ByVal hWnd As IntPtr, ByVal Lparam As IntPtr) As Boolean
捕获此窗口时,我使用从EnumWindows结果中获取的句柄,并使用
EnumChildWindows
获取其子窗口(这是我正在寻找的控件,因为控件也是一种窗口):

我使用的API

    <DllImport("User32.dll")> _
    Public Function EnumChildWindows _
        (ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, _
        ByVal lParam As IntPtr) As Boolean
    End Function

    Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean

' Get window text length signature.
Public Declare Function SendMessage _
 Lib "user32" Alias "SendMessageA" _
 (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

' Get window text signature.
Public Declare Function SendMessage _
 Lib "user32" Alias "SendMessageA" _
 (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As StringBuilder) As Int32
功能

Public Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
    Dim ChildrenList As New List(Of IntPtr)
    Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
    Try
        EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
    Finally
        If ListHandle.IsAllocated Then ListHandle.Free()
    End Try
    Return ChildrenList.ToArray
End Function

Public Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
    Dim ChildrenList As List(Of IntPtr) = GCHandle.FromIntPtr(Parameter).Target
    If ChildrenList Is Nothing Then Throw New Exception("GCHandle Target could not be cast as List(Of IntPtr)")
    ChildrenList.Add(Handle)
    Return True
End Function
现在,当我有了弹出窗口的句柄(parentHandle)时,我可以得到它的子窗口:

Dim hndls() As IntPtr = GetChildWindows(parentHandle)
Dim window As ApiWindow

For Each hnd In hndls
    window = GetWindowIdentification(hnd)
    'Add Code Here 
Next
其中GetWindowIdentification是:

''' <summary>
''' Build the ApiWindow object to hold information about the Window object.
''' </summary>
Public Function GetWindowIdentification(ByVal hwnd As Integer) As ApiWindow

    Const WM_GETTEXT As Int32 = &HD
    Const WM_GETTEXTLENGTH As Int32 = &HE

    Dim window As New ApiWindow()

    Dim title As New StringBuilder()

    ' Get the size of the string required to hold the window title.
    Dim size As Int32 = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0)

    ' If the return is 0, there is no title.
    If size > 0 Then
        title = New StringBuilder(size + 1)

        SendMessage(hwnd, WM_GETTEXT, title.Capacity, title)
    End If

    ' Set the properties for the ApiWindow object.
    window.MainWindowTitle = title.ToString()
    window.hWnd = hwnd

    Return window

End Function
“”
''构建ApiWindow对象以保存有关窗口对象的信息。
''' 
公共函数GetWindowIdentification(ByVal hwnd作为整数)作为ApiWindow
常量WM_GETTEXT为Int32=&HD
常量WM_GETTEXTLENGTH为Int32=&HE
将窗口变暗为新窗口()
将标题设置为新的StringBuilder()
'获取保存窗口标题所需的字符串大小。
Dim大小为Int32=SendMessage(hwnd,WM_GETTEXTLENGTH,0,0)
'如果返回值为0,则没有标题。
如果大小>0,则
标题=新的StringBuilder(尺寸+1)
SendMessage(hwnd,WM_GETTEXT,title.Capacity,title)
如果结束
'设置ApiWindow对象的属性。
window.MainWindowTitle=title.ToString()
window.hWnd=hWnd
返回窗口
端函数

我在另一个网站的网上找到了这个答案,我想我应该分享一下,以防将来有人看到这个问题:

按钮只是更多的窗口。你只是想找更多的孩子 找到的消息框窗口的窗口。你不能就这样 从窗口中删除句柄,然后尝试将其强制转换为窗体对象,然后 希望所有属性都能正常工作。它就是不能那样工作


注:有人能回答我为什么在这个问题上投了反对票吗??这真的让我很不安://

否决票(不是我)可能是因为你没有发布任何代码或显示你尝试过的内容,这通常是不受欢迎的。啊,好吧,我不知道。只是我认为不需要添加任何代码,因为我在问一个关于查找表单控件的方法的一般性问题,所以我没有任何代码可以发布。谢谢你澄清,在我添加了关于我的问题的代码和我使用的解决方案代码后,我再次投了反对票,这真的不公平:@Have-up-vote来弥补它。:)我真的认为这是一个好问题。非常感谢埃德:我真的不明白为什么这次的反对票是赞成的!
''' <summary>
''' Build the ApiWindow object to hold information about the Window object.
''' </summary>
Public Function GetWindowIdentification(ByVal hwnd As Integer) As ApiWindow

    Const WM_GETTEXT As Int32 = &HD
    Const WM_GETTEXTLENGTH As Int32 = &HE

    Dim window As New ApiWindow()

    Dim title As New StringBuilder()

    ' Get the size of the string required to hold the window title.
    Dim size As Int32 = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0)

    ' If the return is 0, there is no title.
    If size > 0 Then
        title = New StringBuilder(size + 1)

        SendMessage(hwnd, WM_GETTEXT, title.Capacity, title)
    End If

    ' Set the properties for the ApiWindow object.
    window.MainWindowTitle = title.ToString()
    window.hWnd = hwnd

    Return window

End Function