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
Console App Mouse-Click X Y Coordinate Detection/Comparison
C# Code to Linkify Urls in a String
Quartz.Net Setup in an ASP.NET Website
How to Add an Item to a Listbox in C# and Winforms
How to Create an Instance from a String in C#
Determine Number of Pages in a PDF File
Binding Selecteditems of Listview to Viewmodel
Is That Possible to Send Httpwebrequest Using Tls1.2 on .Net 4.0 Framework
Linq: Passing Lambda Expression as Parameter to Be Executed and Returned by Method
How to Check for Nulls in a Deep Lambda Expression
Anyone Know of a Set of C# Bindings for Ffmpeg
Reading from Excel (Range into Multidimensional Array) C#
Formatting Datetime in ASP.NET Core 3.0 Using System.Text.JSON
Date Difference in Years Using C#
JSON.Net Custom Serialization with JSONconverter - How to Get the "Default" Behavior