WindowsFormsHost使用问题

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

WindowsFormsHost使用问题的更多相关文章

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

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

  2. WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案

    首先先得瑟一下,有关WPF中不规则窗体与WindowsFormsHost控件不兼容的问题,网上给出的解决方案不能满足所有的情况,是有特定条件的,比如  WPF中不规则窗体与WebBrowser控件的兼 ...

  3. WindowsFormsHost下MouseWheel失效的解决办法

    原文:WindowsFormsHost下MouseWheel失效的解决办法 看了网上有些写的用钩子,但是,在Stack Overflow上找到一个简便的方式

  4. 多个 WindowsFormsHost 叠加顺序调整

    原文:多个 WindowsFormsHost 叠加顺序调整 工作中遇到多个 WindowsFormsHost 包装的控件叠加顺序的调整问题,用了 BingToFront 和 BringToBack,不 ...

  5. wpf怎么使用WindowsFormsHost(即winform控件)

    原文:wpf怎么使用WindowsFormsHost(即winform控件) 使用方法: 1.首先,我们需要向项目中的引用(reference)中添加两个动态库dll,一个是.NET库中的System ...

  6. WPF解决WindowsFormsHost背景透明

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

  7. windowsformshost mouse event not transmit to it's parent control

    in the case you can do it to fix: MouseEventArgs e = new MouseEventArgs(Mouse.PrimaryDevice, 0); e.R ...

  8. 【WPF】 通过FarPoint显示Excel

    1.FarPoint 只支持winform,在Wpf中要引用:WindowsFormsIntegration.dll2.*.xaml文件引用    xmlns:wfi ="clr-names ...

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

    1.      首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2.      在要使用WinForm控 ...

随机推荐

  1. Java EE 编程中路径

    版权声明:未经博主允许,不得转载 首先我们要限定一个范围,是一个项目,或是以个访问地址..就先以一个项目为限定的范围 前述: 学过物理学的都知道相对运动和绝对运动, 虽然是相似的概念,但这里的要简单得 ...

  2. Android RadioButton 语言无法切换问题

    1.Dialog在不退出界面的情况下,RadioButton在语言切换时,无法匹配系统语言的问题: 解决办法为:在RadioButton添加属性 android:saveEnabled="f ...

  3. Qt线程(4) 降低线程占用CPU

    问题描述: 一般将计算量大的处理过程单独放置到一个单独的线程处理,因此很有可能你的处理过程需要while(1)或类似的操作. 也因此很有可能造成线程在处理时计算机CPU占用过高的情况. 解决办法: 降 ...

  4. PHPMyAdmin弱口令猜解【Python脚本】

    PHPMyAdmin弱口令猜解 测试截图: 代码片段 #! /usr/bin/env python # _*_ coding:utf-8 _*_ import requests import time ...

  5. 初学JAVA的 感想 尹鑫磊

    开始学习任何一门课(包括java),兴趣最重要.一直觉得自己在学计算机编程语言,学习了很多,但每门语言都停留在知识边缘地带,都没深入到它们的精华部分,对它们的精华部分知之甚少,于是趁学校开设这门课,并 ...

  6. 第六篇T语言实例开发,多点找色应用

    ---恢复内容开始--- 多点找色应用 文字,图形特征的获取 多点找色 功能原型 窗口多点找色(窗口句柄,x1,y1,x2,y2,颜色值,色点组,相似度,方向,返回x,返回y) 功能说明 根据指定的多 ...

  7. nohup使用(转)

    在启动weblogic的时候我们经常看到如下的命令: nohup ./startWebLogic.sh >out.log 2>&1 & 其中 0.1.2分别代表如下含义:  ...

  8. algorithm 学习之 for_each

    对于algorithm里面的函数使用不算多,但是用过之后才发现,之前写过很多多余的代码,所以打算系统的学习使用下algorithm里的东西,首先就是for_each. 先看下for_each的定义: ...

  9. chosen组件实现下拉框

    chosen组件用于增强原生的select控件,使之有更好的用户体验.官方demo https://harvesthq.github.io/chosen/ 目前项目中碰到的使用,比如一个页面中有两个不 ...

  10. 远程调试js注意事项

    1:使用host切换工具,先注释掉93服务器的地址,打开链接,点击高级选项,进去后登陆账号密码(如果不行重启浏览器): 2:进入后,增加93服务器上的host地址,重启浏览器,css样式生效: 3:使 ...