2023年12月14日发(作者:)

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

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

为了使用PPT功能,添加类

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

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

// Machine generated IDispatch wrapper class(es) created with Add Class from Typelib Wizard

// #import "D:Program FilesMicrosoft " no_namespace

// CApplication wrapper class

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

// Initialize OLE libraries

if (!AfxOleInit())

{

AfxMessageBox("Failed to initialize OLE");

return FALSE;

}

//Start PowerPoint and get

if (!Dispatch("ation"))

{

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 message

_Visible(TRUE);

// 有时候为了ppt不影响程序,会设置启动大小

// _Width(100);

// _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);

l();

// To get the selected file's path and name

CString strFileName;

strFileName = hName();*/

//实现2.直接选择需要打开的ppt文件

CString strFileName("H:");

//

if (!y())

{

presentations = _Presentations();

presentation = (strFileName, 0, 0, 1);//打开相应ppt文件

}

presentations = _ActivePresentation();

slides = _Slides();

// Show the first slide of the presentation

slide = (COleVariant((long)1));

//运行这个演示

Sleep(2000);

slideShowSettings = _SlideShowSettings();

();

//这里可以改变ppt大小

/*slideShowWindow = deShowWindow();

th(100);

ght(100);*/

//关闭PPT

//presentation = _ActivePresentation();

//();

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

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