如何使用 VB.Net 获取弹出消息框中包含的控件的属性 [英] How can I get properties of controls contained in a popup message box using VB.Net

查看:23
本文介绍了如何使用 VB.Net 获取弹出消息框中包含的控件的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 VB.Net 项目,其中一部分我会在它显示时捕获一个弹出消息框并以某种方式处理它.我的问题是我必须知道这个弹出窗口中包含哪些按钮(主要是它们的标题).这可能吗?有人可以告诉我如何做到这一点吗?样品将不胜感激.

I'm working on a VB.Net project where in part of it I catch a pop up message box when it's displayed and handle it in some way. My problem is that I have to know what buttons are included in this pop up window (their captions mainly). Is this possible? Can someone please tell me how this can be done? A sample would be much appreciated.

谢谢.

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

Update: Since I got a down vote, and Ed Cottrell told me that this is because of not adding any code related to my question. Now I have the answer to my question so I'm adding some code:

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

My app catches a pop-up window displayed by another windows application, I used EnumWindows API to know when a new pop-up window is displayed.

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 获取它的子窗口(这是我正在寻找的控件,因为控件是一种窗口太):

When I catch this window, I use its handle that I got from EnumWindows result and get its child windows using EnumChildWindows (which are the controls I'm looking for , since controls are kind of windows too):

我使用的 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

结构 API 窗口

Public Structure ApiWindow
    Public MainWindowTitle As String
    Public ClassName As String
    Public hWnd As Int32
End Structure

功能

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)时,我可以得到它的子窗口:

Now when I have the handle of pop-up window (parentHandle), I can get its children windows:

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

For Each hnd In hndls
    window = GetWindowIdentification(hnd)
    'Add Code Here 
Next

GetWindowIdentification 在哪里:

Where GetWindowIdentification is:

''' <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

推荐答案

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

I found this answer on the net on another website, I thought I should share it in case someone sees this question in the future:

按钮不过是更多的窗口.你只是寻找更多的孩子您找到的消息框窗口的窗口.你不能只是得到窗口中的句柄,然后尝试将其转换为 Form 对象和期望所有属性都能正常工作.就是这样不行.

Buttons are nothing but more windows. You just look for more child windows of the message box window you found. You can NOT just get the handle from a window and then try to cast it to a Form object and expect all the properties to work. It just doesn't work that way.

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

Note: Can some please answer me why I got a down vote on this question?? This really upsets me :/

这篇关于如何使用 VB.Net 获取弹出消息框中包含的控件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆