在演示文稿中使用 C++ 应用形状效果
简介
虽然 PowerPoint 中的效果可用于使形状突出,但它们不同于 填充 或轮廓。使用 PowerPoint 效果,您可以在形状上创建逼真的反射,扩散形状的发光等。

PowerPoint 提供了六种可应用于形状的效果。您可以对形状应用一种或多种效果。
某些效果组合比其他组合更好看。为此,PowerPoint 在 Preset 下提供了选项。Preset 选项本质上是两个或多个已知搭配良好的效果的组合。这样,通过选择预设,您无需浪费时间测试或组合不同的效果来寻找合适的组合。
Aspose.Slides 在 EffectFormat 类下提供属性和方法,允许您对 PowerPoint 演示文稿中的形状应用相同的效果。
应用阴影效果
Aspose.Slides for C++ 支持形状的外部和内部阴影。您可以自定义它们的颜色、方向、距离和模糊半径,以匹配您的演示文稿设计。
应用外部阴影
使用外部阴影使卡片或面板在幻灯片背景上突出。阴影延伸到形状边缘之外,产生形状高于幻灯片的效果。调整其颜色、方向、距离和模糊半径,以匹配模板的光照和样式。
下面的 C++ 代码演示如何对矩形应用 外部阴影效果:
#include <DOM/Effects/IOuterShadow.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IEffectFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::RoundCornerRectangle, 20.0f, 20.0f, 200.0f, 100.0f);
auto effectFormat = shape->get_EffectFormat();
effectFormat->EnableOuterShadowEffect();
auto outerShadowEffect = effectFormat->get_OuterShadowEffect();
outerShadowEffect->get_ShadowColor()->set_Color(Color::get_DarkGray());
outerShadowEffect->set_Distance(10);
outerShadowEffect->set_Direction(45.0f);
presentation->Save(u"shadow_effect.pptx", SaveFormat::Pptx);

控制形状旋转时的阴影方向
默认情况下,外部阴影会随形状旋转。要使不同旋转的形状看起来光线来自同一方向,请将 false 传递给 set_RotateShadowWithShape,并使用 set_Direction 为它们的阴影设置相同的角度。
示例创建了两个带有向右偏移(0°)灰色阴影的箭头。随后复制幻灯片并将两个箭头顺时针旋转 90°。
#include <DOM/Effects/IOuterShadow.h>
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IEffectFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = System::MakeObject<Presentation>();
auto before = presentation->get_Slide(0);
auto left = before->get_Shapes()->AddAutoShape(ShapeType::RightArrow, 55, 130, 110, 60);
left->get_FillFormat()->set_FillType(FillType::Solid);
left->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_SteelBlue());
left->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
left->get_EffectFormat()->EnableOuterShadowEffect();
auto shadow = left->get_EffectFormat()->get_OuterShadowEffect();
shadow->get_ShadowColor()->set_Color(Color::get_Gray());
shadow->set_Direction(0);
shadow->set_Distance(42);
shadow->set_BlurRadius(0);
shadow->set_RotateShadowWithShape(true);
auto right = before->get_Shapes()->AddClone(left);
right->set_X(395);
right->get_EffectFormat()->get_OuterShadowEffect()->set_RotateShadowWithShape(false);
auto after = presentation->get_Slides()->AddClone(before);
for (auto i = 0; i < after->get_Shapes()->get_Count(); ++i)
{
after->get_Shape(i)->set_Rotation(90);
}
presentation->Save(u"arrow_shadows.pptx", SaveFormat::Pptx);
在第二张幻灯片上,左侧箭头的阴影向下投射,而右侧箭头的阴影仍向右投射。两者的配置方向仍为 0°。

应用内部阴影
在复现模板的视觉样式时,使用内部阴影可为卡片或面板提供凹陷外观。外部阴影延伸到形状外部,使其呈现凸起效果,而内部阴影则在其边缘内部着色。
调用 EnableInnerShadowEffect,然后配置 InnerShadowEffect。较大的模糊半径值会产生更柔和的边缘。
下面的 C++ 示例创建了一个带有深灰色内部阴影的浅蓝色卡片,并将其保存为 PPTX 文件:
#include <DOM/Effects/IInnerShadow.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IEffectFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <DOM/FillType.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 20.0f, 20.0f, 200.0f, 100.0f);
shape->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_LightBlue());
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
shape->get_EffectFormat()->EnableInnerShadowEffect();
auto shadow = shape->get_EffectFormat()->get_InnerShadowEffect();
shadow->get_ShadowColor()->set_Color(Color::get_DimGray());
shadow->set_Direction(225);
shadow->set_Distance(7);
shadow->set_BlurRadius(6);
presentation->Save(u"inner_shadow_effect.pptx", SaveFormat::Pptx);

要移除内部阴影,请对形状的 effect format 调用 DisableInnerShadowEffect。
应用、修改和移除预设阴影
调用 EnablePresetShadowEffect 并使用 set_Preset 选择 PresetShadowType。要切换样式,可在同一形状上更改预设。调用 DisablePresetShadowEffect 可在保留填充和轮廓的同时移除阴影。
本例在每个阶段保留矩形的副本。两个阴影的偏移均为 10 磅。225° 的方向将第一个阴影置于矩形的左上方,而 45° 的方向将第二个阴影置于右下方。
#include <DOM/Effects/IPresetShadow.h>
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IEffectFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IParagraph.h>
#include <DOM/IParagraphFormat.h>
#include <DOM/IPortionFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/ITextFrame.h>
#include <DOM/Presentation.h>
#include <DOM/PresetShadowType.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/enum.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = System::MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(800, 120, SlideSizeScaleType::DoNotScale);
auto shapes = presentation->get_Slide(0)->get_Shapes();
auto shape = shapes->AddAutoShape(ShapeType::Rectangle, 15, 30, 170, 60);
shape->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_SteelBlue());
auto outline = shape->get_LineFormat()->get_FillFormat();
outline->set_FillType(FillType::Solid);
outline->get_SolidFillColor()->set_Color(Color::get_DarkBlue());
auto textFrame = shape->get_TextFrame();
textFrame->set_Text(u"No shadow");
textFrame->get_Paragraph(0)->get_ParagraphFormat()->get_DefaultPortionFormat()->set_FontHeight(14);
shapes->AddClone(shape);
shape->set_X(shape->get_X() + 200);
auto effectFormat = shape->get_EffectFormat();
effectFormat->EnablePresetShadowEffect();
auto shadow = effectFormat->get_PresetShadowEffect();
shadow->set_Preset(PresetShadowType::TopLeftDropShadow);
shadow->set_Distance(10);
shadow->set_Direction(225);
textFrame->set_Text(System::Enum<PresetShadowType>::GetName(shadow->get_Preset()));
shapes->AddClone(shape);
shape->set_X(shape->get_X() + 200);
shadow->set_Preset(PresetShadowType::BottomRightDropShadow);
shadow->set_Direction(45);
textFrame->set_Text(System::Enum<PresetShadowType>::GetName(shadow->get_Preset()));
shapes->AddClone(shape);
shape->set_X(shape->get_X() + 200);
effectFormat->DisablePresetShadowEffect();
textFrame->set_Text(u"No shadow");
presentation->Save(u"preset_shadows.pptx", SaveFormat::Pptx);
| 原始 | 第一个预设 | 第二个预设 | 移除后 |
|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
应用反射效果
要在 Aspose.Slides for C++ 中应用反射效果,您可以为形状添加类似镜面的反射,并调整距离、透明度和大小等参数。此效果通过为形状提供更精致、专业的外观来提升演示文稿的美感。只需简单代码即可轻松实现,可快速在多个元素上应用,以保持设计一致性。
下面的 C++ 代码演示如何对形状应用 反射效果:
#include <DOM/Effects/IReflection.h>
#include <DOM/IAutoShape.h>
#include <DOM/IEffectFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <DOM/RectangleAlignment.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::RoundCornerRectangle, 20.0f, 20.0f, 200.0f, 100.0f);
auto effectFormat = shape->get_EffectFormat();
effectFormat->EnableReflectionEffect();
auto reflectionEffect = effectFormat->get_ReflectionEffect();
reflectionEffect->set_RectangleAlign(RectangleAlignment::Bottom);
reflectionEffect->set_Direction(90.0f);
reflectionEffect->set_Distance(40);
reflectionEffect->set_BlurRadius(2);
presentation->Save(u"reflection_effect.pptx", SaveFormat::Pptx);

控制反射淡入淡出
set_StartPosAlpha 和 set_EndPosAlpha 指定淡入淡出 起始位置,以沿渐变距离的百分比表示。set_StartReflectionOpacity 和 set_EndReflectionOpacity 指定在这些位置 反射的透明度。例如,位置为 0% 和 50%,不透明度为 80% 和 0% 时,反射从 80% 不透明度开始,渐变至一半时完全透明。
set_Direction 设置反射相对于形状的偏移角度。set_FadeDirection 设置不透明度变化的角度。示例将五个反射都放置在各自矩形的下方,并保持它们的比例、距离和模糊相同。
#include <DOM/Effects/IReflection.h>
#include <DOM/IAutoShape.h>
#include <DOM/IEffectFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/Presentation.h>
#include <DOM/RectangleAlignment.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto presentation = System::MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(720, 200, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
struct FadeSettings
{
int startPos;
int endPos;
int startOpacity;
int endOpacity;
int fadeAngle;
};
const FadeSettings settings[] = {
{0, 100, 80, 0, 90},
{50, 100, 80, 0, 90},
{0, 50, 80, 0, 90},
{0, 100, 50, 25, 90},
{0, 100, 80, 0, 0}
};
auto x = 16.0f;
for (const auto& fade : settings)
{
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, x, 48, 112, 62);
auto effectFormat = shape->get_EffectFormat();
effectFormat->EnableReflectionEffect();
auto reflection = effectFormat->get_ReflectionEffect();
reflection->set_RectangleAlign(RectangleAlignment::Bottom);
reflection->set_Direction(90);
reflection->set_Distance(6);
reflection->set_ScaleHorizontal(100);
reflection->set_ScaleVertical(-100);
reflection->set_BlurRadius(0);
reflection->set_StartPosAlpha(fade.startPos);
reflection->set_EndPosAlpha(fade.endPos);
reflection->set_StartReflectionOpacity(fade.startOpacity);
reflection->set_EndReflectionOpacity(fade.endOpacity);
reflection->set_FadeDirection(fade.fadeAngle);
x += 144;
}
presentation->Save(u"reflection_fade.pptx", SaveFormat::Pptx);

- 基线。 不透明度从 80% 降至 0%,贯穿整个反射高度。
- 后移起始。 上半部分保持 80% 不透明度,淡出在中途开始。
- 提前结束。 反射在中途达到 0% 不透明度,下半部分保持透明。
- 不同不透明度。 淡出跨越整个高度,起始不透明度为 50%,结束为 25%,因此底部边缘仍可见。
- 水平淡出。 将最右侧的矩形与最左侧的基线进行比较。两个反射均向下偏移 90°。将淡出角度从 90° 改为 0°,梯度将向右。
应用发光效果
要在 Aspose.Slides for C++ 中对形状应用发光效果,您可以在形状周围添加柔和、发光的光环,调整颜色和大小等属性。此效果帮助形状突出,并为演示文稿增添吸引人、引人注目的视觉元素。只需少量代码即可轻松实现,提升幻灯片的整体外观。
下面的 C++ 代码演示如何对形状应用 发光效果:
#include <DOM/Effects/IGlow.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IEffectFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::RoundCornerRectangle, 20.0f, 20.0f, 200.0f, 100.0f);
auto effectFormat = shape->get_EffectFormat();
effectFormat->EnableGlowEffect();
auto glowEffect = effectFormat->get_GlowEffect();
glowEffect->get_Color()->set_Color(Color::get_Magenta());
glowEffect->set_Radius(15);
presentation->Save(u"glow_effect.pptx", SaveFormat::Pptx);

应用柔化边缘效果
要在 Aspose.Slides for C++ 中应用柔化边缘效果,您可以在形状的边缘创建平滑、模糊的过渡。此效果增添更细腻、精致的外观,适合需要柔和外观的设计。您可以轻松调整半径等参数,以在演示文稿中的各种形状上实现所需效果。
下面的 C++ 代码演示如何对形状应用 柔化边缘:
#include <DOM/Effects/ISoftEdge.h>
#include <DOM/IAutoShape.h>
#include <DOM/IEffectFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::RoundCornerRectangle, 20.0f, 20.0f, 200.0f, 150.0f);
auto effectFormat = shape->get_EffectFormat();
effectFormat->EnableSoftEdgeEffect();
auto softEdgeEffect = effectFormat->get_SoftEdgeEffect();
softEdgeEffect->set_Radius(8);
presentation->Save(u"soft_edges_effect.pptx", SaveFormat::Pptx);

应用模糊效果
使用形状模糊可在保持形状可编辑的同时软化其填充和轮廓。对形状的 effect format 调用 SetBlurEffect。半径以磅为单位(1/72 英寸),使用非负值,值越大模糊越强。半径为零时外观保持不变。
Grow 设置控制效果的可见边界。设置为 false 时,模糊被裁剪到原始绘制边界(包括轮廓)。设置为 true 时,模糊可超出这些边界。两种设置均不改变形状的位置或尺寸。
下面的比较始终使用相同的填充、轮廓和形状尺寸。顶行使用 grow = false;底行使用 grow = true。

此 C++ 示例创建一个橙色矩形并配以深蓝色轮廓,先应用 5 磅模糊,然后再次调用 SetBlurEffect 将其更新为 15 磅模糊。它保存两种模糊状态,然后调用 DisableBlurEffect 并保存无模糊的形状。
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IEffectFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <DOM/FillType.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 100.0f, 80.0f, 160.0f, 100.0f);
shape->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Orange());
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_DarkBlue());
shape->get_LineFormat()->set_Width(8);
shape->get_EffectFormat()->SetBlurEffect(5, false);
presentation->Save(u"shape_blur_5.pptx", SaveFormat::Pptx);
shape->get_EffectFormat()->SetBlurEffect(15, true);
presentation->Save(u"shape_blur_15.pptx", SaveFormat::Pptx);
shape->get_EffectFormat()->DisableBlurEffect();
presentation->Save(u"shape_blur_removed.pptx", SaveFormat::Pptx);
形状模糊同时影响填充和轮廓。柔化边缘效果只淡化形状的边缘,而阴影模糊仅软化阴影。本例直接对形状的 effect format 应用模糊,而不将形状转换为图像,图像变换效果则作用于图像内容。
应用填充叠加
形状的 get_FillFormat 方法返回其基础填充格式。调用 EnableFillOverlayEffect 可添加第二层填充。对由 get_FillOverlayEffect 返回的叠加层使用 get_FillFormat 来设置叠加颜色。将 FillBlendMode 值传递给 set_Blend 以控制两种填充的组合方式。
本例保留原始蓝色矩形以作比较。它在一个副本上应用实心珊瑚色叠加,然后在下一个副本上仅更改混合模式。最后一个副本调用 DisableFillOverlayEffect 移除叠加层,显示基础填充。
#include <DOM/Effects/IFillOverlay.h>
#include <DOM/FillBlendMode.h>
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IEffectFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = System::MakeObject<Presentation>();
auto shapes = presentation->get_Slide(0)->get_Shapes();
auto original = shapes->AddAutoShape(ShapeType::Rectangle, 10, 15, 140, 90);
original->get_FillFormat()->set_FillType(FillType::Solid);
original->get_FillFormat()->get_SolidFillColor()->set_Color(Color::FromArgb(80, 140, 200));
original->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
auto multiply = shapes->AddClone(original);
multiply->set_X(170);
multiply->get_EffectFormat()->EnableFillOverlayEffect();
auto overlay = multiply->get_EffectFormat()->get_FillOverlayEffect();
overlay->get_FillFormat()->set_FillType(FillType::Solid);
overlay->get_FillFormat()->get_SolidFillColor()->set_Color(Color::FromArgb(200, 100, 80));
overlay->set_Blend(FillBlendMode::Multiply);
auto screen = shapes->AddClone(multiply);
screen->set_X(330);
screen->get_EffectFormat()->get_FillOverlayEffect()->set_Blend(FillBlendMode::Screen);
auto removed = shapes->AddClone(multiply);
removed->set_X(490);
removed->get_EffectFormat()->DisableFillOverlayEffect();
presentation->Save(u"fill_overlay.pptx", SaveFormat::Pptx);
在 PowerPoint 中,Multiply 使蓝色矩形变暗,而 Screen 使其变亮。叠加层移除后,最后一个矩形与原始矩形相同。

常见问题
我可以对同一形状应用多个效果吗?
是的,您可以在单个形状上组合不同的效果,例如阴影、反射和发光,以实现更具动感的外观。
我可以对哪些形状应用效果?
您可以对多种形状应用效果,包括自定义形状、图表、表格、图片、SmartArt 对象、OLE 对象等。
我可以对组合形状应用效果吗?
是的,您可以对组合形状应用效果。该效果会应用于整个组合。



