Windows 窗体有一个属性 win1.Handle,如果我记得,它返回主窗口句柄的句柄?
是否有等效的方法来获取 WPF 窗口的句柄?
我在网上找到了以下代码,
IntPtr windowHandle = new WindowInteropHelper(Application.Current.MainWindow).Handle;
但我认为这对我没有帮助,因为我的应用程序有多个窗口。
谢谢!!
好吧,而不是传递Application.Current.MainWindow
,只需传递对您想要的任何窗口的引用:new WindowInteropHelper(this).Handle
等等。
只需将您的窗口与 WindowsInteropHelper 类一起使用:
// ... Window myWindow = get your Window instance...
IntPtr windowHandle = new WindowInteropHelper(myWindow).Handle;
现在,您正在请求应用程序的主窗口,其中总会有一个。但是,您可以在任何 Window 上使用相同的技术,只要它是 System.Windows.Window 派生的 Window 类。
您可以使用 :
Process.GetCurrentProcess().MainWindowHandle
Window
如果出于某种原因您想要所有应用程序的窗口句柄,您可以使用该Application.Windows
属性来获取所有 Windows,然后使用它WindowInteropHandler
来获取它们的句柄,正如您已经演示的那样。