wpf怎么调用winform组件

wpf怎么调用winform组件,第1张

功能实现主要分三步:

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

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

<Window x:Class="CrossBowDemo.MainWindow"

xmlns:wfi ="clr-namespace:System.Windows.Forms.Integrationassembly=WindowsFormsIntegration"

xmlns:wf ="clr-namespace:System.Windows.Formsassembly=System.Windows.Forms"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

title="Hosting Windows Forms Control In WPF"

Height="300"

Width="650"

ResizeMode="NoResize"

Loaded="WindowLoadedHandler"

>

</Window>

3、在XAML编码区实现你想添加的控件

原文添加的是DataGridView控<wfi:WindowsFormsHost>

<wf:DataGridView x:Name="Dg" Dock="Fill" SelectionMode="FullRowSelect">

</wf:DataGridView>

</wfi:WindowsFormsHost>件:

1. 新建一个基于.Net Framework 3.5 的WPF 应用程序项目:WPFWMP。

2. 在工程中新建Windows Forms Control Library 项目:WMPControlLibrary。

创建WMP 控件

下面要在WMPControlLibrary 中创建Windows Media Player 控件,在项目中加入Windows Media Player COM。

在左侧工具栏中若没有Windows Media Player 控件的话,可以右键General 选择Choose Items,在COM 组件列表中勾选Windows Media Player 选项。

将Windows Media Player 控件拖入设计窗口,并将Dock 设置为Fill 填充控件。

F6 编译项目后会生成以下三个DLL 文件,这就是我们稍后将要在WPF 中用到的WMP 控件库。

嵌入WMP 控件

回到WPF 项目在前篇文章的基础上,保留其中“Open File” 按键和Button 样式。将上面三个DLL 文件及System.Windows.Forms、WindowsFormsIntegration 加入项目。

在XAML 中加入AxWMPLib 命名空间,并将上篇MediaElement 替换为AxWindowsMediaPlayer 控件,注意此处是将WinForm 控件嵌入WPF 程序,所以要将AxWindowsMediaPlayer 控件放到<WindowsFormsHost>标签中。

你是要在Visual Studio中安装别人开发的WPF组件包(如果它无法自动安装),那么你需要在工具箱上右键点击【选择项】,然后浏览到你要添加的Dll文件,然后找到你要添加的控件,确定就可以安装在工具箱上了。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11456985.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存