如何在WPF中使用Forms(WPF选择文件窗口)

创建WPF类库,在类库中去调用自己所需的功能,编写完成,生成DLL,在自己的项目中去引用此DLL。

在类库中引入System.Windows.Forms

System.Windows.Forms直接Using是无法使用的,需要在项目中引入DLL,网上找的又不一定安全,我们在安装VS2022时,会有自带的DLL文件,我们可以在项目中直接引用该文件。

"C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\8.0.4\ref\net8.0\System.Windows.Forms.dll"

项目树-依赖项-右键-添加项目引用--项目-浏览-在弹出的窗口中按照上面地址找到DLL文件-点击添加按钮(也可以双击DLL文件)-点击确定-在cs文件中using Syetem.Windows.Forms;即可。

创建打开文件窗口

var openFileDialog = new OpenFileDialog
{
    //设置文件过滤器属性        左边自定义 |  右边为文件后缀
    Filter = "PDF Documents|*.pdf"
};
//显示文件选择窗口
openFileDialog.ShowDialog();

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Microsoft.Office.Interop.PowerPoint命名空间的类来实现在WPF窗口播放PPT。具体步骤如下: 1. 添加对Microsoft.Office.Interop.PowerPoint的引用。 2. 在WPF窗口添加一个WindowsFormsHost控件。 3. 在WindowsFormsHost控件添加一个AxHost控件,并将其类型设置为Microsoft.Office.Interop.PowerPoint.Presentation。 4. 在窗口加载事件使用Microsoft.Office.Interop.PowerPoint.Application类加载PPT文件。 5. 在AxHost控件的CreateControl方法使用Microsoft.Office.Interop.PowerPoint.SlideShowSettings类设置PPT的播放方式。 下面是示例代码: ```C# using System.Windows.Forms.Integration; using Microsoft.Office.Interop.PowerPoint; //添加引用:Microsoft.Office.Core、Microsoft.Office.Interop.PowerPoint //在WPF窗口添加一个WindowsFormsHost控件 WindowsFormsHost host = new WindowsFormsHost(); this.grid.Children.Add(host); //在WindowsFormsHost控件添加一个AxHost控件,并将其类型设置为Microsoft.Office.Interop.PowerPoint.Presentation AxHost axHost = new AxHost(); axHost.Dock = DockStyle.Fill; axHost.CreateControl(); axHost.GetType().InvokeMember("Enable", BindingFlags.InvokeMethod, null, axHost, new object[] { true }); axHost.GetType().InvokeMember("DisplayAlerts", BindingFlags.SetProperty, null, axHost, new object[] { PpAlertLevel.ppAlertsNone }); host.Child = axHost; //在窗口加载事件使用Microsoft.Office.Interop.PowerPoint.Application类加载PPT文件 private void Window_Loaded(object sender, RoutedEventArgs e) { Application pptApp = new Application(); Presentation pptPresentation = pptApp.Presentations.Open(@"D:\test.pptx", MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); axHost.GetType().InvokeMember("Ocx", BindingFlags.SetProperty, null, axHost, new object[] { pptPresentation }); } //在AxHost控件的CreateControl方法使用Microsoft.Office.Interop.PowerPoint.SlideShowSettings类设置PPT的播放方式 protected override void CreateControl() { base.CreateControl(); if (this.ActiveXInstance != null) { SlideShowSettings slideShowSettings = ((Presentation)this.ActiveXInstance).SlideShowSettings; slideShowSettings.ShowType = PpSlideShowType.ppShowTypeSpeaker; slideShowSettings.Run(); } } ``` 注意:在使用AxHost控件播放PPT时,需要在CreateControl方法设置PPT的播放方式。此外,由于AxHost控件是Windows Forms控件,因此需要使用WindowsFormsHost控件将其嵌入到WPF窗口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值