【问题标题】:How to use GDI+ in C?如何在 C 语言中使用 GDI+?
【发布时间】:2011-07-04 03:07:02
【问题描述】:

免责声明:我才刚刚开始使用 C,所以很可能我遗漏了一些明显的东西,或者没有正确地思考! :)

我将如何在纯 C 中使用 GDI+? 据我了解,GDI+ 已经封装了为 C++ 制作的对象,但在它下面是一个平面 API,可通过 gdiplusflat.h 访问,这是一个 C 友好的标头。

我的问题是当我#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...

现在,我认为这些错误是由于未定义 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...

问题在于GpStatusGdiPlusGpStubs.h(C++ 头文件)中Status 的typedef,而Status 本身是GdiPlusTypes.h(也是C++ 头文件)中定义的枚举。我尝试自己定义枚举,但由于某种原因编译器不会接受它!

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

【问题讨论】:

    标签: c winapi gdi+ gdi


    【解决方案1】:

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

    你有两个选择。

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

    【讨论】:

      【解决方案2】:

      您可以使用来自 MSDN 的 Flat API。从 Flat API (C) 到 GDI+ (C++) 有一个直接的转换,我认为它相当简单。

      P.S: 但是你仍然可以在 C 中直接使用 GDI。

      【讨论】:

        猜你喜欢
        • 2019-01-27
        • 2020-08-16
        • 1970-01-01
        • 1970-01-01
        • 2011-04-28
        • 2016-03-23
        • 1970-01-01
        • 1970-01-01
        • 2011-11-09
        相关资源
        最近更新 更多