I am trying to learn the gdiplus windows API, and in particular how to use GraphicsPath to get points from different shapes. I noticed that I could never get anything to appear from microsoft's example code, so I tried to see how many points were actually in a GraphicsPath like this:
#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();
}
This always returned a count of 0. Why is this?
I have tried compiling with mingw-64 and Visual Studio with the same result.
I also tried printing out the Gdiplus::Status returned from this:
GraphicsPath path;
int stat = path.StartFigure();
std::cout << stat << std::endl;
Which printed a status of 2, "InvalidParameter" even though StartFigure doesn't take parameters.
CodePudding user response:
Ahh read more documentation and found this: 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
This code works:
#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);
}