詳解C#?wpf如何嵌入hwnd窗口
前言
wpf是Direct UI,窗口中只有一個(gè)hwnd句柄,大部分控件都是直接在上面繪制的。當(dāng)我們需要使用不同的渲染方式進(jìn)行繪制時(shí),就會(huì)和控件繪制產(chǎn)生沖突。比如使用opengl渲染3d圖形或者視頻時(shí),直接在窗口繪制就會(huì)出現(xiàn)閃爍,與控件相互覆蓋。要解決這個(gè)問(wèn)題就需要,添加一個(gè)新的hwnd窗口或控件嵌入wpf窗口中,我們可以通過(guò)HwndHost就可以實(shí)現(xiàn)這樣的功能。
一、如何實(shí)現(xiàn)
1、繼承HwndHost
public class MyWindowHost : HwndHost
2、實(shí)現(xiàn)抽象方法
只需實(shí)現(xiàn)下列2個(gè)方法
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 >
二、具體實(shí)現(xiàn)
1、Win32窗口
我們可以通過(guò)win32 api創(chuàng)建一個(gè)窗口,封裝成HwndHost對(duì)象,提供給xaml使用。
Win32WindowHost.cs
using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; namespace WpfHwndElement { /// <summary> /// 直接通過(guò)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窗口
如果不想導(dǎo)入win32 api,則可以使用HwndSource對(duì)象創(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窗口也可以進(jìn)行嵌入,但需要導(dǎo)入win32對(duì)窗口屬性進(jìn)行設(shè)置,要設(shè)置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>
效果預(yù)覽
總結(jié)
通過(guò)HwndHost的方式嵌入hwnd窗口是比較簡(jiǎn)單易用的,而且也為wpf實(shí)現(xiàn)的界面效果提供的更多的可能性,當(dāng)然嵌入的窗口會(huì)覆蓋wpf控件,雖然有解決的方法,本文主要還是提供基礎(chǔ)的HwndHost用法。
到此這篇關(guān)于詳解C# wpf如何嵌入hwnd窗口的文章就介紹到這了,更多相關(guān)wpf嵌入hwnd窗口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)的Excel文件操作類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的Excel文件操作類,結(jié)合具體實(shí)例形式分析了C#數(shù)據(jù)庫(kù)及Excel文件相關(guān)操作技巧,需要的朋友可以參考下2017-06-06基于C#實(shí)現(xiàn)WinForm開發(fā)操作系統(tǒng)的文件管理系統(tǒng)代碼
基于C#的WinForm應(yīng)用程序來(lái)模擬操作系統(tǒng)文件管理系統(tǒng),可以幫助用戶在Windows環(huán)境下進(jìn)行文件的創(chuàng)建、移動(dòng)、刪除與搜索等操作,這種模擬工具有助于學(xué)習(xí)文件系統(tǒng)的工作原理以及測(cè)試和開發(fā)其他軟件項(xiàng)目2024-12-12unity 切換場(chǎng)景不銷毀物體問(wèn)題的解決
這篇文章主要介紹了unity 切換場(chǎng)景不銷毀物體問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04詳解.NET 4.0中的泛型協(xié)變(covariant)和反變(contravariant)
這篇文章主要介紹了詳解.NET 4.0中的泛型協(xié)變(covariant)和反變(contravariant),本文講解了協(xié)變和反變的背景知識(shí)、.NET 4.0引入的泛型協(xié)變、反變性、協(xié)變和反變的相互作用等內(nèi)容,需要的朋友可以參考下2015-06-06基于Unity實(shí)現(xiàn)3D版2048游戲的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Unity實(shí)現(xiàn)簡(jiǎn)易的3D版2048游戲,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下2023-02-02深入理解c# checked unchecked 關(guān)鍵字
本篇文章是對(duì)c#中的checked unchecked 關(guān)鍵字進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05