gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:16:10 25 4
gpt4 key购买 nike

我用 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/////////////
}

最佳答案

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

}
}

关于c# - 如何从不同的程序读取另一个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8685002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com