在WPF中使用WindowsFormsHost带来的问题及解决方案

首先明确指出的是你要解决的问题没有可行的解决方案,然后你还有兴趣的话,可以继续往下看。

继续往下看的程序员都是好程序员!!!

本人开发环境:VS2013 && .NET 4.5.1

问题:在wpf中嵌入WindowsFormsHost后,WINFORM的元素都在最顶层,所以要在TABCONTROL中切来切去的时候,会发现不管咋切winform的元素都处于显示状态,就算当你想用SCROLLVIEWER包一下,对不起,当你使用滚动条的时候,winform元素甚至连你的滚动条都 遮挡了。

对于此问题,无解!无解!无解!

首先看一下MS的说法:

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

意思就是说,WINFORM元素一直都在最顶层,这是设计的限制。
一直到.NET FRAMEWORK 4.0时候都是这个状况。

再看看MS对这个问题的尝试

A year later, things have changed a bit with .NET 4.5. For those who stumbled upon this, much as I did, here is a more updated excerpt from Walkthrough: Arranging Windows Forms Controls in WPF on MSDN:


By default, visible WindowsFormsHost elements are always drawn on top of other WPF elements, and they are unaffected by z-order. To enable z-ordering, set the IsRedirected property of the WindowsFormsHost to true and the CompositionMode property to Full or OutputOnly.
All you need to do, when using .NET 4.5, is add the following attributes to your WindowsFormsHost element IsRedirected="True" and CompositionMode="Full" or CompositionMode="OutputOnly".

在.net 4.5,微软尝试解决这个问题,就是添加几个属性的事。这是个大坑,也不知道到底解决的如何,此功能仅限于4.5 BETA版,在正式版里,又没有这两个属性了。
(微软自己都解决不了的问题,真尴尬!!!)

再看看MS的新说法

1.3.10 Windows Presentation Foundation (WPF) 1.3.10.1 HwndHost feature has been removed from WPF in the .NET Framework 4.5 Beta The .NET Framework 4.5 Developer Preview included a WPF HwndHost redirection feature. However, this feature had several known issues and has been removed from the .NET Framework 4.5 Beta. It will not be included in any future releases. To resolve this issue: No workaround is available.

意思是说:
1.3.10WindowsPresentationFoundation(WPF)1.3.10.1HwndHost特性已被删除从WPF。净框架4.5Beta。净框架4.5开发人员预览版包括一个WPFHwndHost重定向功能。然而,这个功能有几个已知问题和已被删除。净框架4.5Beta。它将不会被包括在任何未来的版本。解决这一问题:没有可用的解决方法

总之就是无解,无解,无解!!!

以下是一些链接,也是说的相同问题

https://social.msdn.microsoft.com/Forums/vstudio/en-US/8183e167-abfc-49c2-9a0c-05b5ddf2bf30/windowsformshost-always-topmost?forum=wpf

https://social.msdn.microsoft.com/Forums/vstudio/en-US/8183e167-abfc-49c2-9a0c-05b5ddf2bf30/windowsformshost-always-topmost?forum=wpf

http://dolinked.com/questions/1275873/windowsformshost-is-always-the-most-top-from-wpf-element

https://msdn.microsoft.com/zh-cn/library/ms752027(v=vs.110).aspx

猜你喜欢

转载自blog.csdn.net/wanglei9876/article/details/50720870