最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解C#?wpf如何嵌入hwnd窗口

 更新時間:2024年03月27日 08:42:24   作者:CodeOfCC  
wpf是Direct?UI,窗口中只有一個hwnd句柄,大部分控件都是直接在上面繪制的,本文主要來和大家講講如何嵌入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窗口

如果不想導(dǎo)入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窗口也可以進行嵌入,但需要導(dǎo)入win32對窗口屬性進行設(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é)

通過HwndHost的方式嵌入hwnd窗口是比較簡單易用的,而且也為wpf實現(xiàn)的界面效果提供的更多的可能性,當然嵌入的窗口會覆蓋wpf控件,雖然有解決的方法,本文主要還是提供基礎(chǔ)的HwndHost用法。

到此這篇關(guān)于詳解C# wpf如何嵌入hwnd窗口的文章就介紹到這了,更多相關(guān)wpf嵌入hwnd窗口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#實現(xiàn)兩個DocumentDB實例之間同步數(shù)據(jù)的解決方案

    C#實現(xiàn)兩個DocumentDB實例之間同步數(shù)據(jù)的解決方案

    這篇文章主要為大家詳細介紹了C#實現(xiàn)兩個DocumentDB實例之間同步數(shù)據(jù)的解決方案,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2026-02-02
  • 解析c# yield關(guān)鍵字

    解析c# yield關(guān)鍵字

    這篇文章主要介紹了c# yield關(guān)鍵字的相關(guān)資料,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-08-08
  • C#異常處理詳解

    C#異常處理詳解

    這篇文章介紹了C#異常處理,有需要的朋友可以參考一下
    2013-10-10
  • C#如何自定義線性節(jié)點鏈表集合

    C#如何自定義線性節(jié)點鏈表集合

    C#如何自定義線性節(jié)點鏈表集合,這篇文章主要為大家詳細介紹了C#基于泛型的自定義線性節(jié)點鏈表集合示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • c#中合并DataTable重復(fù)行的值

    c#中合并DataTable重復(fù)行的值

    c#中合并DataTable重復(fù)行的值,需要的朋友可以參考一下
    2013-05-05
  • 使用C#設(shè)置Word文檔背景顏色或背景圖片

    使用C#設(shè)置Word文檔背景顏色或背景圖片

    在 .NET 開發(fā)場景中,對 Word 文檔進行自動化格式處理是常見需求,其中設(shè)置文檔背景提升文檔視覺呈現(xiàn)的基礎(chǔ)操作,本文將介紹如何通過該組件在 C# 中為 Word 文檔設(shè)置背景顏色或背景圖片,需要的朋友可以參考下
    2026-02-02
  • Unity實現(xiàn)主角移動與攝像機跟隨

    Unity實現(xiàn)主角移動與攝像機跟隨

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)主角移動與攝像機跟隨,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • C#?List生成Txt文檔并且讀取Txt文檔封裝List

    C#?List生成Txt文檔并且讀取Txt文檔封裝List

    這篇文章主要介紹了C#?List生成Txt文檔并且讀取Txt文檔封裝List,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-08-08
  • 使用C#刪除Excel表格中的重復(fù)行數(shù)據(jù)的代碼詳解

    使用C#刪除Excel表格中的重復(fù)行數(shù)據(jù)的代碼詳解

    重復(fù)行是指在Excel表格中完全相同的多行數(shù)據(jù),刪除這些重復(fù)行至關(guān)重要,因為它們不僅會干擾數(shù)據(jù)分析,還可能導(dǎo)致錯誤的決策和結(jié)論,所以本文給大家介紹了如何使用C#快速刪除Excel表格中的重復(fù)行數(shù)據(jù),需要的朋友可以參考下
    2025-05-05
  • C#根據(jù)前臺傳入實體名稱實現(xiàn)動態(tài)查詢數(shù)據(jù)

    C#根據(jù)前臺傳入實體名稱實現(xiàn)動態(tài)查詢數(shù)據(jù)

    這篇文章主要為大家詳細介紹了C#如何根據(jù)前臺傳入實體名稱實現(xiàn)動態(tài)查詢數(shù)據(jù)的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-04-04

最新評論

扶沟县| 资兴市| 巴里| 卢龙县| 酒泉市| 安达市| 大安市| 西宁市| 凤凰县| 隆昌县| 镇原县| 宜丰县| 芮城县| 习水县| 株洲市| 汝南县| 许昌市| 饶河县| 宝鸡市| 崇明县| 玉林市| 丹阳市| 布尔津县| 包头市| 乐昌市| 长春市| 峨边| 依兰县| 红原县| 武山县| 陇川县| 霍城县| 昌江| 衡山县| 临武县| 久治县| 临颍县| 遂昌县| 洛阳市| 万年县| 兴安盟|