0

I'm working on the WinUI3 desktop application. I'm trying to set an ID to the winui3 window and get the WinUI3 window back with only the ID. I could not find any direct way to do this.

I tried to extract HWND from the WinUI3 window and set the ID using SetProp.

uWindow.try_as<::IWindowNative>()->get_WindowHandle(&hWnd);

SetProp(hWnd,"ID","WindowID")

But it doesn't fit well as per my requirements. I want to get the WinUI3 Window back when I have only the ID, I was able to get the HWND of the window back from the ID which I set as the property for HWND, but I was not able to get back the WinUI3 Window from that HWND.

It would be of great help if you could help me get the WinUI3 window back from HWND of its native window.

Thank you

Harshith
  • 181
  • 2
  • 9
  • 1
    Not sure there's any facility for that, but you own the windows, so why don't you add some dictionary/map (hwnd => Window) to your app and publish it somehow? You can also use a pointer (cast to HANDLE) with SetProp – Simon Mourier Feb 15 '23 at 17:39
  • Thank you for your response, I'm thinking of mapping the hWnd and windows as my last choice – Harshith Feb 16 '23 at 09:31

1 Answers1

0

You could try to follow the following steps:

1, Retrieve the HWND for your existing window object via Retrieve a window handle

2, Pass that HWND to the GetWindowIdFromWindow interop function to retrieve a WindowId.

3, Pass that WindowId to the static AppWindow.GetFromWindowId method to retrieve the AppWindow.

For more details I suggest you could refer to the Doc: Manage app windows (Windows App SDK)

Jeaninez - MSFT
  • 3,210
  • 1
  • 5
  • 20
  • Thank you for your response, In this case how to set a custom ID for a Window, and can we get back the WinUI3 Window from the AppWindow? – Harshith Feb 16 '23 at 09:30
  • Could you please tell us why you want to set a custom ID for a Window? `WindowId` could be available through GetWindowIdFromWindow. `WindowId`: The identifier for the app window. AppWindow represents a high-level abstraction of a system-managed container for the content of an app. If you're familiar with Win32, the "app window" can be seen as a high-level abstraction of the HWND. – Jeaninez - MSFT Feb 17 '23 at 03:17
  • We are working on a Multiplatform system, so we want to set our own ID to the respective windows – Harshith Feb 17 '23 at 10:33
  • 1
    It seems that you couldn't add a custom Id property to the window. https://github.com/microsoft/microsoft-ui-xaml/issues/6241 – Jeaninez - MSFT Feb 21 '23 at 09:01