演练:在 Windows 窗体中承载 Windows Presentation Foundation 复合控件 【转载】

本文介绍如何在Windows窗体中承载WPF复合控件,包括创建WPFUserControl、Windows窗体宿主项目及承载WPFUserControl的过程。使用ElementHost控件简化了这一操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该篇文章是我于2009年6月10日通过自己编写的工具,批量从位于在博客园的博客站点(http://chenxizhang.cnblogs.com)同步而来。文章中的图片地址仍然是链接到博客园的。特此说明!

陈希章

原文地址:http://www.cnblogs.com/chenxizhang/archive/2009/05/07/1451281.html
原文标题:演练:在 Windows 窗体中承载 Windows Presentation Foundation 复合控件 【转载】
原文发表:2009/5/7 1:16:00

http://msdn.microsoft.com/zh-cn/library/ms745781.aspx

更新:2007 年 11 月

本演练演示如何创建 WPF 复合控件,并通过使用 ElementHost 控件在 Windows 窗体控件和窗体中承载它。

在本演练中,将实现一个包含两个子控件的 WPFUserControlUserControl 显示一个三维 (3-D) 圆锥。使用 WPF 呈现三维对象比使用 Windows 窗体更简单。因此,对于在 Windows 窗体中创建三维图形,承载 WPFUserControl 类非常有意义。

本演练涉及以下任务:

有关本演练中所阐释任务的完整代码清单,请参见在 Windows 窗体中承载 Windows Presentation Foundation 复合控件的示例

注意 显示的对话框和菜单命令可能与“帮助”中所述的有所不同,具体取决于当前的设置或版本。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置

先决条件

您需要以下组件来完成本演练:

  • Visual Studio 2008.

创建 UserControl

创建 UserControl
  1. 创建一个名为 HostingWpfUserControlInWf 的 WPF 用户控件库项目。

  2. 在 WPF 设计器中打开 UserControl1.xaml。

  3. 用下面的代码替换生成的代码。

    该代码定义一个包含两个子控件的 System.Windows.Controls.UserControl。第一个子控件是 System.Windows.Controls.Label 控件;第二个控件是显示三维圆锥的 Viewport3D 控件。

创建 Windows 窗体宿主项目

创建宿主项目
  1. 将名为 WpfUserControlHost 的 Windows 应用程序项目添加到解决方案中。有关更多信息,请参见“添加新项目”对话框

  2. 在解决方案资源管理器中,添加一个对名为 WindowsFormsIntegration.dll 的 WindowsFormsIntegration 程序集的引用。

  3. 添加对以下 WPF 程序集的引用:

    • PresentationCore

    • PresentationFramework

    • WindowsBase

  4. 添加对 HostingWpfUserControlInWf 项目的引用。

  5. 在解决方案资源管理器中,将 WpfUserControlHost 项目设置为启动项目。

承载 Windows Presentation Foundation UserControl

承载 UserControl
  1. 在 Windows 窗体设计器中打开 Form1。

  2. 在“属性”窗口中,单击“事件”,然后双击 Load 事件以创建事件处理程序。

    代码编辑器打开并定位到新生成的 Form1_Load 事件处理程序。

  3. 将 Form1.cs 中的代码替换为以下代码。

    Form1_Load 事件处理程序将创建一个 UserControl1 实例,并将其添加到ElementHost 控件的子控件集合中。ElementHost 控件将被添加到窗体的子控件集合中。

    Visual Basic

    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Drawing
    Imports System.Text
    Imports System.Windows.Forms
    
    Imports System.Windows.Forms.Integration
    
    Public Class Form1
        Inherits Form
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Create the ElementHost control for hosting the
            ' WPF UserControl.
           
    Dim host As New ElementHost()
            host.Dock = DockStyle.Fill
    
            ' Create the WPF UserControl.
            Dim uc As New HostingWpfUserControlInWf.UserControl1()
    
            ' Assign the WPF UserControl to the ElementHost control's
            ' Child property.
            host.Child = uc
    
            ' Add the ElementHost control to the form's
            ' collection of child controls.
            Me.Controls.Add(host)
        End Sub
    
    End Class
    
    
  4. 按 F5 生成并运行该应用程序。

作者:陈希章
出处:http://blog.youkuaiyun.com/chen_xizhang
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值