如何使用GDI+在C语言中?
免责声明:我才刚刚开始学习 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...
问题是 GpStatus
是 GdiPlusGpStubs.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是 gdiplusflat.h 确实需要在它之前包含更多 gdi*.h 头文件。但是,许多具有 gdiplusflat.h 引用的所需 typedef 声明的头文件也包含“class”声明和其他 C++ 关键字。当 C 编译器看到这些行时将会出错。
你有两个选择。
简单。接受 C++ 本质上是 C 的超集这一事实。然后只需将您尝试编译的“.c”文件重命名为具有“.cpp”扩展名即可。您的 C 代码将被编译为 C++,但这可能不会改变您编写的一行代码。然后#include gdiplus.h 在#include gdiplusflat.h 之前#include gdiplusflat.h
更难。取决于其他头文件中的 typedef 定义。问题在于,许多头文件都具有 C 编译器会出错的“类”定义和 C++ 关键字。您必须手动将许多 C 声明移植到您自己的头文件中,该头文件包含在 gdiplusflat.h 之前。这是我微弱的尝试。事情还没有完全完成。它消除了一半的编译错误。但我太累了,就选择了选项#1。您可以完成它,但上面的选项 1 更容易。
x
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.
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
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
您可以使用平面 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.