操做PPT的主要内容包括:启动、打开、关闭、幻灯片播放、首页、末页、上一页、下一页等。
本代码以PowerPoint 2013为例,其他OFFICE组件及版本方法与此类似。
一、整体界面如下:
下面是主要步骤和代码:
1.创建MFC对话框应用程序,在向导的第2步选择automation,其他保持默认即可。
2.右击项目,"添加" -> "类" :
3.然后添加PPT的类库(word,excel同理),如下:
选择如下:
4.添加接口(如果不知道用哪个,那就都添加)
确定,将添加了很多头文件到文件里,如下:
主要用到的头文件如下(可以把不想用到的删掉:
5.把以上需要用到的头文件添加到工程的头文件中,并且要把以上添加的每个头文件的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
6.在对话框上添加打开、关闭、首页、末页、上一页、下一页等按钮及函数。
7.在应用程序的InitInstance()中初始化OLE,代码如下:
1 | // Initialize OLE libraries |
2 | if (!AfxOleInit()) |
3 | { |
4 | AfxMessageBox( "Failed to initialize OLE" ); |
5 | return FALSE; |
6 | } |
8.在对话框应用程序的头文件中添加如下变量:
CApplication app;
CPresentation presentation;
CPresentations presentations;
CSlideShowView slideShowView;
CSlideShowWindow slideShowWindow;
CSlideShowSettings slideShowSettings;
CSlides slides;
CSlide slide;
9.在打开按钮函数中添加如下代码:
//Start PowerPoint and get Application object...
if (!app.CreateDispatch(L"Powerpoint.Application"))
{
AfxMessageBox(L"Couldn't start PowerPoint.");
}
else // Make PowerPoint visible and display a message
{
app.put_Visible(TRUE);
// 有时候为了ppt不影响程序,会设置启动大小
// app.put_Width(100);
// app.put_Height(100);
}
CString strFilter = L"PowerPoint Files (*.ppt;*.pptx)|*.ppt;*.pptx|All Files(*.*)|*.*||";
CFileDialog FileDlg(TRUE, L"PPT", NULL, OFN_FILEMUSTEXIST | OFN_NONETWORKBUTTON
| OFN_PATHMUSTEXIST, strFilter);
FileDlg.DoModal();
// To get the selected file's path and name
CString strFileName;
strFileName = FileDlg.GetPathName();
if (!strFileName.IsEmpty())
{
presentations = app.get_Presentations();
presentation = presentations.Open(strFileName, 0, 0, 1);
}
presentations = app.get_ActivePresentation();
slides = presentation.get_Slides();
// Show the first slide of the presentation
slide = slides.Item(COleVariant((long)1));
//运行这个演示
slideShowSettings = presentation.get_SlideShowSettings();
slideShowSettings.Run();
//这里可以改变ppt大小
/*slideShowWindow = presentation.GetSlideShowWindow();
slideShowWindow.SetWidth(100);
slideShowWindow.SetHeight(100);*/
app.put_WindowState((long)2);
//add
HWND hWnd = ::FindWindow(_T("screenClass"), 0);
if (NULL != hWnd)
{
CWnd *pWnd = GetDlgItem(IDC_SHOWING);
CRect rect;
pWnd->GetWindowRect(&rect);
::SetParent(hWnd, pWnd->GetSafeHwnd());
pWnd = CWnd::FromHandle(hWnd);
rect.SetRect(0, 0, rect.Width(), rect.Height());
pWnd->MoveWindow(&rect);
pWnd->Invalidate();
}
10.在关闭按钮函数中添加如下代码:
app.Quit();
11.在首页按钮函数中添加如下代码:
presentation = app.get_ActivePresentation();
slideShowWindow = presentation.get_SlideShowWindow();
slideShowView = slideShowWindow.get_View();
slideShowView.First();
12.在末页按钮函数中添加如下代码:
presentation = app.get_ActivePresentation();
slideShowWindow = presentation.get_SlideShowWindow();
slideShowView = slideShowWindow.get_View();
slideShowView.Last();
13.在翻到前一页按钮函数中添加如下代码:
presentation = app.get_ActivePresentation();
slideShowWindow = presentation.get_SlideShowWindow();
slideShowView = slideShowWindow.get_View();
slideShowView.Previous();
14.在翻到下页按钮函数中添加如下代码:
presentation = app.get_ActivePresentation();
slideShowWindow = presentation.get_SlideShowWindow();
slideShowView = slideShowWindow.get_View();
slideShowView.Next();
以上就是大体的流程了,希望大家可以运行成功哦~
VS2013操作PowerPoint,在MFC窗口中打开PPT 配套的源码下载