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

 更新时间:2024年03月27日 08:42:24   作者:CodeOfCC  
wpf是Direct?UI,窗口中只有一个hwnd句柄,大部分控件都是直接在上面绘制的,本文主要来和大家讲讲如何嵌入hwnd窗口,感兴趣的可以了解一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

前言

wpf是Direct UI,窗口中只有一个hwnd句柄,大部分控件都是直接在上面绘制的。当我们需要使用不同的渲染方式进行绘制时,就会和控件绘制产生冲突。比如使用opengl渲染3d图形或者视频时,直接在窗口绘制就会出现闪烁,与控件相互覆盖。要解决这个问题就需要,添加一个新的hwnd窗口或控件嵌入wpf窗口中,我们可以通过HwndHost就可以实现这样的功能。

一、如何实现

1、继承HwndHost

public class MyWindowHost : HwndHost

2、实现抽象方法

只需实现下列2个方法

protected override HandleRef BuildWindowCore(HandleRef hwndParent)
{
    Handle =创建的窗口句柄
    return new HandleRef(this, Handle);
}
protected override void DestroyWindowCore(HandleRef hwnd)
{
   hwnd.Handle;//根据句柄销毁窗口
}

3、xaml中使用HwndHost控件

<local:MyWindowHost Width="100" Height="100" >
</local:MyWindowHost >

二、具体实现

1、Win32窗口

我们可以通过win32 api创建一个窗口,封装成HwndHost对象,提供给xaml使用。

Win32WindowHost.cs

using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace WpfHwndElement
{
    /// <summary>
    /// 直接通过win32 api创建窗口
    /// </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对象创建句柄窗口。

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>

效果预览

总结

通过HwndHost的方式嵌入hwnd窗口是比较简单易用的,而且也为wpf实现的界面效果提供的更多的可能性,当然嵌入的窗口会覆盖wpf控件,虽然有解决的方法,本文主要还是提供基础的HwndHost用法。

到此这篇关于详解C# wpf如何嵌入hwnd窗口的文章就介绍到这了,更多相关wpf嵌入hwnd窗口内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!

相关文章

  • C#多线程学习之(二)操纵一个线程的方法

    C#多线程学习之(二)操纵一个线程的方法

    这篇文章主要介绍了C#多线程学习之操纵一个线程的方法,实例分析了C#中线程的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • c#中WebService的介绍及调用方式小结

    c#中WebService的介绍及调用方式小结

    这篇文章主要给大家介绍了关于c#中的WebService及其调用方式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • C# 命名空间(Namespace)相关知识总结

    C# 命名空间(Namespace)相关知识总结

    这篇文章主要介绍了C# 命名空间(Namespace)的相关知识,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以参考下
    2020-06-06
  • C#实现单例模式的多种方式

    C#实现单例模式的多种方式

    这篇文章介绍了C#实现单例模式的多种方式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • C#中使用委托的3种方式代码示例

    C#中使用委托的3种方式代码示例

    这篇文章主要介绍了C#中使用委托的3种方式代码示例,本文直接给出代码实例,没有相关的说明解释,需要的朋友可以参考下
    2015-03-03
  • C#数据适配器DataAdapter

    C#数据适配器DataAdapter

    这篇文章介绍了C#中的数据适配器DataAdapter,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • C#实现鼠标移动到曲线图上显示值的方法

    C#实现鼠标移动到曲线图上显示值的方法

    这篇文章主要介绍了C#实现鼠标移动到曲线图上显示值的方法,是C#的WinForm窗体程序设计中非常实用的技巧,需要的朋友可以参考下
    2014-10-10
  • Unity实现本地文本多语言化

    Unity实现本地文本多语言化

    这篇文章主要为大家详细介绍了Unity实现本地文本多语言化,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • C#实现打开指定目录和指定文件的示例代码

    C#实现打开指定目录和指定文件的示例代码

    这篇文章主要为大家详细介绍了如何利用C#实现打开指定目录、打开指定目录且选中指定文件、打开指定文件,感兴趣的小伙伴可以尝试一下
    2022-06-06
  • 如何最大限度地降低多线程C#代码的复杂性

    如何最大限度地降低多线程C#代码的复杂性

    这篇文章主要介绍了如何最大限度地降低多线程C#代码的复杂性,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03

最新评论

?


http://www.vxiaotou.com