Định dạng các hình PowerPoint trong C++
Giới thiệu
Định dạng các hình dạng trong PowerPoint bằng cách điều chỉnh viền, màu nền và hiệu ứng của chúng. Aspose.Slides cho C++ cho phép bạn kiểm soát các cài đặt này bằng chương trình, bao gồm độ trong suốt, xoay và định dạng 3D.

Aspose.Slides cho C++ cung cấp các giao diện và phương thức cho phép bạn định dạng hình dạng bằng những tùy chọn có trong PowerPoint.
Định dạng Đường
Sử dụng IShape::get_LineFormat để điều khiển viền của hình. Ví dụ này tạo một hình chữ nhật không tô màu có viền màu xanh dương, dạng gạch đứt. ILineFormat::set_Style chọn một đường hợp chất dày-mỏng, trong khi ILineFormat::set_Width đặt độ rộng của nó thành 7 điểm và ILineFormat::set_DashStyle chọn mẫu gạch.
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/LineDashStyle.h>
#include <DOM/LineStyle.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 150, 75);
shape->get_FillFormat()->set_FillType(FillType::NoFill);
shape->get_LineFormat()->set_Style(LineStyle::ThickThin);
shape->get_LineFormat()->set_Width(7);
shape->get_LineFormat()->set_DashStyle(LineDashStyle::Dash);
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Blue());
presentation->Save(u"formatted_lines.pptx", SaveFormat::Pptx);
presentation->Dispose();
Kết quả:

Đặt mẫu gạch tùy chỉnh cho Đường
Sử dụng ILineFormat::set_CustomDashPattern khi các kiểu đường đã xác định trước không cung cấp độ dài gạch và khoảng trống bạn cần, ví dụ cho các viền thương hiệu hoặc sơ đồ kỹ thuật. Gán một mẫu sẽ tự động thay đổi kiểu gạch thành LineDashStyle::Custom.
Các mục trong mảng xen kẽ giữa độ dài gạch và khoảng trống, bắt đầu bằng gạch. Mỗi giá trị là một bội số của độ rộng đường: { 4, 2 } ở độ rộng 3 điểm tạo ra một gạch dài 12 điểm và một khoảng trống dài 6 điểm. Sử dụng các cặp giá trị hữu hạn, không âm để định nghĩa các đoạn gạch‑khoảng trống hoàn chỉnh. Ví dụ này dùng các giá trị dương để giữ cho mỗi gạch và khoảng trống đều nhìn thấy được.
Ví dụ này tạo năm đường màu xanh với cùng độ dài và đầu thẳng. Ba đường đầu tiên so sánh các mẫu ở độ rộng 3 điểm. Đường thứ tư lặp lại mẫu đầu tiên với độ rộng gấp đôi, làm gấp đôi cả độ dài gạch và khoảng trống. Đường thứ năm chuyển từ mẫu tùy chỉnh sang đường liền.
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/LineArrowheadStyle.h>
#include <DOM/LineCapStyle.h>
#include <DOM/LineDashStyle.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
#include <system/array.h>
#include <array>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(600, 300, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
const auto patterns = std::array<ArrayPtr<float>, 5>{
MakeArray<float>({ 4, 2 }),
MakeArray<float>({ 1, 2 }),
MakeArray<float>({ 6, 2, 1, 2 }),
MakeArray<float>({ 4, 2 }),
MakeArray<float>({ 4, 2 })
};
for (auto i = 0; i < patterns.size(); i++)
{
auto line = slide->get_Shapes()->AddAutoShape(ShapeType::Line, 50, 50 + i * 50, 500, 0);
line->get_LineFormat()->set_Width(i == 3 ? 6 : 3);
line->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
line->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_DarkBlue());
line->get_LineFormat()->set_CapStyle(LineCapStyle::Flat);
line->get_LineFormat()->set_BeginArrowheadStyle(LineArrowheadStyle::None);
line->get_LineFormat()->set_EndArrowheadStyle(LineArrowheadStyle::None);
line->get_LineFormat()->set_CustomDashPattern(patterns[i]);
}
// Return the last line to a solid style.
slide->get_Shape(4)->get_LineFormat()->set_DashStyle(LineDashStyle::Solid);
presentation->Save(u"custom_dash_patterns.pptx", SaveFormat::Pptx);
presentation->Dispose();
Để quay lại kiểu gạch đã xác định trước, truyền LineDashStyle::Dash cho ILineFormat::set_DashStyle. Kiểu đã chọn sẽ điều khiển cách hiển thị của đường.
Kết quả trong PowerPoint, từ trên xuống dưới: {4, 2}, 3 pt; {1, 2}, 3 pt; {6, 2, 1, 2}, 3 pt; {4, 2}, 6 pt; và Solid, 3 pt.

Đặt Kiểu Đầu Đường
Sử dụng ILineFormat::set_CapStyle để đặt đầu của một đường mở. Phân loại LineCapStyle cung cấp ba kiểu đầu rõ ràng:
Flat: Đường dừng lại tại điểm cuối.Round: Một đầu bán tròn kéo dài ra ngoài điểm cuối một nửa độ rộng đường.Square: Một đầu hình vuông kéo dài ra ngoài điểm cuối một nửa độ rộng đường.
Phân loại cũng bao gồm NotDefined, có nghĩa là không có kiểu đầu nào được chỉ định rõ ràng.
Đầu khác với mũi tên, mà thêm ký hiệu vào đầu đường, và các nối, mà điều khiển góc giữa các đoạn đường. Ví dụ này tắt các mũi tên thông qua ILineFormat::set_BeginArrowheadStyle và ILineFormat::set_EndArrowheadStyle để chỉ so sánh các hình dạng đầu.
Ví dụ sau tạo ba đường ngang gần như giống nhau, rộng 20 điểm. Từ trên xuống dưới, chúng sử dụng đầu flat, round và square. Các điểm cuối của chúng có cùng tọa độ ngang; đầu round và square mở rộng ra ngoài mỗi điểm cuối 10 điểm.
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/LineArrowheadStyle.h>
#include <DOM/LineCapStyle.h>
#include <DOM/LineDashStyle.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
#include <array>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(500, 240, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
const auto capStyles = std::array<LineCapStyle, 3>{ LineCapStyle::Flat, LineCapStyle::Round, LineCapStyle::Square };
for (auto i = 0; i < capStyles.size(); i++)
{
auto line = slide->get_Shapes()->AddAutoShape(ShapeType::Line, 100, 50 + i * 70, 300, 0);
line->get_LineFormat()->set_Width(20);
line->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
line->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_DarkBlue());
line->get_LineFormat()->set_DashStyle(LineDashStyle::Solid);
line->get_LineFormat()->set_CapStyle(capStyles[i]);
line->get_LineFormat()->set_BeginArrowheadStyle(LineArrowheadStyle::None);
line->get_LineFormat()->set_EndArrowheadStyle(LineArrowheadStyle::None);
}
presentation->Save(u"line_caps.pptx", SaveFormat::Pptx);
presentation->Dispose();
Kết quả trong PowerPoint, từ trên xuống dưới: Flat, Round, và Square.

Áp dụng Hiệu Ứng Phác Thảo cho Đường Hình
Một hiệu ứng phác thảo làm cho đường hình trông như vẽ tay. Sử dụng IShape::get_LineFormat để truy cập cài đặt đường, ILineFormat::get_SketchFormat để truy cập cài đặt phác thảo, và ISketchFormat::set_SketchType để chọn một giá trị từ phân loại LineSketchType.
Đoạn mã C++ sau cho thấy cách áp dụng hiệu ứng LineSketchType::Curved, đọc giá trị đã gán rõ ràng, và loại bỏ hiệu ứng bằng LineSketchType::None:
#include <DOM/IAutoShape.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISketchFormat.h>
#include <DOM/ISlide.h>
#include <DOM/LineSketchType.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <system/console.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 200, 100);
auto sketchFormat = shape->get_LineFormat()->get_SketchFormat();
sketchFormat->set_SketchType(LineSketchType::Curved);
auto explicitSketchType = sketchFormat->get_SketchType();
Console::WriteLine(u"Explicit sketch type: {0}", explicitSketchType);
sketchFormat->set_SketchType(LineSketchType::None);
presentation->Dispose();
Giá trị trả về bởi ISketchFormat::get_SketchType đại diện cho cài đặt được gán trực tiếp cho hình. Nếu định dạng đường có thể được kế thừa từ một chủ đề, slide mẫu, hoặc slide bố cục, hãy sử dụng ILineFormat::GetEffective, truy cập ILineFormatEffectiveData::get_SketchFormat, và đọc ISketchFormatEffectiveData::get_SketchType. Giá trị effective phản ánh định dạng thực tế được áp dụng sau khi kế thừa được giải quyết. Ví dụ này yêu cầu một bản trình bày hiện có có ít nhất một hình trên slide đầu tiên và in ra cả hai giá trị:
#include <DOM/ILineFormat.h>
#include <DOM/ILineFormatEffectiveData.h>
#include <DOM/IShape.h>
#include <DOM/ISketchFormat.h>
#include <DOM/ISketchFormatEffectiveData.h>
#include <DOM/ISlide.h>
#include <DOM/LineSketchType.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
auto presentation = MakeObject<Presentation>(u"presentation.pptx");
auto shape = presentation->get_Slide(0)->get_Shape(0);
auto lineFormat = shape->get_LineFormat();
auto explicitSketchType = lineFormat->get_SketchFormat()->get_SketchType();
auto effectiveLineFormat = lineFormat->GetEffective();
auto effectiveSketchType = effectiveLineFormat->get_SketchFormat()->get_SketchType();
Console::WriteLine(u"Explicit sketch type: {0}", explicitSketchType);
Console::WriteLine(u"Effective sketch type: {0}", effectiveSketchType);
presentation->Dispose();
Định dạng Kiểu Nối
Dưới đây là ba tùy chọn kiểu nối:
- Round
- Miter
- Bevel
Kiểu nối điều khiển cách hai đoạn viền gặp nhau tại một góc. Sử dụng Round cho các góc tròn, Miter cho các góc nhọn, hoặc Bevel cho các góc được làm phẳng.

Ví dụ này tạo ba hình chữ nhật đen với viền xanh dày 15 điểm. Nó đặt ILineFormat::set_JoinStyle thành các giá trị miter, bevel và round của LineJoinStyle, tương ứng, để so sánh cách vẽ các góc.
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/LineJoinStyle.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto miterShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 20, 20, 150, 75);
auto bevelShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 210, 20, 150, 75);
auto roundShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 20, 135, 150, 75);
miterShape->get_FillFormat()->set_FillType(FillType::Solid);
miterShape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
bevelShape->get_FillFormat()->set_FillType(FillType::Solid);
bevelShape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
roundShape->get_FillFormat()->set_FillType(FillType::Solid);
roundShape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
miterShape->get_LineFormat()->set_Width(15);
bevelShape->get_LineFormat()->set_Width(15);
roundShape->get_LineFormat()->set_Width(15);
miterShape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
miterShape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Blue());
bevelShape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
bevelShape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Blue());
roundShape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
roundShape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Blue());
miterShape->get_LineFormat()->set_JoinStyle(LineJoinStyle::Miter);
bevelShape->get_LineFormat()->set_JoinStyle(LineJoinStyle::Bevel);
roundShape->get_LineFormat()->set_JoinStyle(LineJoinStyle::Round);
miterShape->get_TextFrame()->set_Text(u"Miter Join Style");
bevelShape->get_TextFrame()->set_Text(u"Bevel Join Style");
roundShape->get_TextFrame()->set_Text(u"Round Join Style");
presentation->Save(u"join_styles.pptx", SaveFormat::Pptx);
presentation->Dispose();
Giới Hạn Nối Miter ở Các Góc Nhọn
Sử dụng ILineFormat::set_MiterLimit kết hợp với ILineFormat::set_JoinStyle đặt thành LineJoinStyle::Miter để giới hạn độ dài mà một góc nhọn có thể mở rộng.
Giới hạn là tỉ lệ giữa khoảng cách giữa góc trong và góc ngoài của nối so với độ rộng đường. Ví dụ, giá trị 8 cho phép độ dài miter lên tới tám lần độ rộng đường; đây không phải là một đo lường bằng điểm. Nếu góc sẽ vượt quá giới hạn, PowerPoint sẽ sử dụng nối bevel tại góc đó. Giới hạn lớn hơn cho phép các điểm nhọn hơn. Cài đặt này chỉ ảnh hưởng đến nối miter và không có tác động nhìn thấy được khi góc vẫn nằm trong cả hai giới hạn.
Ví dụ này tạo hai tam giác không tô màu giống hệt nhau, viền dày 20 điểm. Tam giác trái sử dụng giới hạn 2, còn bên phải sử dụng 8. Mỗi tam giác rộng 80 điểm và cao 160 điểm, cho tỉ lệ độ dài miter‑to‑line‑width ở đỉnh trên khoảng 4.12. Do đó, PowerPoint sẽ bevel đỉnh trái và giữ nguyên đỉnh phải.
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/LineJoinStyle.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
#include <array>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(500, 320, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
const auto miterLimits = std::array<float, 2>{ 2.0f, 8.0f };
for (auto i = 0; i < miterLimits.size(); i++)
{
auto triangle = slide->get_Shapes()->AddAutoShape(ShapeType::Triangle, 90 + i * 240, 110, 80, 160);
triangle->get_FillFormat()->set_FillType(FillType::NoFill);
triangle->get_LineFormat()->set_Width(20);
triangle->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
triangle->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_DarkBlue());
triangle->get_LineFormat()->set_JoinStyle(LineJoinStyle::Miter);
triangle->get_LineFormat()->set_MiterLimit(miterLimits[i]);
}
presentation->Save(u"miter_limits.pptx", SaveFormat::Pptx);
presentation->Dispose();
Kết quả trong PowerPoint: Miter limit 2 ở bên trái và miter limit 8 ở bên phải.

Đổ Gradient
Một độ đổ gradient pha trộn các màu qua một hình. Ví dụ này đổ một hình ellip bằng gradient tuyến tính với góc 45 độ từ màu đỏ sang màu tím bằng cách sử dụng IFillFormat::get_GradientFormat. Phương thức IGradientStopCollection::Add đặt các điểm dừng màu ở 0 và 1, tương ứng với đầu và cuối gradient. Xóa các điểm dừng hiện có đảm bảo chỉ hai màu này được dùng.
#include <DOM/FillType.h>
#include <DOM/GradientShape.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IGradientFormat.h>
#include <DOM/IGradientStopCollection.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <DOM/PresetColor.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Ellipse, 50, 50, 150, 75);
shape->get_FillFormat()->set_FillType(FillType::Gradient);
shape->get_FillFormat()->get_GradientFormat()->set_GradientShape(GradientShape::Linear);
shape->get_FillFormat()->get_GradientFormat()->set_LinearGradientAngle(45);
shape->get_FillFormat()->get_GradientFormat()->get_GradientStops()->Clear();
shape->get_FillFormat()->get_GradientFormat()->get_GradientStops()->Add(1.0f, PresetColor::Purple);
shape->get_FillFormat()->get_GradientFormat()->get_GradientStops()->Add(0.0f, PresetColor::Red);
presentation->Save(u"gradient_fill.pptx", SaveFormat::Pptx);
presentation->Dispose();
Kết quả:

Nếu một gradient trông khác nhau trong hình ảnh đã xuất so với trong PowerPoint, xem mục Choose a Gradient Rendering Style. Bạn có thể chọn kiểu render mà không thay đổi gradient đã lưu trong bản trình bày.
Đặt Góc Gradient Tuyến Tính
Sử dụng IGradientFormat::set_LinearGradientAngle để điều khiển hướng của độ đổ GradientShape::Linear. Góc đo bằng độ và tăng theo chiều kim đồng hồ: 0 chạy từ trái sang phải, 90 từ trên xuống dưới, 180 từ phải sang trái, và 270 từ dưới lên trên. Các góc phân số cũng được hỗ trợ. Các góc nằm ngoài khoảng [0, 360) sẽ được chuẩn hoá thành một vòng quay; ví dụ, -45 trở thành 315 và 360 trở thành 0.
Ví dụ này tạo sáu hình chữ nhật với các điểm dừng đỏ, xanh lá và xanh dương giống hệt nhau tại các vị trí 0, 0.5, và 1. Tính năng scaling bị tắt để góc có thể được so sánh độc lập với tỷ lệ của các hình chữ nhật. Các góc, từ trái sang phải, lần lượt là 0, 45, và 90 ở hàng đầu, và 135, 180, và 270 ở hàng thứ hai.
#include <DOM/FillType.h>
#include <DOM/GradientShape.h>
#include <DOM/IAutoShape.h>
#include <DOM/IFillFormat.h>
#include <DOM/IGradientFormat.h>
#include <DOM/IGradientStopCollection.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/NullableBool.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
#include <array>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(720, 400, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
const auto angles = std::array<float, 6>{ 0, 45, 90, 135, 180, 270 };
for (auto i = 0; i < angles.size(); i++)
{
auto x = 30 + (i % 3) * 230;
auto y = 30 + (i / 3) * 190;
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, x, y, 200, 150);
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
shape->get_FillFormat()->set_FillType(FillType::Gradient);
auto gradient = shape->get_FillFormat()->get_GradientFormat();
gradient->set_GradientShape(GradientShape::Linear);
gradient->get_GradientStops()->Clear();
gradient->get_GradientStops()->Add(0.0f, Color::get_Red());
gradient->get_GradientStops()->Add(0.5f, Color::get_Lime());
gradient->get_GradientStops()->Add(1.0f, Color::get_Blue());
gradient->set_LinearGradientScaled(NullableBool::False);
gradient->set_LinearGradientAngle(angles[i]);
}
presentation->Save(u"linear_gradient_angles.pptx", SaveFormat::Pptx);
presentation->Dispose();
Kết quả trong PowerPoint:

Scale Gradient Tuyến Tính Với Hình
IGradientFormat::set_LinearGradientScaled nhận một giá trị NullableBool. Đặt nó rõ ràng thành False để giữ góc gradient hình học khi tỷ lệ hình thay đổi, hoặc thành True để scale gradient cùng với chiều rộng và chiều cao của hình. Khi scaling được bật, góc nhìn thấy có thể khác với góc đã gán trên một hình không vuông.
Ví dụ sau sử dụng góc 45 độ và cùng các điểm dừng màu như so sánh góc. Cột trái dùng False; cột phải dùng True. Mỗi cột chứa một hình chữ nhật rộng ở trên và một hình chữ nhật cao ở dưới. Cả hai bắt đầu với cùng kích thước rộng; việc thay đổi chiều rộng và chiều cao của hình chữ nhật thứ hai cho thấy cách màu nền phản hồi khi thay đổi kích thước mà không thay đổi cài đặt gradient.
#include <DOM/FillType.h>
#include <DOM/GradientShape.h>
#include <DOM/IAutoShape.h>
#include <DOM/IFillFormat.h>
#include <DOM/IGradientFormat.h>
#include <DOM/IGradientStopCollection.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/NullableBool.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
#include <array>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(720, 460, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
const auto scalingOptions = std::array<NullableBool, 2>{ NullableBool::False, NullableBool::True };
for (auto column = 0; column < scalingOptions.size(); column++)
{
for (auto row = 0; row < 2; row++)
{
auto x = 30 + column * 360;
auto y = row == 0 ? 30 : 150;
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, x, y, 300, 90);
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
shape->get_FillFormat()->set_FillType(FillType::Gradient);
auto gradient = shape->get_FillFormat()->get_GradientFormat();
gradient->set_GradientShape(GradientShape::Linear);
gradient->get_GradientStops()->Clear();
gradient->get_GradientStops()->Add(0.0f, Color::get_Red());
gradient->get_GradientStops()->Add(0.5f, Color::get_Lime());
gradient->get_GradientStops()->Add(1.0f, Color::get_Blue());
gradient->set_LinearGradientAngle(45);
gradient->set_LinearGradientScaled(scalingOptions[column]);
if (row == 1)
{
shape->set_Width(90);
shape->set_Height(280);
}
}
}
presentation->Save(u"linear_gradient_scaling.pptx", SaveFormat::Pptx);
presentation->Dispose();
Kết quả trong PowerPoint, với scaling tắt bên trái và bật bên phải:

Với scaling tắt, các dải màu chéo giữ cùng hướng trong cả hai hình. Khi scaling bật, các dải màu sẽ kéo dãn theo tỷ lệ của hình. Điều này giúp tái tạo gradient của mẫu khi điều chỉnh một hình sang kích thước khác. Trên một hình vuông, bật scaling không thay đổi vẻ ngoài của gradient 45 độ này.
Cả góc và cài đặt scaling đều áp dụng cho gradient tuyến tính. Chúng không điều khiển các độ đổ GradientShape::Radial hoặc GradientShape::Rectangle.
Kết Hợp Góc Với Hướng Gradient
IGradientFormat::set_GradientDirection chọn một kiểu gradient từ phân loại GradientDirection; nó không phải là một thay thế cho góc số. Đối với gradient tuyến tính, các kiểu góc có thể thay đổi vùng màu được phân bố mà không thay đổi góc đã gán thông qua IGradientFormat::set_LinearGradientAngle. Giữ hướng không đổi khi so sánh góc hoặc scaling.
Ví dụ sau áp dụng cùng một cài đặt nhưng theo thứ tự ngược nhau. Hai hình chữ nhật đều có cùng vẻ ngoài: đặt hướng trước hoặc sau góc không làm thay đổi kết quả.
#include <DOM/FillType.h>
#include <DOM/GradientDirection.h>
#include <DOM/GradientShape.h>
#include <DOM/IAutoShape.h>
#include <DOM/IFillFormat.h>
#include <DOM/IGradientFormat.h>
#include <DOM/IGradientStopCollection.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/NullableBool.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
for (auto i = 0; i < 2; i++)
{
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 30, 30 + i * 140, 320, 100);
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
shape->get_FillFormat()->set_FillType(FillType::Gradient);
auto gradient = shape->get_FillFormat()->get_GradientFormat();
gradient->set_GradientShape(GradientShape::Linear);
gradient->get_GradientStops()->Clear();
gradient->get_GradientStops()->Add(0.0f, Color::get_Red());
gradient->get_GradientStops()->Add(0.5f, Color::get_Lime());
gradient->get_GradientStops()->Add(1.0f, Color::get_Blue());
gradient->set_LinearGradientScaled(NullableBool::True);
if (i == 0)
{
gradient->set_GradientDirection(GradientDirection::FromCorner1);
gradient->set_LinearGradientAngle(30);
}
else
{
gradient->set_LinearGradientAngle(30);
gradient->set_GradientDirection(GradientDirection::FromCorner1);
}
}
presentation->Save(u"linear_gradient_direction.pptx", SaveFormat::Pptx);
presentation->Dispose();
Đổ Mẫu
Đổ mẫu áp dụng một thiết kế hai màu, chẳng hạn chấm, sọc hoặc ô cờ. Ví dụ này sử dụng IPatternFormat::set_PatternStyle để chọn mẫu trellis. IPatternFormat::get_BackColor cung cấp màu nền xám nhạt, và IPatternFormat::get_ForeColor cung cấp màu vàng cho mẫu.
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IPatternFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/PatternStyle.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 150, 75);
shape->get_FillFormat()->set_FillType(FillType::Pattern);
shape->get_FillFormat()->get_PatternFormat()->set_PatternStyle(PatternStyle::Trellis);
shape->get_FillFormat()->get_PatternFormat()->get_BackColor()->set_Color(Color::get_LightGray());
shape->get_FillFormat()->get_PatternFormat()->get_ForeColor()->set_Color(Color::get_Yellow());
presentation->Save(u"pattern_fill.pptx", SaveFormat::Pptx);
presentation->Dispose();
Kết quả:

Đổ Hình Ảnh
Đổ hình ảnh sử dụng một bức ảnh bên trong một hình. Ví dụ này yêu cầu một hình ảnh cục bộ và sử dụng Images::FromFile để tải nó vào bộ sưu tập ảnh của bản trình bày. Đặt IPictureFillFormat::set_PictureFillMode thành chế độ lặp lại (tile) sẽ lặp lại ảnh trong hình chữ nhật. Hình ảnh nguồn được sử dụng ở đây được hiển thị dưới đây:

#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IFillFormat.h>
#include <DOM/IImageCollection.h>
#include <DOM/IPictureFillFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlidesPicture.h>
#include <DOM/PictureFillMode.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <IImage.h>
#include <Util/Images.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 255, 130);
shape->get_FillFormat()->set_FillType(FillType::Picture);
shape->get_FillFormat()->get_PictureFillFormat()->set_PictureFillMode(PictureFillMode::Tile);
auto image = Images::FromFile(u"lotus.png");
auto presentationImage = presentation->get_Images()->AddImage(image);
shape->get_FillFormat()->get_PictureFillFormat()->get_Picture()->set_Image(presentationImage);
presentation->Save(u"picture_fill.pptx", SaveFormat::Pptx);
image->Dispose();
presentation->Dispose();
Kết quả:

Lặp Lại Hình Ảnh Như Kết Cấu
Nếu bạn muốn đặt một hình ảnh lặp lại làm kết cấu và tùy chỉnh cách lặp lại, bạn có thể sử dụng các phương thức sau của giao diện IPictureFillFormat và lớp PictureFillFormat:
- set_PictureFillMode: Đặt chế độ độ đổ hình ảnh —
TilehoặcStretch. - set_TileAlignment: Xác định căn chỉnh các tile trong hình.
- set_TileFlip: Điều khiển tile có được lật ngang, dọc, hay cả hai.
- set_TileOffsetX: Đặt độ lệch ngang của tile (theo điểm) từ gốc của hình.
- set_TileOffsetY: Đặt độ lệch dọc của tile (theo điểm) từ gốc của hình.
- set_TileScaleX: Xác định tỷ lệ ngang của tile dưới dạng phần trăm.
- set_TileScaleY: Xác định tỷ lệ dọc của tile dưới dạng phần trăm.
Ví dụ này yêu cầu một hình ảnh cục bộ và cấu hình IPictureFillFormat để lặp lại nó ở 50% kích thước gốc. Độ lệch −32 điểm di chuyển các tile theo chiều ngang và dọc; căn chỉnh góc dưới‑phải và lật cả hai hướng xác định cách kết cấu lặp lại được sắp xếp.
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IFillFormat.h>
#include <DOM/IImageCollection.h>
#include <DOM/IPictureFillFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlidesPicture.h>
#include <DOM/PictureFillMode.h>
#include <DOM/Presentation.h>
#include <DOM/RectangleAlignment.h>
#include <DOM/ShapeType.h>
#include <DOM/TileFlip.h>
#include <Export/SaveFormat.h>
#include <IImage.h>
#include <Util/Images.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
auto presentation = MakeObject<Presentation>();
auto firstSlide = presentation->get_Slide(0);
auto shape = firstSlide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 190, 95);
shape->get_FillFormat()->set_FillType(FillType::Picture);
auto sourceImage = Images::FromFile(u"lotus.png");
auto presentationImage = presentation->get_Images()->AddImage(sourceImage);
auto pictureFillFormat = shape->get_FillFormat()->get_PictureFillFormat();
pictureFillFormat->get_Picture()->set_Image(presentationImage);
pictureFillFormat->set_PictureFillMode(PictureFillMode::Tile);
pictureFillFormat->set_TileOffsetX(-32);
pictureFillFormat->set_TileOffsetY(-32);
pictureFillFormat->set_TileScaleX(50);
pictureFillFormat->set_TileScaleY(50);
pictureFillFormat->set_TileAlignment(RectangleAlignment::BottomRight);
pictureFillFormat->set_TileFlip(TileFlip::FlipBoth);
presentation->Save(u"tile.pptx", SaveFormat::Pptx);
sourceImage->Dispose();
presentation->Dispose();
Kết quả:

Đổ Màu Đơn
Đổ màu đơn cho hình một màu duy nhất, đồng nhất. Ví dụ này đặt IFillFormat::set_FillType thành chế độ đổ màu đơn và gán màu vàng qua IFillFormat::get_SolidFillColor, sau đó lưu hình chữ nhật vào bản trình bày.
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.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>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 150, 75);
shape->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Yellow());
presentation->Save(u"solid_color_fill.pptx", SaveFormat::Pptx);
presentation->Dispose();
Kết quả:

Sao Chép Màu Khi Giữ Tham Chiếu Chủ Đề
Sử dụng IColorFormat::CopyFrom để sao chép một định nghĩa màu, bao gồm loại màu, tham chiếu chủ đề và các phép biến đổi. Điều này hữu ích khi chuyển màu đổ giữa các hình mà vẫn muốn chúng tiếp tục tuân theo chủ đề của bản trình bày. Phương thức chỉ sao chép màu: nó không sao chép kiểu đổ, viền, hoặc các định dạng hình khác. Đặt kiểu đổ của đích một cách rõ ràng bằng IFillFormat::set_FillType.
Ví dụ này tạo ba hình chữ nhật, từ trái sang phải: nguồn, bản sao có nhận thức chủ đề, và bản sao RGB cố định. Nguồn sử dụng IColorFormat::set_SchemeColor để tham chiếu màu accent đầu tiên và thêm một tông màu thông qua IColorOperationCollection::Add. Bản sao RGB sử dụng màu đổ effective của nguồn nên cả ba ban đầu trông giống nhau. Gọi IColorFormat::set_Color đặt một giá trị RGB cố định và xóa mọi phép biến đổi màu hiện có.
#include <DOM/ColorTransformOperation.h>
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IColorOperationCollection.h>
#include <DOM/IFillFormat.h>
#include <DOM/IFillFormatEffectiveData.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/Presentation.h>
#include <DOM/SchemeColor.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <DOM/Theme/IColorScheme.h>
#include <DOM/Theme/IMasterTheme.h>
#include <Export/SaveFormat.h>
#include <IImage.h>
#include <ImageFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
#include <initializer_list>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(660, 220, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
presentation->get_MasterTheme()->get_ColorScheme()->get_Accent1()->set_Color(Color::get_RoyalBlue());
auto source = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 30, 40, 180, 140);
auto themeCopy = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 240, 40, 180, 140);
auto rgbCopy = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 450, 40, 180, 140);
for (auto shape : { source, themeCopy, rgbCopy })
{
shape->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
}
auto sourceColor = source->get_FillFormat()->get_SolidFillColor();
sourceColor->set_SchemeColor(SchemeColor::Accent1);
sourceColor->get_ColorTransform()->Add(ColorTransformOperation::Tint, 0.75f);
themeCopy->get_FillFormat()->get_SolidFillColor()->CopyFrom(sourceColor);
auto effectiveFill = source->get_FillFormat()->GetEffective();
rgbCopy->get_FillFormat()->get_SolidFillColor()->set_Color(effectiveFill->get_SolidFillColor());
auto before = slide->GetImage(1.0f, 1.0f);
before->Save(u"color-copy-before.png", ImageFormat::Png);
before->Dispose();
// Tham chiếu chủ đề sẽ theo màu accent mới; giá trị RGB cố định thì không.
presentation->get_MasterTheme()->get_ColorScheme()->get_Accent1()->set_Color(Color::get_OrangeRed());
auto after = slide->GetImage(1.0f, 1.0f);
after->Save(u"color-copy-after.png", ImageFormat::Png);
after->Dispose();
presentation->Save(u"copied_colors.pptx", SaveFormat::Pptx);
presentation->Dispose();
Trước khi đổi chủ đề, cả ba độ đổ đều giống nhau:

Sau khi đổi chủ đề, nguồn và bản sao có nhận thức chủ đề theo màu accent mới và giữ tông màu. Bản sao RGB cố định vẫn giữ màu xanh:

Định nghĩa đã sao chép độc lập với nguồn. Thay đổi sau này tham chiếu chủ đề của nguồn, gán màu khác, hoặc thêm/bớt các phép biến đổi sẽ không ảnh hưởng đến đích. Thay đổi chủ đề vẫn ảnh hưởng đến cả hai hình vì mỗi hình đều tham chiếu riêng rẽ tới cùng một màu chủ đề.
Lưu và mở lại file PPTX sẽ giữ lại loại màu đã sao chép, có thể truy cập qua IColorFormat::get_ColorType, tham chiếu chủ đề và bộ sưu tập biến đổi trả về bởi IColorFormat::get_ColorTransform; nó không làm phẳng định nghĩa thành RGB.
Khi sao chép giữa các ngữ cảnh chủ đề khác nhau, chẳng hạn các bản trình bày có bảng màu accent khác nhau, tham chiếu đã sao chép sẽ được giải quyết theo chủ đề của đích. Do đó cùng một tham chiếu và các biến đổi có thể tạo ra màu hiển thị khác nhau. Xem mục Presentation Theme để biết thêm về cấu hình màu chủ đề.
Sử Dụng Nền Slide Là Độ Đổ Cho Hình
Sử dụng IAutoShape::set_UseBackgroundFill khi một hình cần khớp với nền slide, ví dụ trong một mẫu bản trình bày. Đặt IFillFormat::set_FillType thành FillType::NotDefined trước để xóa bất kỳ độ đổ nào được chỉ định rõ ràng. Với sự kết hợp này, hình sẽ sử dụng nền slide thay vì độ đổ kiểu của nó. Chỉ bật cờ này không xóa độ đổ rõ ràng hiện có.
Điều này khác với FillType::NoFill: độ đổ nền bao phủ các đối tượng phía sau hình bằng khu vực nền slide tương ứng, trong khi no fill để các đối tượng đó vẫn hiển thị. Gradient vẫn giữ căn chỉnh với slide thay vì khởi động lại trong mỗi hình.
Ví dụ sau tạo một nền master được kế thừa bởi slide và đặt một dải màu hồng phía sau ba hình chữ nhật. Hình chữ nhật trái dùng nền, hình giữa không có độ đổ, và hình bên phải chuyển từ độ đổ nền sang độ đổ màu cam rõ ràng.
#include <DOM/BackgroundType.h>
#include <DOM/FillType.h>
#include <DOM/GradientShape.h>
#include <DOM/IAutoShape.h>
#include <DOM/IBackground.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IGradientFormat.h>
#include <DOM/IGradientStopCollection.h>
#include <DOM/ILayoutSlide.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IMasterSlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(720, 400, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
// Kế thừa gradient từ master.
slide->get_Background()->set_Type(BackgroundType::NotDefined);
slide->get_LayoutSlide()->get_Background()->set_Type(BackgroundType::NotDefined);
auto background = slide->get_LayoutSlide()->get_MasterSlide()->get_Background();
background->set_Type(BackgroundType::OwnBackground);
background->get_FillFormat()->set_FillType(FillType::Gradient);
auto gradient = background->get_FillFormat()->get_GradientFormat();
gradient->set_GradientShape(GradientShape::Linear);
gradient->set_LinearGradientAngle(0);
gradient->get_GradientStops()->Clear();
gradient->get_GradientStops()->Add(0, Color::get_DarkBlue());
gradient->get_GradientStops()->Add(1, Color::get_LightSkyBlue());
// Thêm đối tượng này đầu tiên để nó hiển thị phía sau các hình chữ nhật.
auto band = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 25, 150, 670, 100);
band->get_FillFormat()->set_FillType(FillType::Solid);
band->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Magenta());
band->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
auto backgroundShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 100, 160, 200);
backgroundShape->get_FillFormat()->set_FillType(FillType::NotDefined);
backgroundShape->set_UseBackgroundFill(true);
auto noFillShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 280, 100, 160, 200);
noFillShape->get_FillFormat()->set_FillType(FillType::NoFill);
auto explicitShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 510, 100, 160, 200);
explicitShape->get_FillFormat()->set_FillType(FillType::NotDefined);
explicitShape->set_UseBackgroundFill(true);
// Tắt việc sử dụng nền trước khi áp dụng độ đổ rõ ràng.
explicitShape->set_UseBackgroundFill(false);
explicitShape->get_FillFormat()->set_FillType(FillType::Solid);
explicitShape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Orange());
presentation->Save(u"background_fill.pptx", SaveFormat::Pptx);
presentation->Dispose();
Kết quả, từ trái sang phải: độ đổ nền, không độ đổ, và độ đổ rõ ràng. Dải màu hồng bị ẩn bởi hình trái và phải nhưng vẫn hiển thị qua hình giữa.

Hành vi tương tự áp dụng cho nền được định nghĩa trực tiếp trên slide hoặc kế thừa từ bố cục. Hình sử dụng nền đã được giải quyết của slide; ví dụ để cả nền slide và bố cục không xác định để master cung cấp.
Đặt Độ Trong Suốt
Đối với độ đổ màu đơn, thành phần alpha của màu kiểm soát độ trong suốt: 0 là hoàn toàn trong suốt và 255 là hoàn toàn không trong suốt. Ví dụ này sử dụng giá trị alpha 128 cho một hình chữ nhật màu vàng, làm cho nó khoảng 50% trong suốt để hình chữ nhật phía dưới vẫn nhìn thấy được. Màu được gán qua IFillFormat::get_SolidFillColor.
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.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>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
const int alpha = 128;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto solidShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 150, 75);
auto transparentShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 80, 80, 150, 75);
transparentShape->get_FillFormat()->set_FillType(FillType::Solid);
transparentShape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::FromArgb(alpha, Color::get_Yellow()));
presentation->Save(u"shape_transparency.pptx", SaveFormat::Pptx);
presentation->Dispose();
Kết quả:

Xoay Hình
Sử dụng IShape::set_Rotation để xoay một hình trong mặt phẳng slide. Góc đo bằng độ; ví dụ này xoay một hình chữ nhật 5 độ theo chiều kim đồng hồ và lưu bản trình bày.
#include <DOM/IAutoShape.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 150, 75);
shape->set_Rotation(5);
presentation->Save(u"shape_rotation.pptx", SaveFormat::Pptx);
presentation->Dispose();
Kết quả:

Thêm Hiệu Ứng Bo 3D
Sử dụng IShape::get_ThreeDFormat để tạo độ sâu và các cạnh bo cho hình. Ví dụ này tạo một ellip màu xanh lá cây với viền màu cam, đặt IThreeDFormat::set_Depth thành 4 điểm, và áp dụng một bo trên tròn với chiều rộng và chiều cao 6 điểm qua IThreeDFormat::get_BevelTop. Một camera trực giao hướng về phía trước và ánh sáng ba‑điểm làm cho bo nhìn thấy được.
#include <DOM/BevelPresetType.h>
#include <DOM/CameraPresetType.h>
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/ICamera.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILightRig.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeBevel.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/IThreeDFormat.h>
#include <DOM/LightRigPresetType.h>
#include <DOM/LightingDirection.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Ellipse, 50, 50, 100, 100);
shape->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Green());
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Orange());
shape->get_LineFormat()->set_Width(2.0);
shape->get_ThreeDFormat()->set_Depth(4);
shape->get_ThreeDFormat()->get_BevelTop()->set_BevelType(BevelPresetType::Circle);
shape->get_ThreeDFormat()->get_BevelTop()->set_Height(6);
shape->get_ThreeDFormat()->get_BevelTop()->set_Width(6);
shape->get_ThreeDFormat()->get_Camera()->set_CameraType(CameraPresetType::OrthographicFront);
shape->get_ThreeDFormat()->get_LightRig()->set_LightType(LightRigPresetType::ThreePt);
shape->get_ThreeDFormat()->get_LightRig()->set_Direction(LightingDirection::Top);
presentation->Save(u"3D_bevel_effect.pptx", SaveFormat::Pptx);
presentation->Dispose();
Kết quả:

Thêm Hiệu Ứng Xoay 3D
Góc nhìn 3D của một hình được điều khiển bởi cài đặt camera. Ví dụ này sử dụng ICamera::SetRotation với các góc vĩ độ, kinh độ và quay vòng là 40, 35 và 20 độ tương ứng. Nó chọn một camera isometric qua ICamera::set_CameraType và ánh sáng cân bằng qua ILightRig::set_LightType, sau đó lưu hình chữ nhật đã định dạng.
#include <DOM/CameraPresetType.h>
#include <DOM/IAutoShape.h>
#include <DOM/ICamera.h>
#include <DOM/ILightRig.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/IThreeDFormat.h>
#include <DOM/LightRigPresetType.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto autoShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 150, 75);
autoShape->get_TextFrame()->set_Text(u"Hello, Aspose!");
autoShape->get_ThreeDFormat()->get_Camera()->set_CameraType(CameraPresetType::IsometricLeftUp);
autoShape->get_ThreeDFormat()->get_Camera()->SetRotation(40, 35, 20);
autoShape->get_ThreeDFormat()->get_LightRig()->set_LightType(LightRigPresetType::Balanced);
presentation->Save(u"3D_rotation_effect.pptx", SaveFormat::Pptx);
presentation->Dispose();
Kết quả:

Kiểm Soát Đánh Giá Đen‑Trắng Cho Hình
Phương thức IShape::set_BlackWhiteMode chỉ định cách một hình riêng lẻ được hiển thị khi bản trình bày được xem hoặc xử lý ở chế độ đen‑trắng. Nó không tự động bật chế độ đen‑trắng và không thay đổi độ đổ, đường viền hay định dạng khác của hình trong chế độ màu bình thường.
Sử dụng một giá trị từ phân loại BlackWhiteMode để chọn hành vi mong muốn. Ví dụ, Automatic để ứng dụng render chọn cách chuyển đổi, Gray và LightGray dùng màu xám, BlackWhite chỉ dùng đen và trắng, Black và White buộc màu duy nhất, Color giữ nguyên màu bình thường, và Hidden bỏ qua hình trong chế độ đen‑trắng. NotDefined nghĩa là không có chế độ cấp độ hình được gán.
Ví dụ này tạo một hình chữ nhật màu cam và đặt IShape::set_BlackWhiteMode thành màu xám. Hình đã lưu giữ độ đổ màu cam cho hiển thị màu bình thường và yêu cầu màu xám cho các ứng dụng tôn trọng cài đặt hiển thị đen‑trắng:
#include <DOM/BlackWhiteMode.h>
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.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>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 200, 100);
shape->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Orange());
shape->set_BlackWhiteMode(BlackWhiteMode::Gray);
presentation->Save(u"shape_black_white_mode.pptx", SaveFormat::Pptx);
presentation->Dispose();
Trong chế độ màu bình thường, hình chữ nhật giữ độ đổ màu cam. Trong quy trình làm việc hiển thị đen‑trắng, nó dùng màu xám vì chế độ được đặt thành Gray. Điều này cho phép bạn giữ một slide đầy màu trong khi định nghĩa một giao diện riêng cho việc in, xem trước, hoặc các quy trình khác tuân theo cài đặt hiển thị đen‑trắng của bản trình bày.
Đặt Lại Định Dạng
Sử dụng ISlide::Reset để khôi phục vị trí, kích thước và định dạng của các placeholder slide từ các placeholder bố cục tương ứng. Ví dụ này yêu cầu một bản trình bày hiện có có các placeholder liên kết bố cục và đặt lại mọi slide trước khi lưu. Các hình không có placeholder bố cục tương ứng sẽ không được đặt lại.
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/enumerator_adapter.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
auto presentation = MakeObject<Presentation>(u"sample.pptx");
for (auto&& slide : IterateOver(presentation->get_Slides()))
{
slide->Reset();
}
presentation->Save(u"reset_formatting.pptx", SaveFormat::Pptx);
presentation->Dispose();
FAQ
Định dạng hình có ảnh hưởng đến kích thước cuối cùng của file bản trình bày không?
Màu, kiểu đường và cài đặt hiệu ứng thường chỉ thêm một lượng dữ liệu nhỏ so với các hình ảnh và phương tiện được nhúng. Độ đổ hình ảnh có thể làm tăng kích thước file vì hình ảnh của chúng được lưu trong bản trình bày.
Làm sao tôi có thể phát hiện các hình trên một slide có cùng định dạng để nhóm chúng lại?
So sánh các thuộc tính định dạng chính của mỗi hình — độ đổ, đường viền và cài đặt hiệu ứng. Nếu tất cả các giá trị tương ứng khớp nhau, coi chúng là cùng kiểu và nhóm chúng hợp lý, giúp việc quản lý kiểu sau này trở nên đơn giản hơn.
Tôi có thể lưu một bộ các kiểu hình tùy chỉnh vào một file riêng để tái sử dụng trong các bản trình bày khác không?
Có. Lưu các hình mẫu với các kiểu mong muốn trong một slide mẫu hoặc file template .POTX. Khi tạo một bản trình bày mới, mở template, sao chép các hình đã định dạng cần thiết và áp dụng lại định dạng của chúng ở nơi cần.