【发布时间】:2017-12-06 10:19:32
【问题描述】:
我有两个项目(TestVisual 和 RecordPlayBack)。 TestVisual 有两个窗口(MainWindow 和 TestWindow)。我可以使用下面的代码获取 TestWindow 实例,
var windows = System.Windows.Application.Current.Windows;
IntPtr twHandle = new System.Windows.Interop.WindowInteropHelper(windows[2]).Handle;
现在,我从 TestVisual.wpf 应用程序运行 RecordPlayBack.exe。因此,Application.Current 保存了 TestWindow 不可用的 RecordPlayBack 应用程序。在这种情况下,如何获取TestVisual应用程序的TestWindow实例?
我已经尝试过下面的代码,
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
IntPtr twHandle =(IntPtr) User32.FindWindow("test", null);
请给我建议。
注意: 添加 RecordPlayBack 项目作为对 TestVisual 项目的引用。
谢谢,
【问题讨论】:
-
这可能以前被问过,例如stackoverflow.com/questions/1953582/… 或stackoverflow.com/questions/13547639/…。希望这些答案对您有所帮助!
-
嗨@GrahamMc,感谢您的建议。我已经添加了我的应用程序的屏幕截图,请看一下。我尝试了以下建议, [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();但它捕获了“RecordWindow”,因为我试图在单击 RecordWindow 中的停止按钮时捕获“TestWindow”。所以在这种情况下,RecordWindow 出现在前面。还有其他建议吗?
-
也许可以在这里查看stackoverflow.com/questions/2281429/…,了解如何获取进程中所有窗口的列表。使用答案获得流程后,您可以列出所有窗口并找到正确的窗口?