7

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

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
gt6707a
  • 649
  • 1
  • 7
  • 12

3 Answers3

13

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

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;
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks. This seems to work. I have some value in WindowHandle property. Now I need to figure out how to show the window in a specific monitor from C#. If possible. – gt6707a Jan 21 '16 at 23:02
  • 2
    @gt6707a: If an answer solves your issue, you should accept it. This makes it easier for future visitors to navigate and identify relevant information. – IInspectable Sep 08 '16 at 11:35
  • 1
    The question is - what are you going to do with that HWND? There are no supported APIs that take HWNDs. – Peter Torr - MSFT Sep 13 '16 at 18:43
  • 6
    I just pass the bullets provided by Microsoft, loading them into his gun is up to the OP. – Hans Passant Sep 13 '16 at 19:09
  • @gt6707a, did you find it? I would be really intersted in seeing your solution to display a Window on a specific monitor. If you have time you can send me your solution to ericouellet2@gmail.com – Eric Ouellet Jun 03 '21 at 20:54
6

Please note there are no supported APIs for UWP that accept an HWND. Any API you call will fail Windows Store certification, and even if you avoid the Windows Store (eg, side-load or go through an Enterprise deployment) there is no guarantee the app will work in the future.

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
  • Actually thinking of make a utility to let user to open all third party UWP app in PIP mode (CompactOverlay) , is it possible? Suited People with multi tasking need. – Edward Chan JW Jan 11 '19 at 02:12
0

there are no supported APIs for UWP that accept an HWND

While generally UWA apps should never reason about HWNDs there are many WinRT APIs that support accepting HWNDs to support use from Win32 Apps. Here is an example and another. C# clients will need to use interop techniques, here is an example of that.

Chris Guzak
  • 111
  • 6