C# VSTO:在 PowerPoint 中获取最新的填充颜色

C# VSTO: Getting Latest Fill Color in PowerPoint

如何从 C# VSTO 加载项中访问 PowerPoint 中上次使用的颜色?

在下面的示例中,我指的是桶填充工具指示的橙色。

为了子孙后代

这是已接受答案的 C# 版本:

        var shape = Globals.ThisAddIn.Application.ActivePresentation.Slides[1]
            .Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 0, 0, 100, 100);
        shape.Select();
        Globals.ThisAddIn.Application.CommandBars.ExecuteMso("ShapeFillColorPicker");
        var color = shape.Fill.ForeColor.RGB;
        shape.Delete();

除了徘徊 XML,它可能会或可能不会吐出任何有用的东西,我不知道有什么方法可以通过对象模型做到这一点,但你可以画一个矩形,调用按钮的给它上色的动作,抓住颜色然后删除矩形。以下是 VBA 中的操作方法:

Function GetColorBucketColor() As Long
    Dim oSh As Shape
    Set oSh = ActivePresentation.Slides(1).Shapes.AddShape(msoShapeRectangle, 0, 0, 100, 100)
    oSh.Select
    Application.CommandBars.ExecuteMso ("ShapeFillColorPicker")
    GetColorBucketColor = oSh.Fill.ForeColor.RGB
    oSh.Delete
End Function

在现实生活中,情况要复杂一些; ShapeFill 按钮中的颜色可能是主题颜色,也可能是标准的 RGB 颜色;这将 return RGB 值。将其他形状设置为这种颜色会产生相同的外观,但形状将不再遵循主题。有时重要,有时不重要。

另一个潜在的缺点是要使它起作用,必须选择一些东西。这意味着你将无法在不可见的情况下进行window,并且PPT选择新绘制的形状时会出现一些问题。