PigeonMuyz
PigeonMuyz

Reputation: 1

Unable to provide a control handle when initializing MPV.Net in WinUI3 and C#

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.

Upvotes: 0

Views: 200

Answers (1)

Jeaninez - MSFT
Jeaninez - MSFT

Reputation: 4040

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

Upvotes: 0

Related Questions