C# 创建幻灯片

 //C#如何动态生成ppt幻灯片 并且自动播放呢 我有源码可是缺少引用 确实搞不明白了 我用的office2007
//源码如下:
//C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Core;
using PowerPoint=Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.Graph;
using System.Runtime.InteropServices;
using System.IO; namespace Example {
    public partial class Form1 : Form
    { public Form1()
    {
        InitializeComponent();
    }
        String MyTemplateFile = Directory.GetCurrentDirectory() + " \\Blends.pot"; //浏览图像文件
        private void button1_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter="所有图像文件(JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg; *.gif;*.bmp;*.tif; *.tiff; *.png| JPeg 图像文件 (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 图像文件(*.gif)|*.gif |BMP 图像文件(*.bmp)|*.bmp|Tiff 图像文件(*.tif;*.tiff) |*.tif; *.tiff|Png 图像文件(*.png)| *.png |所有文件 (*.*)|*.*";
            if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                string MyFileName=this.openFileDialog1.FileName;
                this.pictureBox1.Image=Image.FromFile(MyFileName);
            }
        } //创建播放幻灯片
        private void button2_Click(object sender, EventArgs e)
        {
            string MyFileName; PowerPoint.ApplicationClass MyApp;
            PowerPoint.Presentations MyPresSet;
            PowerPoint._Presentation MyPres;
            PowerPoint.Slides MySlides;
            PowerPoint._Slide MySlide;
            PowerPoint.SlideShowTransition MySST;
            PowerPoint.SlideShowSettings MySSS;
            PowerPoint.SlideRange MySldRng;
            PowerPoint.SlideShowWindows MySSWs;
            bool MyAssistantOn;
            int i, MyCount;
            PowerPoint.TextRange MyTextRng;
            Microsoft.Office.Interop.Graph.Chart MyChart;
            PowerPoint.Shapes MyShapes;
            PowerPoint.Shape MyShape;
            try {
                MyFileName = this.openFileDialog1.FileName;
                if (!File.Exists(MyFileName))
                {
                    MessageBox.Show("幻灯片模板文件实际不存在!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return;
                }
                MyApp = new PowerPoint.ApplicationClass();
                MyApp.Visible = MsoTriState.msoCTrue;
                MyPresSet = MyApp.Presentations;
                MyPres = MyPresSet.Open(MyTemplateFile, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
                MySlides = MyPres.Slides;
               
                //创建幻灯片第一页
                MySlide=MySlides.Add(1,PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
                MyTextRng=MySlide.Shapes[1].TextFrame.TextRange;
                MyTextRng.Text="125555555555555555555552111111";
                MyTextRng.Font.Name="宋体";
                MyTextRng.Font.Size=48;
                MySlide.Shapes.AddPicture(MyFileName, MsoTriState.msoFalse,MsoTriState.msoTrue,150, 150, 500, 350);
               
                //创建幻灯片第二页
                MySlide=MySlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
                MyTextRng=MySlide.Shapes[1].TextFrame.TextRange; MyTextRng.Text="演示绘制饼图";
                MyTextRng.Font.Name="宋体";
                MyTextRng.Font.Size= 48;
                MyChart = (Microsoft.Office.Interop.Graph.Chart)MySlide.Shapes.AddOLEObject(150, 150, 480, 320, "MSGraph.Chart.8", "", MsoTriState.msoFalse, "", 0, "",
                    MsoTriState.msoFalse).OLEFormat.Object;
                MyChart.ChartType = Microsoft.Office.Interop.Graph.XlChartType.xl3DPie;
                MyChart.Legend.Position = Microsoft.Office.Interop.Graph.XlLegendPosition.xlLegendPositionBottom;
                MyChart.HasTitle= true; MyChart.ChartTitle.Text="经典系列丛书2006年度图书销量";
               
                //创建幻灯片第三页
                MySlide=MySlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank);
                MySlide.FollowMasterBackground=MsoTriState.msoFalse;
                MyShapes=MySlide.Shapes;
                MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect29,"经典编程实例集锦", "Impact", 80,MsoTriState.msoFalse, MsoTriState.msoFalse, 50, 200);
               
                //创建幻灯片第四页
                MySlide=MySlides.Add(4, PowerPoint.PpSlideLayout.ppLayoutBlank);
                MySlide.FollowMasterBackground=MsoTriState.msoFalse;
                MyShapes=MySlide.Shapes;
                MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect30,"精彩编程实例集锦","Impact", 60,MsoTriState.msoFalse, MsoTriState.msoFalse, 150, 10);
                MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect29,"罗斌编著", "Impact", 80, MsoTriState.msoFalse, MsoTriState.msoFalse, 350, 250);
               
                //创建幻灯片第五页
                MySlide=MySlides.Add(5, PowerPoint.PpSlideLayout.ppLayoutBlank);
                MySlide.FollowMasterBackground=MsoTriState.msoFalse;
                MyShapes=MySlide.Shapes;
                MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27,"The End", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, 230, 200);
                MyCount=(int)MySlides.Count; int[] MySlideIdx=new int[MyCount];
                for (i = 0; i < MyCount; i++)
                {
                    MySlideIdx[i]=i+1;
                }
                MySldRng = MySlides.Range(MySlideIdx);
                MySST=MySldRng.SlideShowTransition;
                MySST.AdvanceOnTime=MsoTriState.msoTrue;
                MySST.AdvanceTime=1;
                MySST.EntryEffect=PowerPoint.PpEntryEffect.ppEffectBoxOut;
                MyAssistantOn=MyApp.Assistant.On;
                MyApp.Assistant.On=false;
                MySSS= MyPres.SlideShowSettings;
                MySSS.StartingSlide=1;
                MySSS.EndingSlide=1;
                MySSS.Run();
                MySSWs= MyApp.SlideShowWindows;
                while (MySSWs.Count >= 1)
                {
                    System.Threading.Thread.Sleep(2000);
                }
                if (MyAssistantOn)
                {
                    MyApp.Assistant.On = true;
                    MyApp.Assistant.Visible = false;
                }
                MyPres.Close();
                MyApp.Quit();
            }
            catch (Exception MyEx)
            {
                MessageBox.Show(MyEx.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值