背景
我正在使用SendKeys()
向活动窗口发送键盘命令,但在通过RemoteApp运行应用程序时,我无法找到子窗口。当我本地运行应用程序时,一切都按预期工作。
Microsoft RemoteApp允许用户通过RDP协议连接到应用程序,但它只显示应用程序窗口,而不是整个远程虚拟机。对于最终用户来说,在RemoteApp下运行的应用程序与在其桌面上运行的应用程序没有区别。
我一直在使用ManagedSpy确定.NET应用程序窗口的类名,以便我可以使用Win32 API函数FindWindowEx
使其中一个子窗口处于活动状态,并且它工作得很好。然而,在应用程序通过RemoteApp运行时,我遇到了问题。
我仍然可以使用.NET Process.GetProcessesByName()
查找应用程序,只需让它调用mstsc.exe
:
IntPtr hwndChild = IntPtr.Zero;
Process[] processess = Process.GetProcessesByName("mstsc");
IntPtr appHandle = IntPtr.Zero;
foreach (Process p in processess)
{
if ((p.MainWindowHandle != IntPtr.Zero))
{
appHandle = p.MainWindowHandle;
}
}
if (appHandle == IntPtr.Zero)
{
MessageBox.Show("Application is not Running.");
return;
}
然而,我无法以同样的方式使用
FindWindowEx
。这个问题围绕着它。为了让非托管代码告诉我哪些窗口是活动的,我使用了Spy++,但是对于
mstsc.exe
,它返回了一个不同的类名,称为RAIL_WINDOW
:以下是我用来查找子窗口的代码:
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
hwndChild = FindWindowEx(appHandle, IntPtr.Zero, "RAIL_WINDOW", "MyApplication (Remote)");
SetForegroundWindow(hwndChild);
问题
我可以使用Spy++在RemoteApp版本的应用程序中突出显示活动子窗口,并获得
RAIL_WINDOW
,但似乎无法通过编程方式访问此窗口。根据上面的代码,我错过了什么才能够这样做?还有其他方法可以将键盘按键发送到在Remote App上运行的应用程序吗?