在 C# 中抓取 Windows 应用程序的屏幕

发布于 2024-07-10 16:09:46 字数 1560 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日裸衫吸 2024-07-17 16:09:47

您可能需要查看WM_GETTEXT消息。 这可以用于从其他窗口读取文本 - 这是一个过时的Windows API 的一部分,如果您使用 C#,则需要对其进行 p/invoke。

请查看此页面,了解在 C# 中执行此操作的示例。

基本上,您首先使用 FindControlEx() 来获取所需窗口的句柄(通过标题)。

其次,使用 EnumChildWindows() 递归枚举该窗口上的控件,以查找该窗口的所有子控件以及所有这些子控件的子控件,直到获得目标窗体的完整映射。

以下是 Google 问答中 Theta-ga 的精彩解释的节选部分:

要获取任何文本框或列表框控件的内容,我们所需要的只是它的窗口句柄。 如果您已经获取了窗口句柄,请转到说明的第 2 部分。

第 1 部分:获取控制句柄

  • 获取对于一个控件,我们首先获取它的父窗口的句柄。 我们可以使用 Win32 FindControlEx() 方法来做到这一点。 此方法接受窗口标题(例如“计算器”)和/或其类名,并返回其句柄。
  • 一旦我们获得了父窗口句柄,我们就可以调用 Win32 EnumChildWindows 方法。 此方法采用一个回调方法,它使用为指定父级找到的每个子控件的句柄来调用该回调方法。 例如,如果我们使用计算器窗口的句柄调用此方法,它将使用文本框控件的句柄调用回调方法,然后再次使用计算器窗口上每个按钮的句柄调用回调方法,依此类推.
  • 由于我们只对文本框控件的句柄感兴趣,因此我们可以在回调方法中检查窗口的类。 Win32 方法 GetClassName() 可用于此目的。 该方法接受一个窗口句柄,并为我们提供一个包含类名的字符串。 所以文本框属于?编辑? 类、“ListBox”类的列表框等等。 一旦确定您拥有正确控件的句柄,就可以读取其内容。

第 2 部分:读取控件的内容

  • 您可以使用 Win32 SendMessage() 函数读入控件的内容,并使用它将 WM_GETTEXT 消息传递给目标控件。 这将为您提供控件的文本内容。 此方法适用于文本框、按钮或静态控件。
  • 但是,如果您尝试读取列表框的内容,上述方法将会失败。 要获取列表框的内容,我们需要首先使用 SendMessage() 和 LB_GETCOUNT 消息来获取列表项的计数。 然后我们需要为列表中的每个项目使用 LB_GETTEXT 消息调用 SendMessage()。

You may want to look into the WM_GETTEXT message. This can be used to read text from other windows -- it's an archaic part of the Windows API, and if you're in C#, you'll need to p/invoke for it.

Check out this page for an example of doing this in C#.

Basically, you first FindControlEx() to get the handle of the window that you want (by caption).

Second, you recursively enumerate the controls on that window with EnumChildWindows() to find all of the window's child controls, and all of those children's children until you have a complete map of the target form.

Here is a selected portion of Theta-ga's excellent explanation from Google Answers:

To get the contents of any textbox or listbox control, all we need is it's window handle. If you have already obtained the window handle then move to part 2 of the explaination.

PART 1: Obtaining the control handle

  • To obtain the handle of a control, we first obtain the handle of it?s parent window. We can do this by using the Win32 FindControlEx() method. This method takes in the window caption (such as 'Calculator') and/or its class name, and return its handle.
  • Once we have the parent window handle, we can call the Win32 EnumChildWindows method. This method takes in a callback method, which it calls with the handle of every child control it finds for the specified parent. For eg., if we call this method with the handle of the Calculator window, it will call the callback method with the handle of the textbox control, and then again with the handles of each of the buttons on the Calculator window, and so on.
  • Since we are only interested in the handle of the textbox control, we can check the class of the window in the callback method. The Win32 method GetClassName() can be used for this. This method takes in a window handle and provides us with a string containing the class name. So a textbox belongs to the ?Edit? class, a listbox to the 'ListBox' class and so on. Once you have determined that you have the handle for the right control, you can read its contents.

PART 2: Reading the contents of a control

  • You can read in the contents of a control by using the Win32 SendMessage() function, and using it to pass the WM_GETTEXT message to the target control. This will give you the text content of the control. This method will work for a textbox, button, or static control.
  • However, the above approach will fail if you try to read the contents of a listbox. To get the contents of a listbox, we need to first use SendMessage() with the LB_GETCOUNT message to get the count of list items. Then we need to call SendMessage() with the LB_GETTEXT message for each item in the list.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文