如何在MFC项目中启用GDI+ 1.1而非1.0?

6

我无法在我的VS2012 MFC C++项目(在Win7上)中使用GDI+ 1.1类。 Image,Bitmap,Graphics类都可以正常工作,但是当我尝试声明Blur类的对象(或其他v1.1类)时,我会收到一个“error C2065:'Blur':未声明的标识符”错误。 我尝试像这样定义stdafx.h中的GDIPVER:

#define GDIPVER 0x0110 //also I get the warning C4005: 'GDIPVER' : macro redefinition
#include <gdiplus.h>
#pragma comment (lib,"Gdiplus.lib")

但它不起作用。

如何打开GDI+ 1.1而不是1.0?


1
重新定义警告很糟糕。在你之前,有些代码会#包含gdiplus.h头文件。将其放置于stdafx.h文件中是最佳选择。使用/showIncludes进行故障排除。 - Hans Passant
是的,这个在我的stdafx.h文件的末尾。 - Craftsmann
也许之前已经包含了它。 - doctorlove
我再次检查了一遍,stdafx.h 是整个解决方案中唯一提到 gdiplus.h 的地方。 - Craftsmann
也许你的解决方案中包含了其他引用该文件的内容。 - Erbureth
3个回答

6

我曾在一项项目中遇到类似的问题。对我来说,我的预编译头文件有以下内容:

#define GDIPVER     0x0110  // Use more advanced GDI+ features

但是预编译头文件并未包含#include "gdiplus.h"。这只会出现在实际调用GDI+的.cpp文件中。为了那些具有GDI+对象指针成员的头文件,我会提前声明GDI+类。正如Hans和其他评论所指出的,很可能有另一个头文件在GDIPVER设置之前包含gdiplus.h。要找出它被包含在哪里,请尝试进入项目的C/C++>命令行设置,并添加/showIncludes,然后进行完整构建,并在构建日志中查找gdiplus.h,然后追溯到第一个包含它的头文件。

一旦您解决了这个障碍,我还发现我的应用程序实际上不会使用1.1功能,除非清单也得更新。因此,我的其中一个.cpp文件具有以下内容:

// Update Manifest
// cf: http://blogs.msdn.com/b/oldnewthing/archive/2007/05/31/2995284.aspx
//
// We use features from GDI+ v1.1 which is new as of Windows Vista. There is no redistributable for Windows XP.
// This adds information to the .exe manifest to force GDI+ 1.1 version of gdiplus.dll to be loaded on Vista
// without this, Vista defaults to loading version 1.0 and our application will fail to launch with missing entry points.
#if 64BIT_BUILD
#pragma comment(linker, "\"/manifestdependency:type='Win32' name='Microsoft.Windows.GdiPlus' version='1.1.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker, "\"/manifestdependency:type='Win32' name='Microsoft.Windows.GdiPlus' version='1.1.0.0' processorArchitecture='X86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif

1
/showIncludes选项非常有用。因此,我发现gdiplus.h已经在之前被包含了。我删除了#include <gdiplus.h>#pragma comment (lib,"Gdiplus.lib"),并在stdafx.h的开头加入了#define GDIPVER 0x0110。现在它可以工作了! - Craftsmann
1
抱歉又翻起了一个老问题,但是我能否建议您使用 * 代替 amd64 或者 x86?这会更简单。 - LHLaurini
我相当确定我曾经尝试过那个方法,但它失败了,但如果它适用于您,那是一个好方法。 - jschroedl

1

顺便提一下,我做了以下操作来在VS 2019 MFC项目中启用GDI+ 1.1:

  1. 在包含pch.h之前,在pch.cpp中添加以下行:
    #define GDIPVER 0x0110
  2. 在包含framework.h之后,在pch.h中添加以下行:
    #include <gdiplus.h>
    using namespace Gdiplus;
  3. 使用稍微修改和更新的代码块版本,这个代码块是7年前由@jschroedl友情提供的:
// Update Manifest
// cf: http://blogs.msdn.com/b/oldnewthing/archive/2007/05/31/2995284.aspx
//
// We use features from GDI+ v1.1 which was new as of Windows Vista. There is no redistributable for Windows XP.
// This adds information to the .exe manifest to force the GDI+ 1.1 version of gdiplus.dll to be loaded.
// Without this, Windows will load the GDI+ 1.0 version of gdiplus.dll and the application will fail to launch with missing entry points.
#ifdef _WIN64
    //#pragma comment(linker, "\"/manifestdependency:type='Win32' name='Microsoft.Windows.GdiPlus' version='1.1.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
    //#pragma comment(linker, "\"/manifestdependency:type='Win32' name='Microsoft.Windows.GdiPlus' version='1.1.0.0' processorArchitecture='X86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif

我在启动代码中实例化了一个 Blur 对象,以验证我是否正在编译 GDI+ 1.1,并对其进行作用域限定,以便在调用 GdiplusShutdown() 之前销毁它,在 x86 和 x64 构建中应用程序都能正确运行和关闭。

0
在包含任何头文件之前,通过将GDIPVER定义为0x0110,您可以访问GDIPlus 1.1的功能。例如,在framework.h中,
define GDIPVER 0x0110

#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接