如何使用GDI+在C语言中?

发布于 2024-10-19 02:12:00 字数 1791 浏览 9 评论 0原文

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

我到底该如何在纯 C 中使用 GDI+? 据我了解,GDI+ 包装了为 C++ 制作的对象,但在它的下面有一个平面 API,可以通过 gdiplusflat.h(一个 C 友好的标头)访问该 API。

我的问题是,当我 #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 的类型定义, Status 本身是在 GdiPlusTypes.h(也是一个 C++ 标头)中定义的枚举。我尝试自己定义枚举,但由于某种原因编译器不会接受它!

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

Disclaimer: I'm only getting started in C, so it's likely that I'm missing something obvious, or not thinking the right way! :)

How exactly would I go about using GDI+ in pure C?
As I understand it, GDI+ has wrapped objects made for C++, but underneath it lies a flat API which is accessible through gdiplusflat.h, a C-friendly header.

My problem is that when I #include it, I get the following errors:

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...

Now, I think these errors are due to GpStatus not being defined because peeking into GdiPlusFlat.h shows that all the functions are of the style:

// 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...

The problem is that GpStatus is a typedef to Status in GdiPlusGpStubs.h (a C++ header), and Status is itself an enum defined in GdiPlusTypes.h (also a C++ header). I tried defining the enum myself, but for some reason the compiler wouldn't take it!

So... how does one exactly use GDI+ functions in C? Should I just load gdiplus.dll dynamically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺ぐ爱和你が 2024-10-26 02:12:00

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

你有两个选择。

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

The problem is that gdiplusflat.h really needs many more gdi*.h header files included before it. But many of these header files that have the needed typedef declarations referenced by gdiplusflat.h also contain "class" declarations and other C++ keywords. The C compiler will error out when it sees these lines.

You have two choices.

  1. Simple. Accept the fact that C++ is essentially a superset of C. Then just rename your ".c" file you are trying to compile to have a ".cpp" extension. Your C code will get compiled as C++, but that likely won't change a line of code you write. Then #include gdiplus.h before you #include gdiplusflat.h

  2. Harder. depends on typedef definitions in other header files. The problem is that many of these header files have "class" definitions and C++ keywords that the C compiler will error out on. You'll have to manually port over many of the C declarations into your own header file that gets included before gdiplusflat.h. Here's my feeble attempt. It's not quite done. It gets half of the compilation errors out. But I got too tired and just went with option #1. You could finish it, but option 1 above is so much easier.

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>
青芜 2024-10-26 02:12:00

您可以使用平面 API。有从 Flat API (C) 到 GDI+ (C++) 的直接转换,我认为这相当简单。

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

You can use Flat API from MSDN. There's a straight conversion from Flat API (C) to GDI+ (C++), I think it's fairly simple.

P.S: However you can still use GDI straight in C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文