WinUI3 : How to get the WinUI3 window back from its respective HWND - winapi

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

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)

Related

How to get the Window hosting a UIElement instance

I'm trying to get the Window instance which is hosting a UIElement instance in WinUI 3.
There's a helper method in the Window class in .NET (see this thread) but i cannot find something similar for C++/WinRT.
I tried VisualTreeHelper as some suggested but it doesn't help here; none of the parents were of type winrt::Microsoft::UI::Xaml::Window.
Is it possible to get the hosting Window of a dependency object?
If it is a UWP application, each UI thread already has a Window that can be retrieved using the static Window.Current property.
If it is a C++/WinRT WinUI3 in Desktop application, Window implements IWindowNative to enable interop through the Window's HWND (WindowHandle). You could get the handle of the Window and do what you want. Like:
// Get the current window's HWND by passing in the Window object
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
For more information, please check: Window Class-Windows App SDK

How to monitor all the top level Windows from an inactive Win32 application?

I am trying to create a Windows Win32 API application which has a requirement to get the Window RECT size of all the overlaying Windows oriented above my App Window.
Currently I am monitoring WM_ACTIVATE messages to keep track of when my app windows has another Window placed on top of it. This works fine but there is a hard limitation. It only helps when my app Window is going from "active/foreground" state to "inactive/background" state. Once it goes to "inactive" state, my app is no longer subscribing to windows messaging. So thereafter, when more windows are opened and my app goes below the Z-counts, I cannot get their handles at all.
What I am looking for is to persistantly get the messaging for the Windows which are opened.
I am currently using EnumWindows API to enumerate all the Windows from the active screen. I want to know how can I continually keep track of the other Windows irrespective of my app being active/inactive?
Right now, I am creating my main Window using standard CreateWindow arguments as this:
auto window_type = WS_OVERLAPPEDWINDOW;
auto window_ex_type = CW_USEDEFAULT;
HWND hWnd = CreateWindowW(szWindowClass, szTitle, window_type,
window_ex_type, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

Identify UWP application as soon as it is launched

I am monitoring the current foreground process using SetWinEventHook() and EVENT_SYSTEM_FOREGROUND, which gives me the HWND of the window that is currently in the foreground.
I've noticed that UWP Apps are just ApplicationFrameHost.exe processes and the foreground HWND points to an ApplicationFrameHost window. Per another StackOverflow post, to find the actual HWND of the app window, I am supposed to find the first child window of that window that does not belong to the same process, then do my processing there. My processing is just finding the AUMID.
Is there any way to get the app window HWND, or the AUMID, as soon as the ApplicationFrameHost is launched, instead of waiting for the app window to be fully initialized?

How do I get the handle of another's window (browser) from my program?

WINAPI
How do I get the handle of another's window (browser) from my program?
Searching by the name of the window does not help, as the name changes.
For example, if you want to find Google Chrome window's handle, use the following code:
//Find the Google Chrome
HWND hGoogleChrome = FindWindowEx(NULL, NULL, L"Chrome_WidgetWin_1", NULL);
Chrome_WidgetWin_1 will rename the Chrome window's name regardless of title that changes.

Can you have a win32 program that consists solely of a tray (notification) icon?

I have a program that literally consists of a tray icon. No GUI is needed. However, when writing the win32 code, is it necessary to still initialize a hWnd object to be associated with the tray icon?
For instance, it is normal to have a the NOTIFYICONDATA hWnd field point to the window's handle. Like
nid.hWnd = hwnd;
Essentially, will my icon be able to still receive messages if i set
nid.hwnd = NULL;
How would you receive messages without a window?
Yes you need a window associated with the tray icon.
You could create a message-only window by specifying HWND_MESSAGE creating the window. However, message-only windows do not receive broadcast messages and you would miss out on the TaskbarCreated message. This message tells your application that explorer.exe has restarted and that your application needs to re-add its notification icons. Rather important. So create a window that never becomes visible: never call ShowWindow().

Resources