'How to get a control's handle in WinUI3 in C++?

I am working on a C++ WinUI3 project and got this problem. How do I obtain the handle of a XAML grid?

Something like this in QT can achieve it:

HWND m_hWnd;

m_hWnd = (HWND)(ui.label->winId());

But I couldn't find the similar thing in WinUI3. I look up on the Internet, but only find this solution which is in C#:

Get-a-controls-handle-in-XAML

And the Microsoft provide this, but it is only working for window handle, not XAML controls.

Retrieve a window handle (HWND)

Could anyone help me with it? Any suggestions will be great!



Solution 1:[1]

How do I obtain the handle of a XAML grid?

You can't because there is none.

All XAML controls on the screen are ultimately backed by a single HWND that belongs to the parent window, i.e. an individual control doesn't have its own handle in Win UI.

There is only a single top-level handle and the controls are rendered as content inside this HWND.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mm8