Cách Kết Nối Các Hình Dạng Với Connectors trong C++

Cách Kết Nối Các Hình Dạng Với Connectors trong C++

Kết nối trong Aspose.Slides FOSS là các hình dạng đường thẳng gắn vào các vị trí kết nối trên các hình dạng khác. Khi bạn di chuyển một hình dạng đã được kết nối, điểm cuối của kết nối sẽ di chuyển cùng nó. Loại kết nối phổ biến nhất là BENT_CONNECTOR3, nó di chuyển quanh các chướng ngại vật với một khúc gập duy nhất.


Điều kiện tiên quyết

git clone https://github.com/aspose-slides-foss/Aspose.Slides-FOSS-for-Cpp.git
cd Aspose.Slides-FOSS-for-Cpp && mkdir build && cd build
cmake .. && cmake --build .

Chỉ mục Trang Kết nối

Mỗi hình có bốn vị trí kết nối được đánh số:

Chỉ mụcVị trí
0Trung tâm trên
1Trung tâm trái
2Trung tâm dưới
3Trung tâm phải

Kết Nối Hai Hình

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    // Add two rectangles
    auto& box1 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 200, 200, 100);
    auto& box2 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 450, 200, 200, 100);

    box1.text_frame()->set_text("Start");
    box2.text_frame()->set_text("End");

    // Add a bent connector (initial bounds are overwritten by the connection)
    auto& conn = slide.shapes().add_connector(
        asf::ShapeType::BENT_CONNECTOR3, 0, 0, 10, 10);

    // Connect right side of box1 (site 3) to left side of box2 (site 1)
    conn.set_start_shape_connected_to(&box1);
    conn.set_start_shape_connection_site_index(3);
    conn.set_end_shape_connected_to(&box2);
    conn.set_end_shape_connection_site_index(1);

    prs.save("connected.pptx", asf::SaveFormat::PPTX);
    return 0;
}

Các giới hạn placeholder (0, 0, 10, 10) được truyền cho add_connector sẽ bị bỏ qua một khi các điểm cuối kết nối được thiết lập; PowerPoint định tuyến lại connector tới các shape được đính kèm.


Các loại đầu nối

namespace asf = Aspose::Slides::Foss;

// Straight line
asf::ShapeType::STRAIGHT_CONNECTOR1;

// Single elbow (L-shape)
asf::ShapeType::BENT_CONNECTOR2;

// Double elbow (Z-shape): most common
asf::ShapeType::BENT_CONNECTOR3;

// Curved connector
asf::ShapeType::CURVED_CONNECTOR3;

Định dạng Đường kết nối

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    auto& box1 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 150, 180, 80);
    auto& box2 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 500, 300, 180, 80);

    auto& conn = slide.shapes().add_connector(
        asf::ShapeType::BENT_CONNECTOR3, 0, 0, 10, 10);
    conn.set_start_shape_connected_to(&box1);
    conn.set_start_shape_connection_site_index(2);   // bottom of box1
    conn.set_end_shape_connected_to(&box2);
    conn.set_end_shape_connection_site_index(0);     // top of box2

    // Style: dashed blue line, 2 pt width
    auto& lf = conn.line_format();
    lf.set_width(2.0);
    lf.fill_format().solid_fill_color().set_color(asf::Color::blue);
    lf.set_dash_style(asf::LineDashStyle::DASH);

    prs.save("styled-connector.pptx", asf::SaveFormat::PPTX);
    return 0;
}

Lưu đồ với nhiều kết nối

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    // Three-step flowchart
    auto& step1 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 350, 50, 200, 70);
    auto& step2 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 350, 220, 200, 70);
    auto& step3 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 350, 390, 200, 70);

    step1.text_frame()->set_text("Step 1");
    step2.text_frame()->set_text("Step 2");
    step3.text_frame()->set_text("Step 3");

    auto connect_vertical = [&](auto& top_shape, auto& bottom_shape) {
        auto& c = slide.shapes().add_connector(
            asf::ShapeType::BENT_CONNECTOR3, 0, 0, 10, 10);
        c.set_start_shape_connected_to(&top_shape);
        c.set_start_shape_connection_site_index(2);   // bottom
        c.set_end_shape_connected_to(&bottom_shape);
        c.set_end_shape_connection_site_index(0);     // top
    };

    connect_vertical(step1, step2);
    connect_vertical(step2, step3);

    prs.save("flowchart.pptx", asf::SaveFormat::PPTX);
    return 0;
}

Đọc Thuộc tính Kết nối

#include <Aspose/Slides/Foss/presentation.h>
#include <iostream>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs("connected.pptx");
    for (size_t i = 0; i < prs.slides()[0].shapes().size(); ++i) {
        auto& shape = prs.slides()[0].shapes()[i];
        if (shape.is_connector()) {
            auto& connector = static_cast<asf::Connector&>(shape);
            std::cout << "Connector found\n";
        }
    }
    return 0;
}

Xem thêm

 Tiếng Việt