【问题标题】:How to read a Message Box content text using the "user32.dll" functions如何使用“user32.dll”函数读取消息框内容文本
【发布时间】:2012-04-28 17:19:07
【问题描述】:

我需要一个简单的 c#/vb.net 中的 win api,从消息框中读取文本。我有一个阅读消息框标题的功能,但我不知道如何获取内容文本。 消息框标题功能为:

' Function to retrieve the popup window associated with the form, as well as to
' find the child windows of the popup...
Private Declare Auto Function GetWindow Lib "user32.dll" ( _
    ByVal hWnd As IntPtr, ByVal uCmd As Long) As IntPtr

' Sendmessage overload that is used to send messages to the button on the
' dialog window...
Private Declare Auto Function SendMessage Lib "user32.dll" Alias "SendMessage" _
    (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, _
     ByRef lParam As IntPtr) As IntPtr

' Sendmessage overloads used to retrieve the window text...
Private Declare Auto Function SendMessageA Lib "user32.dll" _
    Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _
        ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr

...

' This function returns the text of the window, used so that we can confirm that
' we have the right dialog window...
Private Function GetWindowText(ByVal WindowHandle As IntPtr) As String
    Dim ptrRet As IntPtr
    Dim ptrLength As IntPtr

    ' Get length for buffer...
    ptrLength = SendMessageA( _
        WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)

    ' Create buffer for return value...
    Dim sb As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)

    ' Get window text...
    ptrRet = SendMessageString( _
        WindowHandle, WM_GETTEXT, ptrLength.ToInt32 + 1, sb)

    ' Get return value...
    Return sb.ToString
End If

【问题讨论】:

  • 为什么不使用自动化界面呢?它为您完成所有 p/invoking。
  • +1 for UI Automation - 这应该让你更“放心”

标签: c# .net vb.net winapi messagebox


【解决方案1】:

您可能使用了错误的窗口句柄。文本由消息框内的客户端窗口显示。您可以通过 pinvoke GetDlgItem() 获取其句柄,传递 ID 65535。使用 Spy++ 深入了解组成消息框窗口的部分。

【讨论】:

  • 谢谢汉森。我用这个手柄做什么?如何获取文本?
  • Pinvoke GetWindowText()。您需要注意不良的 VB6 声明,例如 GetDlgItemText()。在 www.pinvoke.net 上查找 .NET
  • 嗨汉森。感谢您的帮助,但我放弃了……我尝试同时使用“SendMessage”和“GetDlgItemText”来获取字符串生成器,但它也没有用。我不知道如何使用这些功能。我在网上没有看到任何例子。我认为唯一拥有的数据是(可能)消息框窗口的 id。我也不确定了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
  • 2018-11-19
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-30
相关资源
最近更新 更多