从 UWP 中的 CoreWindow 对象中获取 HWND [英] Getting HWND off of CoreWindow object in UWP

查看:19
本文介绍了从 UWP 中的 CoreWindow 对象中获取 HWND的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简短的 MSDN 文档说 CoreWindow 有 ICoreWindowInterop,可以获取 CoreWindow 的句柄 HWND.但是我找不到有关如何获取它的参考(C#).请帮忙.

This short MSDN documentation says CoreWindow has ICoreWindowInterop that obtains the handle HWND to the CoreWindow. But I cannot find references on how to get it (C#). Help, please.

https://msdn.microsoft.com/en-us/library/dn302119(v=vs.85).aspx

推荐答案

此 COM 接口只能由 C++ 代码直接访问.在 C# 中,您必须自己声明它并使其与 C:Program Files (x86)Windows Kits10Include10.0.10586.0winrtCoreWindow.idl 中的接口声明匹配.像这样:

This COM interface is only directly accessible to C++ code. In C# you have to declare it yourself and make it match the interface declaration in C:Program Files (x86)Windows Kits10Include10.0.10586.0winrtCoreWindow.idl. Like this:

using System.Runtime.InteropServices;
...
    [ComImport, Guid("45D64A29-A63E-4CB6-B498-5781D298CB4F")] 
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface ICoreWindowInterop {
        IntPtr WindowHandle { get; }
        bool MessageHandled { set; }
    }

获取接口引用需要转换,编译器不会让你直接从 CoreWindow 对象中转换.让 DLR 完成工作最容易做到,如下所示:

Obtaining the interface reference requires casting, the compiler won't let you cast from the CoreWindow object directly. It is most easily done by letting the DLR get the job done, like this:

    dynamic corewin = Windows.UI.Core.CoreWindow.GetForCurrentThread();
    var interop = (ICoreWindowInterop)corewin;
    var handle = interop.WindowHandle;

这篇关于从 UWP 中的 CoreWindow 对象中获取 HWND的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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