VS2013操作PowerPoint,在MFC窗口中打开PPT

操做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 配套的源码下载




  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
MFC 显示PPT //启动 PowerPoint: void CMainFrame::OnPowerpointStartpowerpoint() { /// Check if the IDispatch connection exists with PowerPoint, // if not create one. if (m_ppt.m_lpDispatch == NULL) { // Create IDispatch connection to PowerPoint. m_ppt.CreateDispatch("PowerPoint.Application"); }; // Bring the PowerPoint application to the front. m_ppt.Activate(); } void CMainFrame::OnPowerpointStartslideshow() { _Presentation oPresentation; SlideShowSettings oShow; // Attach to the Active Presentation. oPresentation.AttachDispatch(m_ppt.GetActivePresentation()); // Attach to the slide-show settings. oShow.AttachDispatch(oPresentation.GetSlideShowSettings()); // Run the slide show. oShow.Run(); } // 创建幻灯片: void CMainFrame::OnPowerpointCreateslide() { // Connect to the active presentation. There is no error trapping. // If the active presentation the framework traps // the error and displays a message box. _Presentation ActivePresentation(m_ppt.GetActivePresentation()); // Connect to the slides collection. Slides oSlides(ActivePresentation.GetSlides()); // This constant is defined in the PowerPoint Object model. // You can use the Object Browser, with Visual Basic Editor // (VBE), to look up the different constant values. const ppLayoutTitleOnly = 11; // Add a new slide to the presentation. This code adds the new // slide to the end of the presentation. oSlides.Add(oSlides.GetCount() + 1l, ppLayoutTitleOnly); } // 创建演示文稿: void CMainFrame::OnPowerpointCreatepresentation() { Presentations PresCollection; // Make sure there is a dispatch pointer for PowerPoint. if(m_ppt.m_lpDispatch == NULL) { // Display a message indicating that PowerPoint is not running. MessageBox("PowerPoint is not running.", "Start PowerPoint"); } else { // Bring PowerPoint to the front. m_ppt.Activate(); // Attach the presentations collection to the PresCollection // variable. PresCollection.AttachDispatch(m_ppt.GetPresentations()); // Create a new presentation. PresCollection.Add(1);
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DaveBobo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值