问题描述
这个简短的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.
推荐答案
此COM接口只能由C ++代码直接访问。在C#中,您必须自行声明它,并使它与C:\Program Files(x86)\Windows Kits\10\Include\10.0.10586.0\winrt\CoreWindow.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 Kits\10\Include\10.0.10586.0\winrt\CoreWindow.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!