在WPF中内嵌WindowsForm控件-使用WindowsFormsControlLibrary

   

     原来有一个WINFORM项目的功能模块希望集成到新的WPF项目中,怎样集成才最简单?

    思路:将原来的WINFORM项目类型改为WindowsFormsControlLibrary类型就OK了。

步骤:

   1、所以我们就直接建立一个WindowsFormsControlLibrary项目吧!接着我在该项目中新增Windows Form,为Form1。也就是将原来的项目类型改造为WindowsFormsControlLibrary项目。

    




2新建Wpf项目

(1)、添加两个引用:WindowsFormsIntegration.dll(负责整合WPF和Windows)、System.Windows.Forms.

  (2)、在 XAML文件中添加两个引用(粗体部分):

<Window x:Class="WpfApplication1.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"
        Title="MainWindow" Height="350" Width="525">

(3)添加HOST宿主

<Window x:Class="WpfApplication1.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"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
           
        </Grid.ColumnDefinitions>
        <WindowsFormsHost Name="windowsFormsHost1" Grid.Column="0" Margin="3"></WindowsFormsHost>
    </Grid>
   
</Window>

windowsFormsHost1 就是FORM窗体显示的宿主

(4)接着我们要在WPF项目中引用刚才WindowsFormsControlLibrary项目建出来的dll档

(5)在MainWindow.xaml.cs中添加代码

 

using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WindowsFormsControlLibrary1;
using System.Windows.Forms;


namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Form1 _form1 = new Form1();
        public MainWindow()
        {
            InitializeComponent();
            _form1.TopLevel  = false;
            _form1.FormBorderStyle = FormBorderStyle.None;
            windowsFormsHost1.Child = _form1;


        }
    }
}


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值