C# wpf 嵌入winform控件

WPF Hwnd窗口互操作系列

第一章 嵌入Hwnd窗口
第二章 嵌入WinForm控件(本章)
第三章 嵌入WPF控件
第四章 嵌入外部程序
第五章 底部嵌入HwndHost



前言

wpf的强大界面能力,再加上winform的性能以及灵活性,那基本上什么界面都能够做的很好。比如视频渲染用winform,功能按键用wpf,就能达到性能和界面效果和完美结合。wpf有直接的控件支持嵌入winform控件,使用方法也很简单,网上很容易搜到。之所以编写这篇文章,主要用于记录。


一、导入WinForm

1、.Net Framwork

(1)、右键添加引用

在这里插入图片描述

(2)、勾选程序集

在这里插入图片描述

2、.Net 6.0^

.net core导入比较简单,笔者使用的是.net 6.0,其他版本应该类似。

(1)、打开项目属性

在这里插入图片描述

(2)、勾选启用Windows窗体

在这里插入图片描述


二、引用命名空间

注:Window其他属性略。

<Window xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" />

三、使用 WindowsFormsHost

1、直接xaml中使用

添加一个winform的TextBox

<Window x:Class="WpfApp6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp6"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <WindowsFormsHost Width="400" Height="100" >
            <wf:TextBox BackColor="255,192,192,192" />
        </WindowsFormsHost>
    </Grid>
</Window>

效果预览
在这里插入图片描述

2、后台设置

MainWindow.xaml

<Window x:Class="WpfApp6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp6"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <WindowsFormsHost x:Name="wf_test" Width="400" Height="100">         
        </WindowsFormsHost>
    </Grid>
</Window>

MainWindow.xaml.cs
注:对于.Net Framework下列还需要引用System.Drawing程序集

using System.Windows;
namespace WpfApp6
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var tb = new System.Windows.Forms.TextBox();
            tb.BackColor= System.Drawing.Color.RoyalBlue;
            tb.ForeColor = System.Drawing.Color.White;
            tb.Font = new System.Drawing.Font("",24);
            wf_test.Child = tb;
        }
    }
}

在这里插入图片描述


总结

以上就是今天要讲的内容,wpf对winform的嵌入支持还是比较好的,可以直接在xaml使用winform控件,同时也可以在后台代码设置,将winform项目加入到wpf中几乎没什么阻碍,唯一的问题就是winform控件会覆盖wpf控件。

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WPF中调用WinForm控件是通过将WinForm控件嵌入WPF内容中实现的。首先,需要在WPF项目中引用`WindowsFormsIntegration`命名空间。 1. 创建WinForm控件:在WinForm项目中创建所需的WinForm控件,例如一个Windows.Forms.Button。 2. 在WPF中添加WindowsFormsHost控件:在WPF的XAML文件中,添加一个WindowsFormsHost控件。 ```xaml <Grid> <WindowsFormsHost Name="windowsFormsHost1" /> </Grid> ``` 3. 将WinForm控件添加到WindowsFormsHost控件中:在WPF的代码文件中,在相关的事件处理程序或页面加载完成等事件中,将WinForm控件添加到WindowsFormsHost控件中。 ```csharp private void Window_Loaded(object sender, RoutedEventArgs e) { System.Windows.Forms.Button winFormButton = new System.Windows.Forms.Button(); windowsFormsHost1.Child = winFormButton; } ``` 通过以上步骤,就可以将WinForm控件嵌入WPF中了。需要注意的是,由于WPFWinForm使用了不同的UI渲染技术,嵌入WinForm控件的外观可能会与周围的WPF控件不完全一致,需要进行一些样式调整来保持一致性。 同时,还可以通过WPF的命令模型和事件模型与嵌入WinForm控件进行交互。例如,可以通过WPF的命令绑定和事件处理程序来响应WinForm控件的点击事件,实现业务逻辑的处理。 总而言之,通过WindowsFormsHost控件可以在WPF嵌入WinForm控件,并通过WPF的命令和事件模型来与其进行交互,提供更丰富的用户界面和功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeOfCC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值