简体   繁体   English

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

[英]How to read another windows from a different program

I tried it with findwindow and process but it didn't work, how can I find a specific button ? 我尝试使用findwindowprocess但它不起作用,我怎么能找到一个特定的按钮?

For example I have the button class AfxWnd90u and the instance 21 . 例如,我有按钮类AfxWnd90uinstance 21 I want to check if this button is visible. 我想检查这个按钮是否可见。 I tried it with this code, but I couldn't find the button. 我尝试使用此代码,但我找不到按钮。 I think I made a mistake with the instance. 我想我对这个实例犯了一个错误。

Between I didn't use findwindow here because I experimented a little bit. 我之间没有使用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/////////////
}

I believe a combination of FindWindow and SendMessage Windows API functions will give you want you want. 我相信FindWindow和SendMessage Windows API函数的组合将为您提供所需的功能。 The tricky part will be discovering the window class names, but something like WinSpy++ could help you there. 棘手的部分将是发现窗口类名称,但像WinSpy ++这样的东西可以帮助你。

Here's a sample of how to use the API. 以下是如何使用API​​的示例。 Open Notepad.exe a few times, type in some text and then run this sample. 打开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);

    }
}

Autoit provides a great way to interact with Windows. Autoit提供了一种与Windows交互的好方法。

Install the nuget package called AutoItX.Dotnet 安装名为AutoItX.Dotnet的nuget包

using AutoIt;

class Program
{
    static void Main(string[] args)
    {
        var buttonVisible = AutoItX.ControlCommand("Untitled - Notepad", "", "[CLASSNN:Edit1]", "IsVisible", "");

        //in your case it would be:
        //var buttonVisible = AutoItX.ControlCommand("Put the application title here", "", "[CLASSNN:AfxWnd90u21]", "IsVisible", "");

        if (buttonVisible == "1")
        {
            Console.WriteLine("Visible");
        } else
        {
            Console.WriteLine("Not visible");
        }  
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Windows 7 上的 C# 程序读取 Android 手机上的文件? - How to read files on Android phone from C# program on Windows 7? 读取另一个程序的标准输出 - Read std output from another program 让程序读取一个ini文件以选择和激活不同的窗口 - Have the program read an ini file to select and activate different windows 如何从Windows.UI.Xaml.Controls.TextBox中的不同线程中读取文本-Windows Phone 8.1 - How to read text from Windows.UI.Xaml.Controls.TextBox in different thread - Windows Phone 8.1 如何将任何密钥从我的程序发送到另一个-在Windows Mobile中? - how to send any key from my program to another - in windows-mobile? 如何保持从主程序重新启动Windows窗体,直到另一个窗口窗体退出应用程序? - How to keep re-launching a windows form from the main program until another window form exits the application? Windows服务:从具有不同设置的不同服务器读取 - windows service: Read from different servers with different settings 阻止程序在另一个程序运行时启动(Windows) - preventing a program from starting when another prog is running (Windows) 如何从键盘读取“Enter”以退出程序 - How to read "Enter" from the keyboard to exit program 如何从C#读取Gradle程序 - How to read a Gradle program from C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM