欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解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窗口

如果不想導入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)的Excel文件操作類實例

    C#實現(xiàn)的Excel文件操作類實例

    這篇文章主要介紹了C#實現(xiàn)的Excel文件操作類,結(jié)合具體實例形式分析了C#數(shù)據(jù)庫及Excel文件相關操作技巧,需要的朋友可以參考下
    2017-06-06
  • c# 開發(fā)語音識別程序

    c# 開發(fā)語音識別程序

    這篇文章主要介紹了用c# 開發(fā)語音識別程序,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-09-09
  • c#實現(xiàn)爬蟲程序

    c#實現(xiàn)爬蟲程序

    這篇文章介紹了c#實現(xiàn)爬蟲程序的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 基于C#實現(xiàn)WinForm開發(fā)操作系統(tǒng)的文件管理系統(tǒng)代碼

    基于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
  • unity 切換場景不銷毀物體問題的解決

    unity 切換場景不銷毀物體問題的解決

    這篇文章主要介紹了unity 切換場景不銷毀物體問題的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 詳解.NET 4.0中的泛型協(xié)變(covariant)和反變(contravariant)

    詳解.NET 4.0中的泛型協(xié)變(covariant)和反變(contravariant)

    這篇文章主要介紹了詳解.NET 4.0中的泛型協(xié)變(covariant)和反變(contravariant),本文講解了協(xié)變和反變的背景知識、.NET 4.0引入的泛型協(xié)變、反變性、協(xié)變和反變的相互作用等內(nèi)容,需要的朋友可以參考下
    2015-06-06
  • C#實現(xiàn)貝塞爾曲線的方法

    C#實現(xiàn)貝塞爾曲線的方法

    這篇文章主要為大家詳細介紹了C#實現(xiàn)貝塞爾曲線的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 基于Unity實現(xiàn)3D版2048游戲的示例代碼

    基于Unity實現(xiàn)3D版2048游戲的示例代碼

    這篇文章主要為大家詳細介紹了如何利用Unity實現(xiàn)簡易的3D版2048游戲,文中的示例代碼講解詳細,具有一定的學習價值,需要的可以參考一下
    2023-02-02
  • 深入理解c# checked unchecked 關鍵字

    深入理解c# checked unchecked 關鍵字

    本篇文章是對c#中的checked unchecked 關鍵字進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C#中的Dialog對話框

    C#中的Dialog對話框

    這篇文章介紹了C#中的Dialog對話框,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05

最新評論