MFC控制:实现打开PPT详细步骤

来源:互联网 发布:淘宝超时发货赔付规则 编辑:程序博客网 时间:2024/05/17 04:41

 

其余选择默认,一直“下一步”即可。

 

为了使用PPT功能,添加类

 

 

不知道生成哪些类,可全部生成。之后将主要用到以下几类

 

把以上需要用到的头文件添加到工程的头文件中,并且要把以上添加的每个头文件的import注释掉,如CApplication.h

// Machine generated IDispatch wrapper class(es) created with Add Class from Typelib Wizard// #import "D:\\Program Files\\Microsoft Office\\OFFICE11\\MSPPT.OLB" no_namespace// CApplication wrapper class


在应用程序DetectDlg.cpp的InitInstance()中初始化OLE和创建PowerPoint接口对象,代码如下: 

// Initialize OLE librariesif (!AfxOleInit()){AfxMessageBox("Failed to initialize OLE");return FALSE;}//Start PowerPoint and get Application object...if (!app.CreateDispatch("Powerpoint.Application")){AfxMessageBox("Couldn't start PowerPoint.");return FALSE;}


在对话框应用程序的头文件中添加如下头文件和变量

#include "CApplication.h"#include "CPresentation.h"#include "CPresentations.h"#include "CSlideShowView.h"#include "CSlideShowWindow.h"#include "CSlideShowSettings.h"#include "CSlides.h"#include "CSlide.h" public:CApplication app;CPresentation presentation;CPresentations presentations;CSlideShowView slideShowView;CSlideShowWindow slideShowWindow;CSlideShowSettings slideShowSettings;CSlides slides;CSlide slide;


添加一个按钮,在其click函数中添加代码

// Make PowerPoint visible and display a messageapp.put_Visible(TRUE); // 有时候为了ppt不影响程序,会设置启动大小// app.put_Width(100);// app.put_Height(100); ////////////两种打开文件方法//////////////////////////////////////////////////实现1.生成选择ppt文件对话框/*CString strFilter = "PowerPoint Files (*.ppt;*.pptx)|*.ppt;*.pptx|All Files(*.*)|*.*||";CFileDialog FileDlg(TRUE, "PPT", NULL, OFN_FILEMUSTEXIST | OFN_NONETWORKBUTTON| OFN_PATHMUSTEXIST, strFilter);FileDlg.DoModal();// To get the selected file's path and nameCString strFileName;strFileName = FileDlg.GetPathName();*///实现2.直接选择需要打开的ppt文件CString strFileName("H:\\SpotDetect\\Detect\\Test.ppt");//////////////////////////////////////////////////if (!strFileName.IsEmpty()){presentations = app.get_Presentations();presentation = presentations.Open(strFileName, 0, 0, 1);//打开相应ppt文件} presentations = app.get_ActivePresentation();slides = presentation.get_Slides();// Show the first slide of the presentationslide = slides.Item(COleVariant((long)1));//运行这个演示Sleep(2000);slideShowSettings = presentation.get_SlideShowSettings();slideShowSettings.Run();//这里可以改变ppt大小/*slideShowWindow = presentation.GetSlideShowWindow();slideShowWindow.SetWidth(100);slideShowWindow.SetHeight(100);*/ //关闭PPT//presentation = app.get_ActivePresentation();//presentation.Close();

这样就可以成功打开一个ppt文件了。

之后将更新对PPT文件的操作和项目中的交互操作。