C#与PowerPoint 2010集成

C#与PowerPoint 2010集成,c#,ms-office,powerpoint,C#,Ms Office,Powerpoint,我正在制作“东西”(我还不知道如何定义它),我要做的是创建一个pptx演示文稿和一些幻灯片。。。但我面临一些问题:我无法更改幻灯片和文本框的背景色/图像。。。我想不出来。。。有人能帮我吗?关于在幻灯片上设置对象的颜色,您可能缺少的一点是COM Interop对颜色的思考与您在.NET Framework中可能使用的略有不同 在.NET Framework中,我们使用恰当命名的表示颜色,它封装了颜色的alpha通道值及其红色、绿色和蓝色组件。但是,COM Interop将颜色表示为整数值,格式为B

我正在制作“东西”(我还不知道如何定义它),我要做的是创建一个
pptx
演示文稿和一些幻灯片。。。但我面临一些问题:我无法更改幻灯片和文本框的背景色/图像。。。我想不出来。。。有人能帮我吗?

关于在幻灯片上设置对象的颜色,您可能缺少的一点是COM Interop对颜色的思考与您在.NET Framework中可能使用的略有不同

在.NET Framework中,我们使用恰当命名的表示颜色,它封装了颜色的alpha通道值及其红色、绿色和蓝色组件。但是,COM Interop将颜色表示为
整数
值,格式为
BGR
。这意味着红色、绿色和蓝色的组件值实际上存储为蓝色、绿色和红色

然而,.NET Framework提供了一种简单的内置方式来在这两种颜色格式之间进行转换:和方法。因此,您应该能够使用以下代码更改PowerPoint幻灯片的背景色:

//Create a color
Color myBackgroundColor = Color.LimeGreen;

//Translate to an OLE color
int oleColor = ColorTranslator.ToOle(myBackgroundColor);

//Set the background color of the slide
mySlide.Background.Fill.ForeColor.RGB = oleColor;
相反,要将当前背景色检索为.NET颜色,则必须执行相反的操作:

//Get the current background color of the slide
int oleColor = mySlide.Background.Fill.ForeColor.RGB;

//Translate to a .NET Color
color myBackgroundColor = ColorTranslator.FromOle(oleColor);
当然,如果要设置形状的前景(填充)颜色,只需设置其
ForeColor
属性,如下所示:

//Create a color
Color myForegroundColor = Color.Aqua;

//Translate to an OLE color
int oleColor = ColorTranslator.ToOle(myForegroundColor );

//Set the foreground color of a shape
myShape.Fill.ForeColor.RGB = oleColor;

在幻灯片上设置对象颜色方面,您可能缺少的一点是,COM Interop对颜色的考虑与您在.NET Framework中可能使用的颜色略有不同

在.NET Framework中,我们使用恰当命名的表示颜色,它封装了颜色的alpha通道值及其红色、绿色和蓝色组件。但是,COM Interop将颜色表示为
整数
值,格式为
BGR
。这意味着红色、绿色和蓝色的组件值实际上存储为蓝色、绿色和红色

然而,.NET Framework提供了一种简单的内置方式来在这两种颜色格式之间进行转换:和方法。因此,您应该能够使用以下代码更改PowerPoint幻灯片的背景色:

//Create a color
Color myBackgroundColor = Color.LimeGreen;

//Translate to an OLE color
int oleColor = ColorTranslator.ToOle(myBackgroundColor);

//Set the background color of the slide
mySlide.Background.Fill.ForeColor.RGB = oleColor;
相反,要将当前背景色检索为.NET颜色,则必须执行相反的操作:

//Get the current background color of the slide
int oleColor = mySlide.Background.Fill.ForeColor.RGB;

//Translate to a .NET Color
color myBackgroundColor = ColorTranslator.FromOle(oleColor);
当然,如果要设置形状的前景(填充)颜色,只需设置其
ForeColor
属性,如下所示:

//Create a color
Color myForegroundColor = Color.Aqua;

//Translate to an OLE color
int oleColor = ColorTranslator.ToOle(myForegroundColor );

//Set the foreground color of a shape
myShape.Fill.ForeColor.RGB = oleColor;

我想出来了。。。为了更改幻灯片的背景,在更改颜色/图片之前,必须首先调用Fill.background()方法!!!谢谢大家的帮助

我想出来了。。。为了更改幻灯片的背景,在更改颜色/图片之前,必须首先调用Fill.background()方法!!!谢谢大家的帮助

我遇到了同样的问题,直到我发现您需要将slide.followsmasterbackground设置为false:

Slide slide = presentation.Slides.AddSlide(presentation.Slides.Count + 1, layout);
slide.FollowMasterBackground = Microsoft.Office.Core.MsoTriState.msoFalse;
slide.Background.Fill.ForeColor.RGB = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkBlue);

在发现您需要将slide.FollowMasterBackground设置为false之前,我遇到了相同的问题:

Slide slide = presentation.Slides.AddSlide(presentation.Slides.Count + 1, layout);
slide.FollowMasterBackground = Microsoft.Office.Core.MsoTriState.msoFalse;
slide.Background.Fill.ForeColor.RGB = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkBlue);

我想回答你的问题,但我面临一些问题。您是想通过Office Interop从C#更改幻灯片的背景色,还是想在PowerPoint中执行此操作?文本框在哪里?如何在PowerPoint幻灯片上获取文本框?从c#,我想构建一个“控制”PowerPoint的程序。。。就像一个API。。。元素(形状)是根据用户请求创建的。。。别担心他们在哪里。。。我有他们。。。我会知道他们在哪里。。。我有指针。。。TKS!没有任何指针。请向我们展示您的代码。它是:public void CreateNewSlide(Presentation p,int index){Slide newSlide=p.Slides.AddSlide(index,p.SlideMaster.CustomLayouts[0]);//更改newSlide的背景图像?//添加文本框Shape newTextBox=newSlide.Shapes.AddTextbox(MsoTextOrientation.msotextorientationhorizontation,0,015,75);//设置newTextBox.TextFrame.TextRange.text=“Hello world!!!”;//设置背景色?}我正试图回答你的问题,但我遇到了一些问题。您是想通过Office Interop从C#更改幻灯片的背景色,还是想在PowerPoint中执行此操作?文本框在哪里?如何在PowerPoint幻灯片上获取文本框?从c#,我想构建一个“控制”PowerPoint的程序。。。就像一个API。。。元素(形状)是根据用户请求创建的。。。别担心他们在哪里。。。我有他们。。。我会知道他们在哪里。。。我有指针。。。TKS!没有任何指针。请向我们展示您的代码。它是:public void CreateNewSlide(Presentation p,int index){Slide newSlide=p.Slides.AddSlide(index,p.SlideMaster.CustomLayouts[0]);//更改newSlide的背景图像?//添加文本框Shape newTextBox=newSlide.Shapes.AddTextbox(MsoTextOrientation.msotextorientationhorizontation,0,015,75);//设置newTextBox.TextFrame.TextRange.text=“Hello world!!!”;//设置背景色?}好的,它对文本框和图片有效。。。但它不适用于幻灯片背景。。。不管怎样!你真是帮了大忙!!!我很抱歉。我相信您也希望
ForeColor
设置幻灯片背景<代码>背景色仅用于渐变、图案填充等。我已更新了答案。是的。。。它不起作用。。。它不会改变,但不会返回异常或其他任何东西…@莱昂纳多:如果你编辑你的原始问题并发布你试图使用的代码,也许我可以更好地了解问题所在。好吧,它确实适用于文本框和图片。。。但它不适用于幻灯片背景。。。不管怎样!你真是帮了大忙!!!我很抱歉。我相信您也希望
ForeColor
设置幻灯片背景<代码>背景色仅用于渐变、图案填充等。我已更新了答案。是的。。。它不起作用。。。不是茶