如何从不同的程序读取另一个窗口

2024-02-26

我尝试过findwindow and process但它不起作用,我怎样才能找到特定的按钮?

例如我有按钮类AfxWnd90uinstance 21。我想检查这个按钮是否可见。我用这段代码尝试过,但找不到按钮。我想我在这个例子上犯了一个错误。

之间我没有使用findwindow在这里因为我做了一点实验。

//////IMPORTANT/////////////
System.Diagnostics.Process[] move = System.Diagnostics.Process.GetProcessesByName("PartyGaming");
ArrayList save = new ArrayList();
RECT rct = new RECT();
listBox1.Items.Add(move.Length);
List<System.Diagnostics.Process> process = new List<System.Diagnostics.Process>();

// use only the process with the button AfxWnd90u21
for (int i = 0; i < move.Length;++i ) 
{
    IntPtr hCheck = FindWindowEx(move[i].MainWindowHandle, IntPtr.Zero, "AfxWnd90u21", null);
    //if button is visible
    if (hCheck != IntPtr.Zero)
        process.Add(move[i]);

    //////IMPORTANT/////////////
}

我相信FindWindow和SendMessage Windows API函数的结合会给你带来你想要的。棘手的部分是发现窗口类名称,但像 WinSpy++ 这样的东西可以帮助你。

以下是如何使用 API 的示例。打开 Notepad.exe 几次,输入一些文本,然后运行此示例。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<WinText> windows = new List<WinText>();

            //find the "first" window
            IntPtr hWnd = FindWindow("notepad", null);

            while (hWnd != IntPtr.Zero)
            {
                //find the control window that has the text
                IntPtr hEdit = FindWindowEx(hWnd, IntPtr.Zero, "edit", null);

                //initialize the buffer.  using a StringBuilder here
                System.Text.StringBuilder sb = new System.Text.StringBuilder(255);  // or length from call with GETTEXTLENGTH

                //get the text from the child control
                int RetVal = SendMessage(hEdit, WM_GETTEXT, sb.Capacity, sb);

                windows.Add(new WinText() { hWnd = hWnd, Text = sb.ToString() });

                //find the next window
                hWnd = FindWindowEx(IntPtr.Zero, hWnd, "notepad", null);
            }

            //do something clever
            windows.OrderBy(x => x.Text).ToList().ForEach(y => Console.Write("{0} = {1}\n", y.hWnd, y.Text));

            Console.Write("\n\nFound {0} window(s).", windows.Count);
            Console.ReadKey();
        }

        private struct WinText
        {
            public IntPtr hWnd;
            public string Text;
        }

        const int WM_GETTEXT = 0x0D;
        const int WM_GETTEXTLENGTH = 0x0E;

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int Param, System.Text.StringBuilder text);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从不同的程序读取另一个窗口 的相关文章

  • 如何在运行 ASP.NET 页面时以编程方式设置表格背景?

    我有一个 aspx 页面 其默认背景颜色是一种 当选择单选按钮的某个选项时 我需要能够以编程方式更改它 我尝试设置表的 ID 字段 但似乎无法在 C 代码隐藏文件中访问它 我原来的表是 table style width 100 backg
  • 实体框架 - sql server 表中未设置默认值

    SQL Server 2005 数据库表有一列 createdon 其默认值设置为 getdate 我正在尝试使用实体框架添加记录 createdon 列未更新 我是否错过了实体框架中的任何属性 请提出建议 这是实体框架存在的少数问题之一
  • 查找周边上的点来表示边界/形状

    我有一个简单的二维网格 其格式为myGrid x y 我正在尝试找到一种方法来找到所选网格周围的周长 这样我就有了所选网格的形状 这是我的意思的一个例子 这里的想法是找到所有相关的 角点 也就是图像周边的红点 放入一个列表中 这样我就可以从
  • 在 IEnumerable 中查找相同的集合

    有一项任务要弄清楚如何更新表 DataTable 连接到一个database没有UPDATE陈述 我想出的例子是从邮箱中读取警报 该表将写入 Alerts 如果邮件正文包含单词 SUCCESS gt Alert 变为绿色 如果 FAIL g
  • 在 std::forward 中它如何接受右值?

    看看斯科特 迈耶的有效的现代 C 第200 201页 建议的简化实施std forward可能是 在其他地方看到了正确的实现 template
  • ctypes.ArgumentError:不知道如何转换参数

    我在C库中定义了一个函数 如下所示 int Test char str1 int id1 char str2 float val float ls 我想在Python中使用它 所以我编写了以下Python代码 str1 a str2 b i
  • C# - 如何从 Steam 交易 API 获取图标的 URL(编码)

    以下是 API 为每个项目返回的 XML 部分
  • 带有 Prism 区域适配器的 AvalonDock

    我看到了一些关于 SO 的问题 但似乎没有一个适合我 我希望能够使用伟大的使用 Prism 4 但是 所有示例区域适配器均适用于 Avalondock 1 x 系列 我无法使其工作 有人有关于如何为 AvalonDock 的 LayoutD
  • 变形:Opencv 使用 Visual Studio 将图像显示到曲面屏幕

    我正在尝试使用 opencv API 来扭曲图像 以便将其显示到曲面屏幕上 我已经浏览了opencv中提供的翘曲apihere http docs opencv org 2 4 modules stitching doc warpers h
  • 使用 openssl 库获取 x509 证书哈希

    我目前正在开发一个应用程序 它使用 openssl 库 libcrypto 来生成证书 现在我必须获取现有证书的哈希值 当我使用终端时 我可以使用以下命令生成哈希值 openssl x509 hash in cert pem noout 输
  • 等效

    这是否保证始终为真 std numeric limits
  • ASP.NET Core中间件如何进行DI?

    我正在尝试将依赖项注入到我的中间件构造函数中 如下所示 public class CreateCompanyMiddleware private readonly RequestDelegate next private readonly
  • BlueZ D-Bus C,应用 BLE

    我正在尝试编写一个应用程序来搜索附近的蓝牙设备并与它们通信 我的应用程序将用 C 语言编写 并打算在 Linux 下工作 是否有通过 C 中的 D Bus 使用 BlueZ 的教程或示例 此应用程序的目的是从 BLE 中的文件发送数据 你能
  • 对嵌套属性使用 XmlAttributeOverrides

    我试图使用 XmlAttributeOverrides 来控制类序列化后哪些类属性出现在 xml 中 它适用于 根 类上的属性 但不适用于嵌套属性 这是一个简单的例子来说明我想要完成的任务 我的类层次结构如下 public class Ma
  • 使用 c++20 范围删除最后一个元素的最佳方法是什么

    有没有比反转两次更好的方法来使用 c 20 范围删除容器中的最后一个元素 include
  • 为什么 istream/ostream 慢

    于 50 40http channel9 msdn com Events GoingNative 2013 Writing Quick Code in Cpp Quickly http channel9 msdn com Events Go
  • 如何设置扬声器声音增强设置

    如何以编程方式设置 Windows 扬声器设置 增强 选项卡 中可用的声音效果 恐怕这是不可能的 参见 Maurits 对他的评论blog http blogs msdn com b matthew van eerde archive 20
  • 以编程方式连接和断开 USB,“无需拔出和重新插入”

    我需要以编程方式连接和断开 USB 也就是说 我已经插入了USB设备 我需要使用 C NET 应用程序传输文件 该应用程序将监视特定文件夹并将文件从该文件夹传输到 USB 驱动器 我需要在文件传输后断开 USB 设备的连接 并在需要时连接
  • { Qt5.0.2/QML/QtQuick2.0/C++ } 运行没有错误的示例项目? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我的设置是Qt5 0 2 MinGW 32位 我在寻找 Qt5 0 2 QML QtQuick2 0 C 代码项目示例 不是Qt Qu
  • In 和 Out 属性在 .NET 中如何工作?

    我一直在尝试跨序列化数组AppDomain边界 使用以下代码 public int Read byte buffer int offset int count return base Read buffer offset count 作为猜

随机推荐