您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“C#中wpf如何通過HwndHost渲染視頻”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“C#中wpf如何通過HwndHost渲染視頻”這篇文章吧。
通過繼承HwndHost并實現抽象方法即可作為一個帶句柄的wpf控件在xaml中使用,代碼如下:
win32Api版本:
class NativeHost : HwndHost { new public IntPtr Handle { get { return (IntPtr)GetValue(HandleProperty); } set { SetValue(HandleProperty, value); } } // Using a DependencyProperty as the backing store for Hwnd. This enables animation, styling, binding, etc... public static readonly DependencyProperty HandleProperty = DependencyProperty.Register("Handle", typeof(IntPtr), typeof(NativeHost), new PropertyMetadata(IntPtr.Zero)); protected override HandleRef BuildWindowCore(HandleRef hwndParent) { Handle = CreateWindowEx( 0, "static", "", WS_CHILD | WS_VISIBLE | LBS_NOTIFY, 0, 0, (int)Width, (int)Height, hwndParent.Handle, IntPtr.Zero, IntPtr.Zero, 0); return new HandleRef(this, Handle); } protected override void DestroyWindowCore(HandleRef hwnd) { DestroyWindow(hwnd.Handle); } const int WS_CHILD = 0x40000000; const int WS_VISIBLE = 0x10000000; const int LBS_NOTIFY = 0x001; [DllImport("user32.dll")] internal static extern IntPtr CreateWindowEx(int exStyle, string className, string windowName, int style, int x, int y, int width, int height, IntPtr hwndParent, IntPtr hMenu, IntPtr hInstance, [MarshalAs(UnmanagedType.AsAny)] object pvParam); [DllImport("user32.dll")] static extern bool DestroyWindow(IntPtr hwnd); }
HwndSource版本:
class NativeHost : HwndHost { new public IntPtr Handle { get { return (IntPtr)GetValue(HandleProperty); } set { SetValue(HandleProperty, value); } } // Using a DependencyProperty as the backing store for Hwnd. This enables animation, styling, binding, etc... public static readonly DependencyProperty HandleProperty = DependencyProperty.Register("Handle", typeof(IntPtr), typeof(NativeHost), new PropertyMetadata(IntPtr.Zero)); HwndSource _source; protected override HandleRef BuildWindowCore(HandleRef hwndParent) { _source = new HwndSource(0, WS_CHILD | WS_VISIBLE | LBS_NOTIFY, 0,0,0, (int)Width, (int)Height, "nativeHost", hwndParent.Handle); Handle = _source.Handle; return new HandleRef(this,Handle); } protected override void DestroyWindowCore(HandleRef hwnd) { _source.Dispose(); } const int WS_CHILD = 0x40000000; const int WS_VISIBLE = 0x10000000; const int LBS_NOTIFY = 0x001; }
直接在xaml中使用上述實現的控件:
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApp1" xmlns:interop="clr-namespace:System.Windows.Interop;assembly=PresentationFramework" mc:Ignorable="d" Title="MainWindow" Height="440" Width="640" > <Grid> <!--控件有個Handle屬性,可以綁定,使用OneWaytoSource賦值給viewModel--> <local:NativeHost x:Name="NH_Plane" Height="360" Width="640" ></local:NativeHost> </Grid> </Window>
在Loaded事件中才能獲取到句柄,在此事件之前句柄還沒有生成。
private void Window_Loaded(object sender, RoutedEventArgs e) { //獲取控件句柄 var hwnd=NH_Plane.Handle //通過句柄進行渲染 }
示例代碼:
https://download.csdn.net/download/u013113678/40304426
注:示例代碼與文本所有代碼基本一致,渲染部分在c++的dll不可見,請根據需要下載。
效果預覽:
以上是“C#中wpf如何通過HwndHost渲染視頻”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。