詳解C#?wpf如何嵌入hwnd窗口
前言
wpf是Direct UI,窗口中只有一個hwnd句柄,大部分控件都是直接在上面繪制的。當我們需要使用不同的渲染方式進行繪制時,就會和控件繪制產(chǎn)生沖突。比如使用opengl渲染3d圖形或者視頻時,直接在窗口繪制就會出現(xiàn)閃爍,與控件相互覆蓋。要解決這個問題就需要,添加一個新的hwnd窗口或控件嵌入wpf窗口中,我們可以通過HwndHost就可以實現(xiàn)這樣的功能。
一、如何實現(xiàn)
1、繼承HwndHost
public class MyWindowHost : HwndHost
2、實現(xiàn)抽象方法
只需實現(xiàn)下列2個方法
protected override HandleRef BuildWindowCore(HandleRef hwndParent) { Handle =創(chuàng)建的窗口句柄 return new HandleRef(this, Handle); }
protected override void DestroyWindowCore(HandleRef hwnd) { hwnd.Handle;//根據(jù)句柄銷毀窗口 }
3、xaml中使用HwndHost控件
<local:MyWindowHost Width="100" Height="100" > </local:MyWindowHost >
二、具體實現(xiàn)
1、Win32窗口
我們可以通過win32 api創(chuàng)建一個窗口,封裝成HwndHost對象,提供給xaml使用。
Win32WindowHost.cs
using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; namespace WpfHwndElement { /// <summary> /// 直接通過win32 api創(chuàng)建窗口 /// </summary> public class Win32WindowHost : HwndHost { //重新定義Handle為依賴屬性,可以用于綁定 new public IntPtr Handle { get { return (IntPtr)GetValue(HandleProperty); } private 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(Win32WindowHost), new PropertyMetadata(IntPtr.Zero)); protected override HandleRef BuildWindowCore(HandleRef hwndParent) { Handle = CreateWindowEx(0, "static", "", WS_CHILD | WS_VISIBLE | LBS_NOTIFY | WS_CLIPSIBLINGS, 0, 0, (int)Width, (int)Height, hwndParent.Handle, IntPtr.Zero, IntPtr.Zero, 0); return new HandleRef(this, Handle); } [DllImport("user32.dll", SetLastError = true)] static extern System.IntPtr DefWindowProcW(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); protected override void DestroyWindowCore(HandleRef hwnd) { DestroyWindow(hwnd.Handle); } const int WS_CHILD = 0x40000000; const int WS_VISIBLE = 0x10000000; const int LBS_NOTIFY = 0x001; const int WS_CLIPSIBLINGS = 0x04000000; [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); } }
2、HwndSource窗口
如果不想導入win32 api,則可以使用HwndSource對象創(chuàng)建句柄窗口。
using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; ???????namespace WpfHwndElement { class HwndSourceHost : HwndHost { //重新定義Handle為依賴屬性,可以用于綁定 new public IntPtr Handle { get { return (IntPtr)GetValue(HandleProperty); } private 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(HwndSourceHost), new PropertyMetadata(IntPtr.Zero)); HwndSource _source; protected override HandleRef BuildWindowCore(HandleRef hwndParent) { _source = new HwndSource(0, WS_CHILD | WS_VISIBLE | LBS_NOTIFY| WS_CLIPSIBLINGS, 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; const int WS_CLIPSIBLINGS = 0x04000000; } }
3、Wpf窗口
wpf窗口也可以進行嵌入,但需要導入win32對窗口屬性進行設置,要設置WS_CHILD 以及父窗口。
using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; namespace WpfHwndElement { //重新定義Handle為依賴屬性,可以用于綁定 public class WpfWindowHost : HwndHost { new public IntPtr Handle { get { return (IntPtr)GetValue(HandleProperty); } private 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(WpfWindowHost), new PropertyMetadata(IntPtr.Zero)); const int WS_CHILD = 0x40000000; const int GWL_STYLE = (-16); [DllImport("user32.dll", EntryPoint = "GetWindowLongW")] static extern int GetWindowLong(IntPtr hwnd, int nIndex); [DllImport("user32.dll", EntryPoint = "SetWindowLongW")] static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong); [DllImport("user32.dll")] public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); protected override HandleRef BuildWindowCore(HandleRef hwndParent) { var window = new Window(); var hwnd = new WindowInteropHelper(window).EnsureHandle(); window.Show(); SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) | WS_CHILD); SetParent(hwnd, hwndParent.Handle); return new HandleRef(this, hwnd); } ??????? protected override void DestroyWindowCore(HandleRef hwnd) { var window = HwndSource.FromHwnd(hwnd.Handle)?.RootVisual as Window; window?.Close(); } } }
三、使用示例
MainWindow.xaml
<Window x:Class="WpfHwndElement.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:local="clr-namespace:WpfHwndElement" mc:Ignorable="d" Title="MainWindow" Height="360" Width="640" > <StackPanel> <local:Win32WindowHost Width="100" Height="100"/> <local:HwndSourceHost Margin="0,10,0,0" Width="100" Height="100"/> <local:WpfWindowHost Margin="0,10,0,0" Width="100" Height="100"/> </StackPanel> </Window>
效果預覽
總結(jié)
通過HwndHost的方式嵌入hwnd窗口是比較簡單易用的,而且也為wpf實現(xiàn)的界面效果提供的更多的可能性,當然嵌入的窗口會覆蓋wpf控件,雖然有解決的方法,本文主要還是提供基礎的HwndHost用法。
到此這篇關于詳解C# wpf如何嵌入hwnd窗口的文章就介紹到這了,更多相關wpf嵌入hwnd窗口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于C#實現(xiàn)WinForm開發(fā)操作系統(tǒng)的文件管理系統(tǒng)代碼
基于C#的WinForm應用程序來模擬操作系統(tǒng)文件管理系統(tǒng),可以幫助用戶在Windows環(huán)境下進行文件的創(chuàng)建、移動、刪除與搜索等操作,這種模擬工具有助于學習文件系統(tǒng)的工作原理以及測試和開發(fā)其他軟件項目2024-12-12詳解.NET 4.0中的泛型協(xié)變(covariant)和反變(contravariant)
這篇文章主要介紹了詳解.NET 4.0中的泛型協(xié)變(covariant)和反變(contravariant),本文講解了協(xié)變和反變的背景知識、.NET 4.0引入的泛型協(xié)變、反變性、協(xié)變和反變的相互作用等內(nèi)容,需要的朋友可以參考下2015-06-06