如何使用 MFC 创建并显示一个 PowerPoint 演示文稿

  1. 请按照下面的 Microsoft 知识库文章,以创建示例项目使用该 IDispatch 接口,并在 Msppt8.olb 类型库中的成员函数定义中的步骤 1 到 12:

    178749  (http://support.microsoft.com/kb/178749/EN-US/ ) 如何创建自动化项目使用 MFC 和类型库
  2. 请按 CTRL + W 以再次显示此类向导并选择自动选项卡,单击添加类按钮并选择一个类型库。找到 Graph8.olb,然后单击打开。选择"表"类别,然后单击确定。单击确定以关闭此类向导。

    : 与本示例将创建的演示文稿包含嵌入的 Microsoft Graph 图表 ; 这是原因的在图中添加从 Microsoft Graph 类型库 (Graph8.olb) 的类。
  3. AutoProjectDlg.cpp 文件顶部添加以下行:
          #include "msppt8.h"
          #include "graph8.h"
    					
  4. 将下面的代码添加到 CAutoProjectDlg::OnRun() AutoProjectDlg.cpp 文件中:
       // Commonly used OLE-variants.
       COleVariant
                   covTrue((short)TRUE),
                   covFalse((short)FALSE),
                   covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    
       _Application   app;
       Presentations  presentations;
       _Presentation  presentation;
       Slides         slides;
       _Slide         slide;
       ShapeRange     shaperange;
       Shapes         shapes;
       Shape          shape;
       TextFrame      textframe;
    
       TextRange      textrange;
       Font           font;
       FillFormat     fillformat;
       ColorFormat    colorformat;
       ShadowFormat   shadow;
    
          CString strPic1 ="C://Program Files//Microsoft Office//Clipart//Popular//Amhappy.wmf";
          CString strTemplate = 
             "C://Program Files//Microsoft Office//Templates//Presentation Designs//Zesty.pot";
    
    
        if(!app.CreateDispatch("Powerpoint.Application"))
          {
             AfxMessageBox("Could not create Powerpoint object.");
             return;
          }
    
          //Make the application visible and minimized.
          app.SetVisible((long)TRUE);
          app.SetWindowState((long) 2);   //ppWindowMinimized=2
    
          //Get the Presentations collection and create a new presentation
          //from a template(.POT)
          presentations.AttachDispatch(app.GetPresentations());
          presentation = presentations.Open(
             strTemplate,   //File name
             (long)0,       //Read-only
             (long)-1,      //Untitled
             (long)-1       //WithWindow
             );
    
          slides = presentation.GetSlides();
    
          //Add the first slide to the presentation.
          slide = (slides.Add((long)1, (long)11));   //pptLayoutTitleOnly=11
    
          //Modify the text and font name of the TextFrame on the slide.
          shapes = slide.GetShapes();
          shape = shapes.Item(COleVariant((short)1));
          textframe = shape.GetTextFrame();
          textrange = textframe.GetTextRange();
          textrange.SetText("My Sample Presentation");//Set the text
          font = textrange.GetFont();
          font.SetName("Comic Sans MS");              //Set the font name.
          font.SetSize((float)48);                    //Set the font size.
    
          //Insert a picture into the slide and position it.
    
          shapes.AddPicture(
                       strPic1,      //Filename
                       (long)0,      //LinkToFile
                       (long)-1,     //SaveWithDocument
                       (float)150,   //Left
                       (float)150,   //Top
                       (float)500,   //Width
                       (float)350    //Height
                       );
    
          //Add the second slide to the presentation.
          slide = (slides.Add((long)2, (long)11));   //pptLayoutTitleOnly=11
    
          //Modify the text and font name of the
          //TextFrame on the slide.
          shapes = slide.GetShapes();
          shape = shapes.Item(COleVariant((short)1));
          textframe = shape.GetTextFrame();
          textrange = textframe.GetTextRange();
          textrange.SetText("Look! It's a Graph!");//Set the text
          font = textrange.GetFont();
          font.SetName("Comic Sans MS");           //Set the font name.
          font.SetSize((float)48);                 //Set the font size.
    
          //Add a Graph8 object to the slide.
          shape = shapes.AddOLEObject(
                         (float)150,      //Left
                         (float)150,      //Top
                         (float)480,      //Width
                         (float)320,      //Height
                         "MSGraph.Chart", //Classname
                         "",              //FileName
                         (long)0,         //DisplayAsIcon
                         "",              //IconFileName
                         (long)0,         //IconIndex
                         "",              //IconLabel
                         (long)0          //Link
                         );
    
          //Modify the charttype of the graph.
          Chart chart;
          OLEFormat olefmt = shape.GetOLEFormat();
          chart = olefmt.GetObject();
          chart.SetChartType((long)70);
    
          //Add the third slide to the presentation.
          slide = (slides.Add((long)3, (long)12));   //ppLayoutBlank = 12
    
          //Change the background color of the slide.
          slide.SetFollowMasterBackground((long)0);
          shaperange = slide.GetBackground();
          fillformat = shaperange.GetFill();
          colorformat = fillformat.GetForeColor();
          colorformat.SetSchemeColor((long)3);   //ppShadow = 3
    
    
          //Add Text Effects to the last slide
          shapes = slide.GetShapes();
          shape = shapes.AddTextEffect(
                         (long)27,    //PresetTextEffect (msoTextEffect28 = 27)
                         "The End",   //Text
                         "Impact",    //FontName
                         (float)96,   //FontSize
    
                         (long)0,     //FontBold
                         (long)0,     //FontItalic
                         (float)230,  //Left
                         (float)200   //Top
                         );
    
          //Apply a gradient fill to the texteffect.
          fillformat = shape.GetFill();
          colorformat = fillformat.GetForeColor();
          colorformat.SetSchemeColor((long)2);   //ppForeground = 2
          colorformat = fillformat.GetBackColor();
          colorformat.SetSchemeColor((long)4);   //ppTitle = 4
    
          fillformat.TwoColorGradient ((long)1, (long)1);
                                                 //msoGradientHorizontal = 1
    
    
          //Apply a shadow to the texteffect.
          shadow = shape.GetShadow();
          colorformat = shadow.GetForeColor();
          colorformat.SetSchemeColor((long)2);   //ppForeground = 2
          shadow.SetVisible((long)-1);
          shadow.IncrementOffsetX((float)3);
          shadow.IncrementOffsetY((float)3);
    
          SlideShowTransition   show;
          SlideShowSettings   slideshow;
    
          //Set the Transition effects for Slide 1.
          slide = slides.Item(COleVariant((short)1));
          show = slide.GetSlideShowTransition();
          show.SetEntryEffect((long)769);      //ppEffectBlindsHorizontal=769
          show.SetAdvanceOnTime((long)-1);
          show.SetAdvanceTime((float)3);
    
          //Set the Transition effects for Slide 2.
          slide = slides.Item(COleVariant((short)2));
          show = slide.GetSlideShowTransition();
          show.SetEntryEffect((long)770);      //ppEffectBlindsVertical=770
          show.SetAdvanceOnTime((long)-1);
          show.SetAdvanceTime((float)3);
    
          //Set the Transition effects for Slide 3.
          slide = slides.Item(COleVariant((short)3));
          show = slide.GetSlideShowTransition();
          show.SetEntryEffect((long)3073);      //ppEffectBoxOut=3073
          show.SetAdvanceOnTime((long)-1);
          show.SetAdvanceTime((float)3);
    
          //Run the show.
          slideshow = presentation.GetSlideShowSettings();
          slideshow.Run();
    
          //Quit the application after waiting 9 seconds (the length of the
          //slide show).
          ::Sleep(9000);
          presentation.SetSaved((long)-1);
          app.Quit();
    					
  5. 如果需要修改该工作分配的 strPic1 和 strTemplate 到图像文件和一个 PowerPoint 演示文稿模板的有效路径:
    CString strPic1 ="C://Program Files//Microsoft Office//Clipart//Popular//Amhappy.wmf";
    CString strTemplate = 
             "C://Program Files//Microsoft Office//Templates//Presentation Designs//Zesty.pot";
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值