0

I am using the MPV.Net library, as shown below, which requires me to provide a control handle: MpvPlayer(IntPtr hwnd, string libMpvPath)

However, I am using Microsoft’s WinUI3 technology, similar to WPF, and I cannot get the control handle. I want to use a WinForm control but I can’t reference it and I don’t know what to do.

Below is the initialization of a static MPVPlayer object MP that I wrote in HomePage:

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Mpv.NET.Player;
using PigeonPlayer.ViewModels;

namespace PigeonPlayer.Views;

public sealed partial class HomePage : Page
{
    public HomeViewModel ViewModel
    {
        get;
    }

    public static MpvPlayer mp;

    public HomePage()
    {
        initMPV();
        ViewModel = App.GetService<HomeViewModel>();
        InitializeComponent();
    }

    async void initMPV()
    {
        StackPanel sp = new StackPanel();
        string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
        string dllPath = Path.Combine(currentDirectory, "mpv-1.dll");
        mp = new MpvPlayer( dllPath);
    }
}

I don’t know how to modify it. I used some WinUI.Interop but it can only get the parent window handle.

I tried to use System.Windows.Form and WindowsFormsHost to use Winforms controls like in WPF, but failed. I couldn't use Winform controls in my WinUI project.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • A WinUI3 control has no window handle. Only the Window class has one, that you can get with `var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(thisWindowInstance);`. See also https://stackoverflow.com/questions/74273875/retrive-window-handle-in-class-library-winui3/74286947#74286947 – Simon Mourier Jul 12 '23 at 18:01

1 Answers1

0

I suggest you could refer to the thread: How to get a control's handle in WinUI3 in C++?

XAML is a windowless framework. Controls don't have windows. All XAML controls on the screen are ultimately backed by a single HWND that belongs to the parent window.

You just could retrieve the window handle for the window. You could try to call the GetWindowHandle method on the WinRT.Interop.WindowNative C# interop class.

// MainWindow.xaml.cs
private async void myButton_Click(object sender, RoutedEventArgs e)
{
    // Retrieve the window handle (HWND) of the current WinUI 3 window.
    var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
}

For more details, I suggest you could refer to the Doc:Retrieve a window handle

Jeaninez - MSFT
  • 3,210
  • 1
  • 5
  • 20
  • If I get the window handle, I won’t be able to initialize the MpvPlayer instance.This library needs a control handle during initialization, and if I don’t provide one, it will create its own window. But that won’t look or work the way I want it to. – PigeonMuyz Jul 18 '23 at 17:29
  • Anyway, controls don't have windows in winui3. You couldn't get the control handle in winui3. I suggest you could post the issue to GitHub for better help: https://github.com/mpvnet-player/mpv.net/issues – Jeaninez - MSFT Jul 19 '23 at 08:24