gpt4 book ai didi

c# - 如何使用其控件 ID 获取子窗口?

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:38 26 4
gpt4 key购买 nike

我是 WINAPI 的新手,已经知道如何向另一个程序发送消息。我正在使用的程序但是我希望能够让它点击特定的按钮。从我通过查看 Spy++ 窗口了解到的情况来看,每次重新加载时程序的句柄都会发生变化,它们的控件的句柄也会发生变化。控件 ID 保持不变。经过两天的摸索,我来到了这里。

在 SendMesssageA 下,如果我将当前句柄指定为 Spy++ 可见并使用它并运行代码,它工作正常并单击我的外部应用程序上的按钮。我正在尝试使用 GetDlgItem,因为我已经读过我可以使用它获取控件(子窗口)的句柄。但是,我做错了什么,因为无论我做什么,它都会返回 0 或“null”。

如何让 GetDlgItem 返回子控件句柄,以便我可以使用它发送消息以在外部应用程序中单击该控件?

感谢您提前输入的帮助。

    [DllImport("User32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
Process[] myProcess = Process.GetProcessesByName("program name here");

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SendMessageA(IntPtr hwnd, int wMsg, int wParam, uint lParam);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetDlgItem(int hwnd, int childID);

public const int WM_LBUTTONDOWN = 0x0201;
public const int WM_LBUTTONUP = 0x0202;


public void SendClick()
{
IntPtr hwnd = myProcess[0].MainWindowHandle;
SetForegroundWindow(hwnd);

int intCID = 1389;
IntPtr ptrTest = GetDlgItem(hwnd, intCID);
SendKeys.SendWait(" ");
Thread.Sleep(1000);
SendKeys.SendWait("various text to be sent here");
Thread.Sleep(1000);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(1000);

SendMessageA(ptrTest, WM_LBUTTONDOWN, WM_LBUTTONDOWN, 0);
}

最佳答案

我认为您必须使用 Win32 API 来找到“接收”应用程序窗口,然后找到该句柄的子窗口。这是我在谷歌搜索 Win32 API FindWindow 时发现的东西

http://www.c-sharpcorner.com/UploadFile/shrijeetnair/win32api12062005005528AM/win32api.aspx

关于c# - 如何使用其控件 ID 获取子窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3796497/

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