为什么 GraphicsPath 的 GetPointCount 总是 return 0?

Why does GetPointCount of a GraphicsPath always return 0?

我正在尝试学习 gdiplus windows API,尤其是如何使用 GraphicsPath 从不同形状获取点。我注意到我永远无法从微软的示例代码中得到任何东西,所以我试着看看 GraphicsPath 中实际有多少点,如下所示:

#include <windows.h>
#include <gdiplus.h>
#include <iostream>

//use gdiplus library when compiling
#pragma comment( lib, "gdiplus" )

using namespace Gdiplus;

VOID GetPointCountExample()
{
    // Create a path that has one ellipse and one line.
    GraphicsPath path;
    path.AddLine(220, 120, 300, 160);

    // Find out how many data points are stored in the path.
    int count = path.GetPointCount();
    std::cout << count << std::endl;
}

int main()
{
    GetPointCountExample();
}

这始终返回计数 0。这是为什么?

我试过用 mingw-64 和 Visual Studio 编译得到相同的结果。

我还尝试打印出由此返回的 Gdiplus::Status:

GraphicsPath path;
int stat = path.StartFigure();

std::cout << stat << std::endl;

即使 StartFigure 不带参数,它也打印了状态 2,“InvalidParameter”。

啊,阅读更多文档后发现:The GdiplusStartup function initializes Windows GDI+. Call GdiplusStartup before making any other GDI+ calls https://docs.microsoft.com/en-us/windows/win32/api/Gdiplusinit/nf-gdiplusinit-gdiplusstartup

此代码有效:

#include <windows.h>
#include <gdiplus.h>
#include <iostream>

//use gdiplus library when compiling
#pragma comment( lib, "gdiplus" )

using namespace Gdiplus;

VOID GetPointCountExample()
{
    // Create a path that has one ellipse and one line.
    GraphicsPath path;
    path.AddLine(0, 0, 0, 1);

    // Find out how many data points are stored in the path.
    int count = path.GetPointCount();
    std::cout << count << std::endl;
}

int main()
{
    //Must call GdiplusStartup before making any GDI+ calls
    //https://docs.microsoft.com/en-us/windows/win32/api/Gdiplusinit/nf-gdiplusinit-gdiplusstartup
    ULONG_PTR token;
    GdiplusStartupInput input;
    input.GdiplusVersion = 1;
    input.SuppressBackgroundThread = false;

    GdiplusStartup(&token, &input, NULL);

    GetPointCountExample();

    //Shutdown GDI+ when finished using
    GdiplusShutdown(token);
}