Async's blog - .Net
type: channel
link: https://blog.async.website/index.php/tag/Net/
items: WINUI3，WinAppSDK1.7实现Window的Min/Max width,height
items: C# LibVLCSharp将VLC窗口渲染至当前窗口的两种方式

WINUI3，WinAppSDK1.7实现Window的Min/Max width,height
type: item
date: 2025-05-22T08:14:56+00:00
description: 来自github官方示例。public sealed partial class MainWindow: Window { private static WinProc newWndProc = null; private static IntPtr oldWndProc = IntPtr.Zero; private delegate IntPtr WinProc(IntPtr hWnd, WindowMessage Msg, IntPtr wParam, IntPtr lParam); [DllImport("User32.dll")] internal static extern int GetDpiForWindow(IntPtr hwnd); [DllImport("user32.dll", EntryPoint = "SetWindowLong")] private static extern int SetWindowLong32(IntPtr hWnd, WindowLongIndexFlags nIndex, WinProc newProc); [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")] private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, WindowLongIndexFlags nIndex, WinProc newProc); [DllImport("user32.dll")] private static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, WindowMessage Msg, IntPtr wParam, IntPtr lParam); public static int MinWindowWidth { get; set; } = 900; public static int MaxWindowWidth { get; set; } = 1800; public static int MinWindowHeight { get; set; } = 600; public static int MaxWindowHeight { get; set; } = 1600; public MainWindow() { this.InitializeComponent(); RegisterWindowMinMax(this); } private static void RegisterWindowMinMax(Window window) { var hwnd = GetWindowHandleForCurrentWindow(window); newWndProc = new WinProc(WndProc); oldWndProc = SetWindowLongPtr(hwnd, WindowLongIndexFlags.GWL_WNDPROC, newWndProc); } private static IntPtr GetWindowHandleForCurrentWindow(object target) => WinRT.Interop.WindowNative.GetWindowHandle(target); private static IntPtr WndProc(IntPtr hWnd, WindowMessage Msg, IntPtr wParam, IntPtr lParam) { switch (Msg) { case WindowMessage.WM_GETMINMAXINFO: var dpi = GetDpiForWindow(hWnd); var scalingFactor = (float)dpi / 96; var minMaxInfo = Marshal.PtrToStructure(lParam); minMaxInfo.ptMinTrackSize.x = (int)(MinWindowWidth * scalingFactor); minMaxInfo.ptMaxTrackSize.x = (int)(MaxWindowWidth * scalingFactor); minMaxInfo.ptMinTrackSize.y = (int)(MinWindowHeight * scalingFactor); minMaxInfo.ptMaxTrackSize.y = (int)(MaxWindowHeight * scalingFa…

C# LibVLCSharp将VLC窗口渲染至当前窗口的两种方式
type: item
date: 2025-05-20T02:02:00+00:00
description: 在添加了LibVLCSharp和LibVLCSharp.Platforms.Windows的Nuget包后，新建media绑定至XAML页面的VideoView的MediaPlayer，启动Play事件，VLC会弹出一个单独的Foreground窗口来进行输出，尝试了两种方式将MediaPlayer约束近当前窗口。1.句柄方法0X00.引用using LibVLCSharp.Platforms.Windows; using LibVLCSharp.Shared;0X01.NativeMethodspublic static class NativeMethods { [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetActiveWindow(); [DllImport("user32.dll", SetLastError = true)] public static extern bool IsWindow(IntPtr hWnd); } //通过P/Invoke调用Windows API0X02.获取Foreground窗口句柄并渲染VLCprivate MediaPlayer mediaPlayer; private IntPtr hwnd; private LibVLC libVLC; mediaPlayer ??= new MediaPlayer(libVLC); mediaPlayer. Media = new Media(libVLC, new System.Uri("your video uri here")); mediaPlayer.Hwnd = hwnd; if (!mediaPlayer.IsPlaying) mediaPlayer.Play();2.SwapChain方法0X00.引用using LibVLCSharp.Platforms.Windows; using LibVLCSharp.Shared;0X01.VideoView Initialized事件private MediaPlayer mediaPlayer; private LibVLC libVLC; private string[] swapChain; private void videoView_Initialized(object sender, InitializedEventArgs e) { swapChain = e.SwapChainOptions; libVLC = new LibVLC(swapChain); }0X02.PageLoaded事件private void Page_Loaded(object sender, RoutedEventArgs e) { mediaPlayer ??= new MediaPlayer(libVLC); mediaPlayer. Media = new Media(libVLC, new System.Uri("your video uri here")); videoView.MediaPlayer=mediaPlayer; if (!videoView.MediaPlayer.IsPlaying) videoView.MediaPlayer.Play(); }