在WPF中驻留Windows窗体控件

在WPF中驻留Windows窗体控件

虽然在WPF中已经提供了很多元素可供我们使用,使我们能够编出一个完美的程序。但是当我们想用以前已经开发好的winform控件时,如果把它们转换成WPF控件,将会消耗很多精力和成本。尤其是使用的第三方控件是winform控件时,更加不可能。

解决方案

为在WPF中显示Windows窗体控件,需要使用System.Windows.Forms.Integration名称控件中的WidnowsFormsHost类。WindowsFormsHost类是WPF元素,该类可以包含Windows窗体控件,其中的Windows窗体控件是通过其Child属性提供的。创建的方法很简单,唯一的缺点是VS没有为WindowsFormsHost控件提供多少设计时支持。

实例

XAML代码

<Window x:Class="添加WinForm控件.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
   Title="MainWindow" Height="820" Width="824">
    <Border BorderBrush="#FFDE12D5" BorderThickness="5" Margin="10,0,2,5">
        <Grid Margin="10">
            <Grid.RowDefinitions>
                <RowDefinition Height="14*"/>
                <RowDefinition Height="15*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="247*"/>
                <ColumnDefinition Width="270*"/>
            </Grid.ColumnDefinitions>

            <WindowsFormsHost>
                <forms:MaskedTextBox  x:Name="mm" Text="haohao " Dock="Fill"></forms:MaskedTextBox>
            </WindowsFormsHost>
            <WindowsFormsHost Grid.Row="1"
                          Grid.Column="1" Margin="5,0,-6,0">
                <forms:PictureBox x:Name="pic" Dock="Fill" ImageLocation="c:\外星人.png" BorderStyle="Fixed3D"/>
            </WindowsFormsHost>
            <WindowsFormsHost Grid.Row="1"
                          Grid.Column="0" Margin="5,0,-6,0">
                <forms:PictureBox x:Name="pic1" Dock="Fill" ImageLocation="c:\外星人.png" BorderStyle="Fixed3D" SizeMode="StretchImage"/>
            </WindowsFormsHost>
            <Image x:Name="image1" Grid.Column="1" Margin="5,5,0,0" />

        </Grid>
    </Border>
</Window>

C#代码

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Media.Imaging;
using Timer = System.Windows.Forms.Timer;

namespace 添加WinForm控件
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly Timer _time = new Timer();

        public MainWindow()
        {

            InitializeComponent();
            //加载本地图片
            pic.Image = System.Drawing.Image.FromFile(@"c:\外星人.png");
            //加载网络图片
            pic1.ImageLocation = @"http://pic17.nipic.com/20111026/8686680_140506586159_2.jpg";

            //使用WPF自带的Image控件(元素)加载本地图片
            var image = new BitmapImage(new Uri(@"C:\外星人.png", UriKind.Absolute));
            image1.Source = image;

            //使用WinForm的Timer组件
            _time.Tick += time_Tick;
            _time.Interval = 500;
            _time.Start();
        }

        private void time_Tick(object sender, EventArgs e)
        {
            mm.Text = DateTime.Now.ToString(CultureInfo.InvariantCulture);
        }
    }
}

运行效果

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值