Getting Hwnd Off of Corewindow Object in Uwp

How to Get ICoreWindowInterop from CoreWindow

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 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.

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;
};

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.

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;
}

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.

How to round corner of App main window in c# windows 10 UWP?

How to round corner of App main window in c# windows 10 UWP?

I'm afraid there is not api could set the app's main window round corner for uwp currently, it is managed by system, it has not provided api to set it manaually.

Derive from code you mentioned, you could get hwnd of window, but there are no supported APIs that take HWNDs. For DwmSetWindowAttribute please refer to it's document minimum supported client Windows Vista [desktop apps only], but not UWP platform.



Related Topics



Leave a reply



Submit