首页
学习
活动
专区
圈层
工具
发布
50 篇文章
1
【愚公系列】2023年09月 WPF控件专题 XAML介绍
2
【愚公系列】2023年09月 WPF控件专题 WPF应用程序组成
3
【愚公系列】2023年09月 WPF控件专题 Window窗体属性和事件
4
【愚公系列】2023年09月 WPF控件专题 Label、TextBox、PasswordBox控件介绍
5
【愚公系列】2023年09月 WPF控件专题 Button控件详解
6
【愚公系列】2023年09月 WPF控件专题 RadioButton控件详解
7
【愚公系列】2023年09月 WPF控件专题 CheckBox控件详解
8
【愚公系列】2023年09月 WPF控件专题 Image控件详解
9
【愚公系列】2023年09月 WPF控件专题 Border控件详解
10
【愚公系列】2023年09月 WPF控件专题 ComboBox控件详解
11
【愚公系列】2023年09月 WPF控件专题 ListBox控件详解
12
【愚公系列】2023年09月 WPF控件专题 DatePicker控件详解
13
【愚公系列】2023年09月 WPF控件专题 Calendar控件详解
14
【愚公系列】2023年09月 WPF控件专题 Slider控件详解
15
【愚公系列】2023年09月 WPF控件专题 ProgressBar控件详解
16
【愚公系列】2023年10月 WPF控件专题 StackPanel控件详解
17
【愚公系列】2023年10月 WPF控件专题 WrapPanel控件详解
18
【愚公系列】2023年10月 WPF控件专题 DockPanel控件详解
19
【愚公系列】2023年10月 WPF控件专题 Canvas控件详解
20
【愚公系列】2023年10月 WPF控件专题 Grid控件详解
21
【愚公系列】2023年10月 WPF控件专题 Groupbox控件详解
22
【愚公系列】2023年10月 WPF控件专题 Expander控件详解
23
【愚公系列】2023年10月 WPF控件专题 TabControl控件详解
24
【愚公系列】2023年10月 WPF控件专题 Frame控件详解
25
【愚公系列】2023年10月 WPF控件专题 ListView控件详解
26
【愚公系列】2023年10月 WPF控件专题 DataGrid控件详解
27
【愚公系列】2023年10月 WPF控件专题 Menu控件详解
28
【愚公系列】2023年10月 WPF控件专题 ContextMenu控件详解
29
【愚公系列】2023年10月 WPF控件专题 TreeView控件详解
30
【愚公系列】2023年10月 WPF控件专题 ToolBar控件详解
31
【愚公系列】2023年10月 WPF控件专题 ToolBarTray控件详解
32
【愚公系列】2023年10月 WPF控件专题 StatusBar控件详解
33
【愚公系列】2023年11月 WPF控件专题 MediaElement控件详解
34
【愚公系列】2023年11月 WPF控件专题 RichTextBox控件详解
35
【愚公系列】2023年11月 WPF控件专题 GridView控件详解
36
【愚公系列】2023年11月 WPF控件专题 Rectangle控件详解
37
【愚公系列】2023年11月 WPF控件专题 Popup控件详解
38
【愚公系列】2023年11月 WPF控件专题 OpenFileDialog控件详解
39
【愚公系列】2023年11月 WPF控件专题 SaveFileDialog控件详解
40
【愚公系列】2023年11月 WPF控件专题 RepeatButton控件详解
41
【愚公系列】2023年11月 WPF控件专题 Ellipse控件详解
42
【愚公系列】2023年11月 WPF控件专题 Polygon控件详解
43
【愚公系列】2023年11月 WPF控件专题 Path控件详解
44
【愚公系列】2023年11月 WPF控件专题 WindowFormsHost控件详解
45
【愚公系列】2023年11月 WPF控件专题 WebBrowser控件详解
46
【愚公系列】2023年11月 WPF控件专题 Validation控件详解
47
【愚公系列】2023年11月 WPF控件专题 Page控件详解
48
【愚公系列】2023年11月 WPF控件专题 PrintDialog控件详解
49
【愚公系列】2023年11月 WPF控件专题 Track控件详解
50
【愚公系列】2023年11月 WPF控件专题 Polyline控件详解

【愚公系列】2023年11月 WPF控件专题 WindowFormsHost控件详解

🏆 作者简介,愚公搬代码 🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,CSDN博客专家,阿里云专家博主,腾讯云优秀博主,掘金优秀博主,51CTO博客专家等。 🏆《近期荣誉》:2022年CSDN博客之星TOP2,2022年华为云十佳博主等。

🏆《博客内容》:.NET、Java、Python、Go、Node、前端、IOS、Android、鸿蒙、Linux、物联网、网络安全、大数据、人工智能、U3D游戏、小程序等相关领域知识。

🏆🎉欢迎 👍点赞✍评论⭐收藏

🚀前言

WPF控件是Windows Presentation Foundation(WPF)中的基本用户界面元素。它们是可视化对象,可以用来创建各种用户界面。WPF控件可以分为两类:原生控件和自定义控件。

原生控件是由Microsoft提供的内置控件,如Button、TextBox、Label、ComboBox等。这些控件都是WPF中常见的标准用户界面元素。

自定义控件则允许开发人员使用XAML和C#等编程语言来创建个性化的用户界面元素。自定义控件可以根据需求提供更多的功能和自定义化选项,以及更好的用户体验。

🚀一、WindowFormsHost控件详解

WindowFormsHost控件是WPF中与WinForms集成的控件。它允许您将现有的WinForms控件嵌入到WPF应用程序中,以利用它们的功能。WindowFormsHost控件工作原理是将WinForms控件作为其子元素添加到WPF的逻辑树中。在运行时,WinForms控件被呈现在WPF窗口中。

使用WindowFormsHost控件时,首先需要添加对System.Windows.Forms和System.Windows.Forms.Integration命名空间的引用。然后,创建一个WindowFormsHost控件,并将要嵌入的WinForms控件设置为其Child属性。例如,以下代码将一个Windows Forms的ListBox嵌入到WPF的WindowFormsHost控件中:

代码语言:html
复制
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <wfi:WindowsFormsHost>
            <wf:ListBox x:Name="listBox" Width="200" Height="200" />
        </wfi:WindowsFormsHost>
    </Grid>
</Window>

在上面的示例中,我们首先在XAML文件中添加了System.Windows.Forms和System.Windows.Forms.Integration命名空间的引用,然后创建了一个WindowsFormsHost控件,将ListBox控件添加到其子元素中。

在代码中,我们可以像访问其他WPF控件一样访问嵌入的WinForms控件。例如,我们可以像以下代码一样向嵌入的ListBox控件添加项:

代码语言:c#
复制
using System.Windows.Forms;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        listBox.Items.Add("Item 1");
        listBox.Items.Add("Item 2");
        listBox.Items.Add("Item 3");
    }
}

需要注意的是,嵌入的WinForms控件不能直接访问WPF的资源,因为它们存在于不同的UI线程中。要解决这个问题,可以使用Dispatcher对象在WPF和WinForms控件之间传递信息。

🔎1.属性介绍

WindowFormsHost是WPF中的一个控件,它可以用来在WPF应用程序中嵌入一个Windows Forms控件。WindowFormsHost控件拥有以下属性:

  1. Child:Windows Forms控件被包含在这个属性中。可以通过编程方式或在设计时设置。
  2. EnableVisualStyles:一个布尔值,指示Windows Forms控件是否启用视觉样式。
  3. HostBackColor:一个颜色,用于设置WindowFormsHost控件的背景色。
  4. HostFont:一个字体,用于设置WindowFormsHost控件的字体。
  5. ChildChanged:事件,当Windows Forms控件的Child属性发生更改时发生。
  6. GotFocus、LostFocus:事件,当WindowFormsHost控件获得或失去焦点时发生。
  7. ChildGotFocus、ChildLostFocus:事件,当Windows Forms控件获得或失去焦点时发生。
  8. ChildMouseWheel:事件,当鼠标滚轮在Windows Forms控件上滚动时发生。
  9. ChildMouseEnter、ChildMouseLeave:事件,当鼠标进入或离开Windows Forms控件时发生。

🔎2.常用场景

WindowFormsHost控件是WPF中的一个容器控件,用于承载Windows Forms控件。常见的场景有:

  1. 在WPF应用程序中使用Windows Forms控件:有些Windows Forms控件具有WPF中没有的功能或特性,如PropertyGrid控件。使用WindowFormsHost控件,可以在WPF应用程序中使用这些控件。
  2. 迁移已有的Windows Forms应用程序:对于已有的Windows Forms应用程序,使用WindowFormsHost控件可以逐步将其迁移到WPF平台,减少重构的工作量。
  3. 在WPF应用程序中加速Windows Forms控件开发:有些开发者对Windows Forms控件更为熟悉,使用WindowFormsHost控件可以在WPF应用程序中加速控件开发。

需要注意的是,使用WindowFormsHost控件时,需要避免出现由于两个不同平台的控件互相影响而引起的问题。

🔎3.具体案例

WindowFormsHost控件用于在WPF的窗口中嵌入WinForms控件,可以方便地在WPF应用程序中使用WinForms控件。

以下是一个简单的案例,展示如何在WPF窗口中嵌入WinForms控件,以及在WinForms控件中使用WPF控件。

  1. 在Visual Studio中创建一个WPF应用程序。
  2. 在MainWindow.xaml文件中添加WindowFormsHost控件:
代码语言:html
复制
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <WindowsFormsHost x:Name="winFormsHost" />
    </Grid>
</Window>
  1. 在MainWindow.xaml.cs文件中添加以下代码:
代码语言:c#
复制
using System.Windows.Forms.DataVisualization.Charting;

public MainWindow()
{
    InitializeComponent();

    // 创建一个WinForms控件
    Chart chart = new Chart();
    chart.Titles.Add("Pie Chart");
    chart.Series.Add("Series1");
    chart.Series["Series1"].ChartType = SeriesChartType.Pie;
    chart.Series["Series1"].Points.AddXY("Apple", 60);
    chart.Series["Series1"].Points.AddXY("Banana", 40);

    // 将WinForms控件嵌入WPF窗口
    winFormsHost.Child = chart;
}

这个例子中创建一个Pie Chart图表,并将它嵌入到WPF窗口中。在WinForms控件中使用WPF控件也是同样的原理。


我正在参与2023腾讯技术创作特训营第三期有奖征文,组队打卡瓜分大奖!

下一篇
举报
领券