主要内容包括:启动、打开、关闭、幻灯片播放、首页、末页、上一页、下一页等。
本代码以PowerPoint 2003为例,其他OFFICE组件及版本方法与此类似。
下面是主要步骤和代码:
1、创建MFC对话框应用程序,在向导的第2步选择automation,其他保持默认即可。
2、在对话框上添加启动、打开、关闭、幻灯片播放、首页、末页、上一页、下一页等按钮及函数。
3、在应用程序的InitInstance()中初始化OLE,代码如下:
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("Failed to initialize OLE");
return FALSE;
}
4、运用类向导添加PowerPoint类型库,类型库默认在"C:\Program Files\Microsoft Office\Office11\"下,文件名为:msppt.olb。//注意选择下面第6点、的类名!!!
5、在对话框应用程序的头文件中添加:
#include "msppt.h"
6、在在对话框应用程序的头文件中添加如下变量:
_Application app;
Presentations Presentations;
_Presentation Presentation;
SlideShowView View;
SlideShowWindow SlideShowWindow;
SlideShowSettings slideshow;
Slides slides;
_Slide slide;
DocumentWindow documentwindow;
View ActiveView;
7、在启动按钮函数中添加如下代码:
void CXXXDlg::OnBtnStart()
{
// Start PowerPoint and get Application object...
if(!app.CreateDispatch("Powerpoint.Application"))
{
AfxMessageBox("Couldn't startPowerPoint.");
}
else // Make PowerPoint visible and display a message
{
app.SetVisible(TRUE);
TRACE("PowerPoint isRunning!");
}
}
8、在打开按钮函数中添加如下代码:
void CXXXDlg::OnBtnOpen()
{
static char BASED_CODE szFilter[] = "PowerPoint Files(*.ppt)|*.ppt||";
CFileDialogFileDlg(TRUE,"PPT",NULL,OFN_FILEMUSTEXIST|OFN_NONETWORKBUTTON
|OFN_PATHMUSTEXIST,szFilter);
FileDlg.DoModal();
// To get the selected file's path andname
CString strFileName;
strFileName = FileDlg.GetPathName();
if(!strFileName.IsEmpty())
{
Presentations =app.GetPresentations();
Presentation =Presentations.Open(strFileName,0,0,1);
}
}
9、在关闭按钮函数中添加如下代码:
void CXXXDlg::OnBtnClose()
{
documentwindow=app.GetActiveWindow();//获得活动的文档
documentwindow.Close();//关闭当前活动的文档
if (CanExit())
app.Quit();
}
10、在运行按钮函数中添加如下代码:
void CXXXDlg::OnBtnRun()
{
Presentations = app.GetActivePresentation();
slides = Presentation.GetSlides();
// Show the first slide of the presentation
slide = slides.Item(COleVariant((long)1));
//Run the show
slideshow = Presentation.GetSlideShowSettings();
slideshow.Run();
}
11、在翻到首页按钮函数中添加如下代码:
void CXXXDlg::OnBtnFirst()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.First();
}
12、在翻到末叶按钮函数中添加如下代码:
void CXXXDlg::OnBtnLast()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.Last();
}
13、在翻到前页按钮函数中添加如下代码:
void CXXXDlg::OnBtnPrevious()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.Previous();
}
14、在翻到下页按钮函数中添加如下代码:
void CXXXDlg::OnBtnNext()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.Next();
}
15.获得幻灯片总数
void CXXXDlg::OnBtnGetSlidesCount()
{
Presentations=app.GetActivePresentation();
slides=Presentation.GetSlides();
long endpos=slides.GetCount(); //获得幻灯片总数
}