【发布时间】:2010-09-07 16:24:41
【问题描述】:
我正在尝试使用 Win32 API 将一系列用户输入自动化到 C# 中已编译的应用程序中。我没有我试图控制的应用程序的任何源代码,它在我试图控制它时正在运行。在我的代码中,我有一个按钮,当单击该按钮时,需要对我要控制的应用程序进行 3 个输入序列:
- 在树形视图中选择一个项目
- 点击按钮
- 单击另一个按钮
它的工作方式是步骤 2 中的按钮根据步骤 1 中树视图中选择的项目执行操作。我可以通过简单地发送消息来使按钮单击正常工作,但我不知道找出如何选择我想要的 TreeView 项目。 TreeView 是静态的,因此项目和布局永远不会改变。它具有以下布局:
-itemsA
-itemsB
--itemB1
-itemsC
其中 itemB1 是为了使步骤 2 和 3 中的按钮单击起作用而需要选择的项目。默认情况下 ItemsB 是折叠的,所以我可能需要先展开它才能选择 ItemB1?这是我的代码。我真的很感谢任何帮助!
//Find Window API
[DllImport("User32.dll")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
//Find WindowEx API
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
//Send Message API
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
private const int BN_CLICKED = 245;
//Method called by button click
public static void Start()
{
int hwnd = 0;
int prod = 0;
IntPtr hwndChild = IntPtr.Zero;
IntPtr treeChild = IntPtr.Zero;
IntPtr prodChild = IntPtr.Zero;
hwnd = FindWindow(null, "Application");
if (hwnd > 0)
{
//Get Handle for TreeView, THIS IS WHERE I AM STUCK!!
treeChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "AfxMDIFrame80", null);
treeChild = FindWindowEx((IntPtr)treeChild, IntPtr.Zero, "AfxMDIFrame80", null);
treeChild = FindWindowEx((IntPtr)treeChild, IntPtr.Zero, "SysTreeView32", null);
//Need to Add code to select item in TreeView ???
//Click First Button
hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "AfxMDIFrame80", null);
hwndChild = FindWindowEx((IntPtr)hwndChild, IntPtr.Zero, "AfxMDIFrame80", null);
hwndChild = FindWindowEx((IntPtr)hwndChild, IntPtr.Zero, "#32770", null);
IntPtr scanBtn = FindWindowEx((IntPtr)hwndChild, IntPtr.Zero, "Button", "&Scan");
SendMessage((int)scanBtn, BN_CLICKED, 0, IntPtr.Zero);
//Click Second Button
prod = FindWindow("#32770", "Product: WPC");
prodChild = FindWindowEx((IntPtr)prod, IntPtr.Zero, "Button", "&Collect");
SendMessage((int)prodChild, BN_CLICKED, 0, IntPtr.Zero);
}
}//END Start
汉斯,
你能给我举个例子来说明我会怎么做吗?我真正遇到的问题是找到要选择的树视图项的句柄。如果我使用 Spy++ 查找当前句柄并将其硬编码到我的方法中,它可以正常工作,如下所示:
SendMessage((int)treeChild, TV_SELECTITEM, TVGN_CARET, (IntPtr)0x092DCB30);
如果我使用 SendMessage 并将 TVGN_ROOT 发送到 TreeView 句柄,它是否会返回一个 IntPtr 以及要在树视图中选择的项目的句柄,或者它是如何工作的?我也在尝试使用 AutoIt,但我希望将所有代码保存在一个应用程序中。
【问题讨论】: