3

I want a Winui3 window, to host a Win32 control, I tried the following:

App.xaml.cs:

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
    m_window = new MainWindow();
    var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(m_window);
    var ctrl = CreateWindow("EDIT", "Edit", WindowStyles.WS_CHILD | WindowStyles.WS_VISIBLE, 100, 100, 300, 50, hwnd, (IntPtr)0, (IntPtr)0, (IntPtr)0);
        
    m_window.Activate();
}

Unfortunately, the control did not show up directly, It was only visible if you resize the window really fast. I think that's because the control is created behind the XAML. I tried to fix this, by setting the WindowStyle to TopMost, but it didn't work either.

SetWindowPos(ctrl, (IntPtr)(-1), 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);

Is there a workaround, or did I forget something?

The documentation, to access Win32 apis: https://learn.microsoft.com/en-us/windows/apps/winui/winui3/desktop-winui3-app-with-basic-interop

FrozenAssassine
  • 1,392
  • 1
  • 7
  • 16
  • Sounds like you're trying to host a native Win32 control inside a XAML window. I'm not even sure that kind of interop is supported. Though, since I don't care about WinUI 3, I might just be wrong. – IInspectable May 28 '22 at 08:03
  • 2
    WinUI currently has no facility for putting a win32 window as a child of a XAML window. You can try making your Win32 window a sibling of the DesktopWindowXamlSource. (Make your main window a win32 window, and then put the XAML as a child of the main window. Then give the main window a second child that is your Win32 window. I don't know how well this will work but it's worth a try.) – Raymond Chen May 28 '22 at 12:35
  • Check if this helps - https://learn.microsoft.com/en-us/answers/questions/1115740/can-we-use-both-winui3-controls-and-win32-controls.html – Harshith Dec 07 '22 at 11:31

0 Answers0