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

时间:2011-12-30 23:22:47

标签: c# winforms

我尝试使用findwindowprocess,但它不起作用,我该如何找到特定按钮?

例如,我有按钮类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/////////////
}

2 个答案:

答案 0 :(得分:10)

我相信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);

    }
}

答案 1 :(得分:0)

Autoit提供了一种与Windows交互的好方法。

安装名为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");
        }  
    }
}