查找 WPF 窗口的句柄

发布于 2024-08-07 17:38:10 字数 255 浏览 3 评论 0原文

Windows 窗体有一个属性 win1.Handle,如果我记得的话,它返回主窗口句柄的句柄?

有没有等效的方法来获取 WPF 窗口的句柄?

我在网上找到了以下代码,

IntPtr windowHandle = 
    new WindowInteropHelper(Application.Current.MainWindow).Handle;

但我认为这对我没有帮助,因为我的应用程序有多个窗口。

Windows forms had a property win1.Handle which, if I recall, returns the handle of the main window handle?

Is there an equivalent way to get the handle of a WPF Window?

I found the following code online,

IntPtr windowHandle = 
    new WindowInteropHelper(Application.Current.MainWindow).Handle;

But I don't think that will help me because my application has multiple windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

贱贱哒 2024-08-14 17:38:10

那么,不用传递 Application.Current.MainWindow,只需传递对您想要的窗口的引用:new WindowInteropHelper(this).Handle 等等。

Well, instead of passing Application.Current.MainWindow, just pass a reference to whichever window it is you want: new WindowInteropHelper(this).Handle and so on.

轻拂→两袖风尘 2024-08-14 17:38:10

只需将您的窗口与 WindowsInteropHelper 类一起使用:

// ... Window myWindow = get your Window instance...
IntPtr windowHandle = new WindowInteropHelper(myWindow).Handle;

现在,您正在请求应用程序的主窗口,其中总会有一个。但是,您可以在任何 Window 上使用相同的技术,只要它是 System.Windows.Window 派生的 Window 类。

Just use your window with the WindowsInteropHelper class:

// ... Window myWindow = get your Window instance...
IntPtr windowHandle = new WindowInteropHelper(myWindow).Handle;

Right now, you're asking for the Application's main window, of which there will always be one. You can use this same technique on any Window, however, provided it is a System.Windows.Window derived Window class.

纸短情长 2024-08-14 17:38:10

你可以使用:

Process.GetCurrentProcess().MainWindowHandle

you can use :

Process.GetCurrentProcess().MainWindowHandle
别低头,皇冠会掉 2024-08-14 17:38:10

在我的用例中,我需要在启动期间主窗口的句柄,无论我做什么,我都无法让 new WindowInteropHelper(...).Handle 返回除空句柄之外的任何内容因为窗口尚未初始化。

您可以使用 < code>EnsureHandle() 方法,如果句柄尚不存在,它将创建句柄,如果存在,则返回当前句柄。

var hWnd = new WindowInteropHelper(Application.Current.MainWindow).EnsureHandle();

应用程序启动后,它会继续使用相同的句柄,不会出现问题。

In my use case I needed a handle to the main window during startup, and no matter what I did I couldn't get new WindowInteropHelper(...).Handle to return anything other than a null handle since the window hadn't been initialized yet.

You can use the EnsureHandle() method instead, which will create the handle if it doesn't already exist, or return the current one if it does exist.

var hWnd = new WindowInteropHelper(Application.Current.MainWindow).EnsureHandle();

Once the application has started, it continues using the same handle without issue.

爱你是孤单的心事 2024-08-14 17:38:10

如果您出于某种原因想要应用程序的所有 Window 的窗口句柄,则可以使用 Application.Windows 属性获取所有 Windows,然后使用 WindowInteropHandler 来获取它们的句柄,正如您已经演示的那样。

If you want window handles for ALL of your application's Windows for some reason, you can use the Application.Windows property to get at all the Windows and then use WindowInteropHandler to get at their handles as you have already demonstrated.

不气馁 2024-08-14 17:38:10

在我的例子中,在 OnLoaded() 事件之后工作。在构造函数中它给出了一个零句柄。

var winHandle = new WindowInteropHelper(this).Handle;

Works after OnLoaded() event in my case. In the constructor it gives a zero handle.

var winHandle = new WindowInteropHelper(this).Handle;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文