如果您只是想在相当典型的标签,字段和按钮应用程序中单击按钮,则可以使用一些P/Invoke来使用FindWindow和SendMessage控制.
如果您还不熟悉Spy ++,现在是时候开始了!
它与Visual Studio 2012 RC打包在:C:\Program Files\Microsoft Visual Studio 11.0\Common7\Tools.对于其他版本应该类似地找到它.
试试这个Console C#应用程序:
class Program
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
private const uint BM_CLICK = 0x00F5;
static void Main(string[] args)
{
// Get the handle of the window
var windowHandle = FindWindow((string)null, "Form1");
// Get button handle
var buttonHandle = FindWindowEx(windowHandle, IntPtr.Zero, (string)null, "A Huge Button");
// Send click to the button
SendMessage(buttonHandle, BM_CLICK, 0, 0);
}
}
Run Code Online (Sandbox Code Playgroud)
这将获得窗口标题为"Form1"的句柄.使用该句柄,它获取Window中Button的句柄.然后向按钮控件发送一条"BM_CLICK"类型的消息,其中没有有用的参数.
我使用测试WinForms应用程序作为我的目标.一个按钮和一些代码后面增加一个计数器.

运行P/Invoke控制台应用程序时,您应该看到计数器增量.但是,您不会看到按钮动画.
您还可以使用Spy ++ Message Logger功能.我建议使用BM_CLICK过滤器,也可以使用WM_LBUTTONDOWN/WM_LBUTTONUP(手动点击会给你).
希望有所帮助!
| 归档时间: |
|
| 查看次数: |
10599 次 |
| 最近记录: |