【发布时间】:2020-02-12 11:39:20
【问题描述】:
我已经使用 c# 创建了一个 Windows 窗体应用程序,并且我有一个要启动的应用程序。我以管理员身份成功启动了应用程序,接下来我要做的是向应用程序发送一些键,特别是 Alt 和箭头键并输入。我使用 FindWindow 函数通过窗口名称查找窗口。然后我使用 SetForeground 函数将焦点设置到我的应用程序,最后我将键发送给它。
为了检查我的代码是否正确,我尝试将密钥发送到标题为“无标题 - 记事本”的记事本,它可以正常工作。但是如果我用应用程序的标题替换它,它就不起作用。我使用了 Visual Studio 提供的“spy++”工具。我开始我的申请。复制它的类名和窗口标题,粘贴到 FindWindow 函数中,还是不行。
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = finalString;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.Verb = "runas";
myProcess.StartInfo.Arguments = "--AutoInject --EmulateController";
myProcess.Start(); String Pid = "" + myProcess.Id;
/* The variables found using spy++
Window Caption = [DEBUG] PS4 Macro - v0.5.2 (BETA)
Class Name = WindowsForms10.Window.8.app.0.31c915c_r6_ad1
*/
IntPtr AppHandle = FindWindow(null, "[DEBUG] PS4 Macro - v0.5.2 (BETA)");
if (AppHandle == IntPtr.Zero)
{
MessageBox.Show("Something's wrong I can feel it");
return;
}
SetForegroundWindow(AppHandle);
SendKeys.SendWait("^(o)");
运行后,应用程序运行,但键 ctrl+o 没有到达它。 它应该收到 ctrl+o 命令。另请注意:当应用程序启动时,我的表单的菜单栏不会变白,即它可能不会失去焦点。我表单的菜单栏和启动的应用程序都是黑色的。
【问题讨论】: