前言
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窗口内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论