这篇文章主要为大家展示了“C#中WPF如何通过HwndHost渲染视频”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“C#中wpf如何通过HwndHost渲染视频”这篇文章吧。一、如何实现通过继承
这篇文章主要为大家展示了“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渲染视频”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!
--结束END--
本文标题: C#中wpf如何通过HwndHost渲染视频
本文链接: https://lsjlt.com/news/305534.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0