获取窗口的句柄在C# [英] Getting the handle of window in C#

查看:639
本文介绍了获取窗口的句柄在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类中声明:

public partial class MainWindow : Window

和我需要得到该窗口的实际把手,一旦窗口有一个。
我怎样才能做到这一点,我应该把查询功能。

And I need to get the actual handle of the window once the window has one. How can I do that and where should I put the query function.

我试过到目前为止是:

IntPtr hwnd = new WindowInteropHelper(this).Handle;

不过,我找回了句柄为0,这可能是因为它被种植在OnInitialized - 也许该窗口还没有准备好在那个阶段。
而且,是的 - 它是通过WPF连接,谢谢指点出来

But the handle I get back is 0, which might be because it was planted in OnInitialized - maybe the window is not ready yet at that stage. And, yes - it is connected via WPF, thank you for pointing it out!

感谢

推荐答案

OnInitialized 方法在句柄尚未建立。但你是在正确的轨道上。如果你把你的电话在加载事件在处理将被创建,它应该返回正确的处理

In the OnInitialized method the handle has not yet been created. But you are on the right track. If you put your call in the Loaded event the handle will have been created and it should return the correct handle.

这篇关于获取窗口的句柄在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆