WPF WindowsFormsHost 类

允许在 WPF 页面上承载 Windows Forms控件的元素。

命名空间:  System.Windows.Forms.Integration
程序集:  WindowsFormsIntegration(在 WindowsFormsIntegration.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

简单介绍在wpf程序中整合windows form:

1.在references中添加WindowsFormsIntegration和System.Windows.Forms。

2.在xaml中使用的时候要写清楚名字空间,可以把这两个ns定义出来。

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

插入WindowsFormsControl:

<wfi:WindowsFormsHost>
            <wf:DateTimePicker/>
</wfi:WindowsFormsHost>

在WPF中添加Windows Form控件

首先,需要向项目中的reference添加两个dll,一个是.NET库中的System.Windows.Forms,另外一个是WindowsFormsIntegration,它的位置一般是在C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF 里。

添加完两个dll以后,就可以在控件库中找到WindowsFormsHost这个控件了。这个控件是我们添加Windows Form控件的基础。跟别的其他的控件一样,它也是可控的,可以自定义它在窗口中的位置、控件大小颜色等属性。我一般是比较喜欢在Blend里面创建控件。可以在Blend中的Assets中找到这个控件。或者你也可以在vs中的设计模式下的toolbox中找到它。放置完以后在xmal代码中会自动生成相应代码:

<WindowsFormsHost Height="196" HorizontalAlignment="Left" Margin="104,65,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="286"/>

然后,需要在xmal的开始处添加两行代码

xmlns:WinFormHost="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"xmlns:WinFormControls="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

这样就可以在WindowsFormsHost下放置需要的Windows Form控件了,比如

<WindowsFormsHost Height="196" HorizontalAlignment="Left" Margin="104,65,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="286"><WinFormControls:Button Text="WinformButton" Width="150"/></WindowsFormsHost>

这是最简单的情况,就是添加了一个button,运行以后会发现整个WindowsFormsHost上就放置了一个硕大的button……如果需要有布局的可以在WindowsFormsHost下放置Panel等布局控件。

最后附上整个xmal代码

<Window x:Class="WpfApplication2.MainWindow"        xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation        xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml        xmlns:WinFormHost="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"        xmlns:WinFormControls="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"        Title="MainWindow" Height="350" Width="525"><Grid><WindowsFormsHost Height="196" HorizontalAlignment="Left" Margin="104,65,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="286"><WinFormControls:Button Text="WinformButton" Width="150"/></WindowsFormsHost></Grid></Window>

附上一个有用的链接,如果想做响应的朋友可以参考一下

http://www.dotblogs.com.tw/ouch1978/archive/2011/01/03/wpf_windowsformsintegration.aspx

另外在wpf中使用winform控件会存在控件叠放层次问题

示例:

有一个第三方控件,WinForm的,我要在WPF里面使用它,用WindowsFormsHost可以实现。
现在需要在这个控件的底部附加一个半透明栏,显示一些文字和几个按钮。但由于WindowsFormsHost是默认置顶的,不能像WPF控件那样层叠实现。考虑过用Popup来实现覆盖到WindowsFormsHost上面,但Popup会把弹出窗口(如MessageBox等)覆盖掉,网上有一个自定义Popup部分解决这个问题,但是一个窗口中使用多个Popup的话其他Popup会被隐藏掉。

解决方案1

<Grid>
<wfi:WindowsFormsHost><你的控件>
</wfi:WindowsFormsHost>
<wfi:WindowsFormsHost Margin="10" Height="30" Width="100"><ElementHost><你要加的内容></ElementHost>
</wfi:WindowsFormsHost>
</Grid>

把你要附加的WPF控件也包装成Winform控件,这样你的Winform控件就不会覆盖掉你附加的那个半透明栏了。

解决方案2

用代码动态的来重绘winform控件实现

解决方案3

用Popup承载这些元素。

WPF:
<Window [stuff]
   LocationChanged="Window_LocationChanged"
   SizeChanged="Window_SizeChanged"
>
   <Grid Name="Player">
   [same code as before]
       <Popup Name="toolbar_popup" IsOpen="True" PlacementTarget="{Binding ElementName=host}">
           [toolbar grid goes here]
       </Popup>
   </Grid>
< /Window>

C#

private void resetPopup()
{
   // Update position
   // http://stackoverflow.com/a/2466030/865883
   var offset = toolbar_popup.HorizontalOffset;
   toolbar_popup.HorizontalOffset = offset + 1;
   toolbar_popup.HorizontalOffset = offset;

// Resizing
   toolbar_popup.Width = Player.ActualWidth;
   toolbar_popup.PlacementRectangle = new Rect(0, host.ActualHeight, 0, 0);
   toolbar_popup.Placement = System.Windows.Controls.Primitives.PlacementMode.Top;
}
private void Window_LocationChanged(object sender, EventArgs e)
{ resetPopup(); }

private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{ resetPopup(); }

为什么会出现这样的问题?

WindowsFormsHost is always the most top from WPF element

 

According to MSDN (Layout Considerations for the WindowsFormsHost Element)

A hosted Windows Forms control is drawn in a separate HWND, so it is always drawn on top of WPF elements.

This is a design limitation

Reference:

WindowsFormsHost 元素的布局注意事项

MouseWheel in WindowsFormsHost(鼠标滚轮事件) 

在wpf应用程序中有WindowsFormsHost,可以使用wpf UI与winform或者 win32窗口进行交互,但是实际上,对于键盘事件及鼠标部分事件(比如鼠标滚轮事件),是无法获取到的,如下所提的

http://social.msdn.microsoft.com/Forums/en/wpf/thread/2f927aa6-834a-41a3-affa-7188377f71cf

转载于:https://www.cnblogs.com/Daywei/archive/2013/04/11/3015557.html

WindowsFormsHost使用问题相关推荐

  1. 关于wpf窗体中Allowtransparent和WindowsFormsHost的纠纷

    最近有个项目需要在wpf中浏览pdf文档,所以就采用了Adobe Reader 的com组件(该组件只能用在winform中,幸好wpf里可以通过WindowsFormsHost寄宿winform程序 ...

  2. WPF解决WindowsFormsHost背景透明

    项目案例:WPF使用WindowsFormsHost播放视频,视频上显示边框.字幕等特效: 难点问题 1.WindowsFormsHost不支持背景透明: 2.WPF Panel.ZIndex无效,W ...

  3. WPF 中如何使用第三方控件 ,可以使用WindowsFormsHost 类

    允许在 WPF 页面上承载 Windows Forms控件的元素. 命名空间:   System.Windows.Forms.Integration 程序集:   WindowsFormsIntegr ...

  4. WPF开发:WindowsFormsHost上浮动控件方法

    WindowsFormsHost上浮动控件方法 WindowsFormsHost是WPF中承载windows form类型的控件,它的优先级特别高,在同一个窗口上的其他类型控件都能被它盖在下边.TE的 ...

  5. mouseWheel in WindowsFormsHost(鼠标滚轮事件)

    在wpf应用程序中有WindowsFormsHost,可以使用wpf UI与winform或者 win32窗口进行交互,但是实际上,对于键盘事件及鼠标部分事件,是无法获取到的,如下所提的 http:/ ...

  6. WPF 浏览PDF 文件

    很长时间没写文章感觉手有点生了,前段时间忙的要死公事.家事.私事,事事操心.还好现在有些时间可以继续写博客了.本篇将为大家演示如何在WPF 程序中浏览PDF 文件,本例将通过Adobe PDF Rea ...

  7. 在WPF中使用WinForm控件方法

    在WPF中使用WinForm控件方法 原文:在WPF中使用WinForm控件方法 1.      首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,Syste ...

  8. WPF中用于嵌入其他进程窗口的自定义控件(AppContainer)

    原文:WPF中用于嵌入其他进程窗口的自定义控件(AppContainer) 版权声明:本文为博主原创文章,转载请注明作者和出处 https://blog.csdn.net/ZZZWWWPPP11199 ...

  9. 在Browser Application中使用XNA

    在WPF中,我们使用Mode3D等API来绘制三维场景,当期间的"三角形"超过一定数量时,整个场景的渲染速率直线下降,无论显卡的运行速度有多快,帧率都维持在3.5帧每秒. XNA是 ...

最新文章

  1. 历史上最伟大的方程 (托尼·赖斯 著)
  2. 重温强化学习之函数近似
  3. vue 事件总线EventBus的概念、使用以及注意点
  4. 【Python】疯狂的加速函数!
  5. CVPR 2019 | 无监督领域特定单图像去模糊
  6. 设置Spring 3开发环境
  7. 机器学习知识总结系列- 特征工程(1-1)
  8. 100万并发连接服务器笔记之测试端就绪
  9. 夺命雷公狗ThinkPHP项目之----企业网站13之文章列表页的实现(主要是分页的实现)...
  10. 2020移动应用设计流行素材|交互动画
  11. python 百度识图_用python做图片识别(调用百度API)
  12. Node.js TLSWrap 实现中的释放后使用漏洞分析
  13. matlab图像处理宝典 秦襄培,秦襄培
  14. 50年过去了,嫦娥五号探月依然不能直播,告诉你三个可能
  15. matlab画图 横坐标为年月
  16. python的线程池_python线程池 ThreadPoolExecutor 的用法示例
  17. word中图片为嵌入式格式时显示不全_word插入图片嵌入式 word图片显示不全
  18. 离线地图数据tpk的制作
  19. Pyhton零基础投喂(综合练习:2:论⽂作者统计)
  20. 惠普暗影精灵8和惠普暗影精灵8 Plus 评测

热门文章

  1. Synchronized关键字和锁升级
  2. STM32开发 -- 系统软复位
  3. java 颜色比较_我该如何比较Java中的颜色?
  4. data spring 指定时区_Spring 框架基础(05):Mvc架构模式,执行流程详解
  5. Python之区块链入门,揭秘比特币
  6. Android后台杀死系列之二:ActivityManagerService与App现场恢复机制
  7. Android开发工具之Android Studio----版本控制SVN使用(一)
  8. java 多线程数量_java多线程之计算数量
  9. 卸载后清理干净_想要清理你的Mac?选这几款软件就对了
  10. python enumerate用法_【Python】python enumerate用法总结