Get the App
SLTechnology News&Howtos  ›  Development  › 

How to render video through HwndHost by wpf in C #

Shulou Source: shulou.com Published: 2022-06-02 15:27:41 10月04日 Update

This article mainly shows you "how wpf in C# renders video through HwndHost", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to render video through HwndHost in wpf in C#".

First, how to realize

By inheriting HwndHost and implementing abstract methods, you can use it in xaml as a wpf control with a handle, as follows:

Win32Api version:

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 = 0x100000000; 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 version:

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, (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 = 0x100000000; const int LBS_NOTIFY = 0x001;} II. Usage

Use the controls implemented above directly in xaml:

The handle is only obtained in the Loaded event, which has not been generated before this event.

Private void Window_Loaded (object sender, RoutedEventArgs e) {/ / get control handle var hwnd=NH_Plane.Handle / / render via handle} 3. Example

Sample code:

Https://download.csdn.net/download/u013113678/40304426

Note: the sample code is basically the same as all the code in the text. The rendering part is not visible in the dll of C++. Please download it as needed.

Effect Preview:

The above is all the content of the article "how to render video through HwndHost in wpf in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Handles code video clips content controls examples articles events versions learning help consistency effects text ways methods easy to understand more organized Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno Apple Microsoft Shulou Tech Info