如何在C中使用GDI +?

Met*_*eta 12 c winapi gdi+ gdi

免责声明:我只是开始使用C语言,所以很可能我错过了一些明显的东西,或者没有想到正确的方法!:)

我究竟如何在纯C中使用GDI +?据我所知,GDI +已经包装了为C++制作的对象,但在它下面是一个平面API,可通过gdiplusflat.hC友好的头部访问.

我的问题是当我#include它时,我得到以下错误:

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\gdiplusflat.h(30) : error C2143: syntax error : missing '{' before '__stdcall'
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\gdiplusflat.h(31) : error C2146: syntax error : missing ')' before identifier 'brushMode'
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\gdiplusflat.h(31) : error C2061: syntax error : identifier 'brushMode'
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\gdiplusflat.h(31) : error C2059: syntax error : ';'
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\gdiplusflat.h(31) : error C2059: syntax error : ','
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\gdiplusflat.h(31) : error C2059: syntax error : ')'
and 100 more...
Run Code Online (Sandbox Code Playgroud)

现在,我认为这些错误是由于GpStatus没有被定义,因为偷看GdiPlusFlat.h显示所有函数都是样式:

// WINGDIAPI is #defined as __stdcall
GpStatus WINGDIPAPI
GdipCreatePath(GpFillMode brushMode, GpPath **path);
GpStatus WINGDIPAPI
GdipCreatePath2(GDIPCONST GpPointF*, GDIPCONST BYTE*, INT, GpFillMode,
                                    GpPath **path);
GpStatus WINGDIPAPI
GdipCreatePath2I(GDIPCONST GpPoint*, GDIPCONST BYTE*, INT, GpFillMode,
                                     GpPath **path);
etc...
Run Code Online (Sandbox Code Playgroud)

问题是它GpStatus是一个typedef Statusin GdiPlusGpStubs.h(一个C++头),Status它本身是一个枚举GdiPlusTypes.h(也是一个C++头).我自己尝试定义枚举,但由于某种原因,编译器不会接受它!

那么......在C中如何正确使用GDI +函​​数?我应该动态加载gdiplus.dll吗?

sel*_*bie 6

问题是gdiplusflat.h确实需要更多的gdi*.h头文件才能包含在它之前.但是,许多具有gdiplusflat.h引用的所需typedef声明的头文件也包含"类"声明和其他C++关键字.C编译器在看到这些行时会出错.

你有两个选择.

  1. 简单.接受C++本质上是C的超集这一事实.然后只需将您尝试编译的".c"文件重命名为".cpp"扩展名.您的C代码将被编译为C++,但这可能不会改变您编写的代码行.然后在#include gdiplusflat.h之前#include gdiplus.h

  2. 更难.取决于其他头文件中的typedef定义.问题是许多这些头文件都有"类"定义和C++关键字,C编译器会错误地输出.您必须手动将许多C声明移植到您自己的头文件中,该文件在gdiplusflat.h之前包含.这是我的微弱尝试.它还没有完成.它得到了一半的编译错误.但我太累了,只是选择#1.你可以完成它,但上面的选项1更容易.

X

enum Status
{
    Ok = 0,
    GenericError = 1,
    InvalidParameter = 2,
    OutOfMemory = 3,
    ObjectBusy = 4,
    InsufficientBuffer = 5,
    NotImplemented = 6,
    Win32Error = 7,
    WrongState = 8,
    Aborted = 9,
    FileNotFound = 10,
    ValueOverflow = 11,
    AccessDenied = 12,
    UnknownImageFormat = 13,
    FontFamilyNotFound = 14,
    FontStyleNotFound = 15,
    NotTrueTypeFont = 16,
    UnsupportedGdiplusVersion = 17,
    GdiplusNotInitialized = 18,
    PropertyNotFound = 19,
    PropertyNotSupported = 20,
#if (GDIPVER >= 0x0110)
    ProfileNotFound = 21,
#endif //(GDIPVER >= 0x0110)
};



typedef Status GpStatus;

enum FillMode
{
    FillModeAlternate,        // 0
    FillModeWinding           // 1
};

typedef FillMode GpFillMode;

struct GpPath {};

typedef float REAL;

struct GpPointF
{
    REAL x;
    REAL y;
};

struct GpPoint
{
    int x;
    int y;
};
#include <gdiplusflat.h>
Run Code Online (Sandbox Code Playgroud)