如何从 CoreWindow 获取 ICoreWindowInterop [英] How to Get ICoreWindowInterop from CoreWindow

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

问题描述

我有一个 C++/WinRT/UWP 项目.我需要 HWND 和 HINSTANCE 才能正确初始化直接输入,否则 DirectInput 只能枚举键盘和鼠标而不是操纵杆..别问我为什么,我什至试图从 HInstance 中获取 TopMostWindowGetModule,它在从我的 C++/WinRT/UWP 应用程序运行时返回 NULL,但在从控制台应用程序运行时有效.

I have a C++/WinRT/UWP project. I need the HWND and HINSTANCE to be able to correctly initialize Direct Input, otherwise DirectInput manage only to enumerate the keyboard and mouse but not the joysticks.. don't ask me why, I even tried to get the TopMostWindow from the HInstance got with GetModule, it just return NULL when running from my C++/WinRT/UWP app, but works when running from a console app.

文档 https://docs.microsoft.com/en-us/windows/win32/api/corewindow/nn-corewindow-icorewindowinterop我不知道如何将我的 CoreWindow 转换为 ICoreWindowInterop.CComPtr 在 C++/WinRT 中不可用且不可用,与 IUnkwnown 冲突.

documentation https://docs.microsoft.com/en-us/windows/win32/api/corewindow/nn-corewindow-icorewindowinterop I have no idea how to cast my CoreWindow to ICoreWindowInterop. CComPtr is not availble and not usable in C++/WinRT, conflicts with IUnkwnown.

在方法 App::OnLaunched(LaunchActivatedEventArgs const& e)我得到这样的当前窗口

In the method App::OnLaunched(LaunchActivatedEventArgs const& e) I get the current window like this

CoreWindow w = Window::Current().CoreWindow().GetForCurrentThread();

CoreWindow w = Window::Current().CoreWindow().GetForCurrentThread();

然后我不知道如何从中获取 ICoreWindowInterop.CoreWindow 是一个 ICoreWindow ,但我在 Microsoft 文档中没有看到任何解释:/

Then I don't know how to get the ICoreWindowInterop from it. CoreWindow is a ICoreWindow , but I don't see any explanation in the Microsoft documentation :/

我尝试进行转换并重新解释转换但没有成功(编译错误).我不是 COM/Windows 专家,所以我现在很迷茫.

I tried casting and reinterpret casting without success ( compilation error ). I'm no COM/Windows expert, so I'm quite lost now.

感谢您的帮助干杯,塞布

Thanks for the help Cheers, Seb

推荐答案

ICoreWindowInterop 不能立即从 CoreWindow 获得.界面是隐形的,因此在使用 IInspectable 的自省时不会显示.您必须下拉到原始 COM 并显式查询接口.

The ICoreWindowInterop is not immediately available from a CoreWindow. The interface is cloaked, and as such will not show up when using IInspectable's introspection. You'll have to drop down to raw COM and explicitly query for the interface.

Kenny Kerr 多年前写过一篇文章 (Windows 8,你把我的 HWND 放在哪里了?!)详细说明了所需的步骤.要在 C++/WinRT 应用程序中编译它,还需要做一些工作.

Kenny Kerr has written an article years ago (Windows 8, where’d you put my HWND?!) that details the required steps. There's still a bit of work required to get this to compile in a C++/WinRT application.

首先,您必须声明 ICoreWindowInterop 接口.以下内容就足够了:

First up, you'll have to declare the ICoreWindowInterop interface. The following will suffice:

struct
__declspec(uuid("45D64A29-A63E-4CB6-B498-5781D298CB4F"))
__declspec(novtable)
ICoreWindowInterop : public IUnknown
{
    virtual HRESULT STDMETHODCALLTYPE get_WindowHandle(HWND* hwnd) = 0;
    virtual HRESULT STDMETHODCALLTYPE put_MessageHandled(unsigned char value) = 0;
};

接下来,我们需要一个指向 CoreWindowIUnknown 接口指针.有预建功能作为免费功能 get_unknown.要使其编译,您必须在包含任何 C++/WinRT 头文件之前 #include .

Next, we need an IUnknown interface pointer to the CoreWindow. There is pre-built functionality as the free function get_unknown. To get this to compile, you'll have to #include <Unknwn.h> before including any C++/WinRT headers.

一旦一切就绪,您就可以轻松获得 HWND 给定的 CoreWindow 实例:

Once all that is in place, you can easily get the HWND given a CoreWindow instance:

HWND from_core_window(CoreWindow const& window)
{
    winrt::com_ptr<ICoreWindowInterop> interop {};
    winrt::check_hresult(winrt::get_unknown(window)->QueryInterface(interop.put()));
    HWND hwnd {};
    winrt::check_hresult(interop->get_WindowHandle(&hwnd));
    return hwnd;
}

似乎有证据表明,深入到 HWNDMicrosoft Store 认证失败.如果这是一个问题,您必须找到不同的解决方案.

There seems to be evidence that reaching down to the HWND will fail Microsoft Store certification. If that is an issue, you'll have to find a different solution.

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

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