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
?
轉載于:https://www.cnblogs.com/Daywei/archive/2013/04/11/3015557.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的WindowsFormsHost使用问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springmvc 实例应用
- 下一篇: 局域网聊天工具的设计与实现 java设