Wie man Formen mit Connectors in C++ verbindet

Wie man Formen mit Connectors in C++ verbindet

Verbindungen in Aspose.Slides FOSS sind Linienformen, die an Verbindungspunkten anderer Formen angebracht werden. Wenn Sie eine verbundene Form verschieben, bewegt sich der Endpunkt der Verbindung mit ihr. Der am häufigsten verwendete Verbindungstyp ist BENT_CONNECTOR3, der Hindernisse mit einem einzigen Ellenbogenbogen umgeht.


Voraussetzungen

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 .

Verbindungsstellen-Indizes

Jede Form hat vier nummerierte Verbindungspunkte:

IndexPosition
0Oben Mitte
1Links Mitte
2Unten Mitte
3Rechts Mitte

Zwei Formen verbinden

#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;
}

Die Platzhaltergrenzen (0, 0, 10, 10), die an add_connector übergeben werden, werden ignoriert, sobald die Verbindungsendpunkte festgelegt sind; PowerPoint leitet den Verbinder zu den angehängten Formen um.


Anschlusstypen

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;

Stil der Verbindungslinie

#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;
}

Flussdiagramm mit mehreren Verbindern

#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;
}

Connector‑Eigenschaften lesen

#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;
}

Siehe auch

 Deutsch