如何在 MFC 项目中打开 GDI+ 1.1 而不是 1.0

How to turn on GDI+ 1.1 instead of 1.0 in MFC project?

本文关键字:GDI+ MFC 项目      更新时间:2023-10-16

我无法在我的VS2012 MFC C++项目(在Win7上)中使用GDI+ 1.1类。类图像,位图,图形工作得很好,但是当我尝试声明Blur类(或其他v1.1类)的对象时,我得到了一个error C2065: ‘Blur’: undeclared identifier。我试图像这样定义GDIPVER(在 stdafx.h 中)

#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?

我在一个项目上与类似的问题斗争了一段时间。对我来说,我的预编译头有这个:

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

但是预编译的标头不会 #include"gdiplus.h"。这仅发生在实际进行 GDI+ 调用的.cpp文件中。 我为将 GDI+ 对象指针作为成员的标头转发声明 GDI+ 类。正如Hans和其他评论所指出的,在设置GDIPVER之前,可能还有另一个包含gdiplus.h的标题。要确定它包含的位置,请尝试转到项目的 C/C++> 命令行设置并添加/showInclude,然后执行完整构建并查看 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

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

  1. 在包含pch.h之前,在pch.cpp中添加了此行:
    #define GDIPVER 0x0110
  2. 包含framework.h
    #include <gdiplus.h>
    using namespace Gdiplus; 之后,在pch.h中添加了这些行
  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 版本中都正确运行和关闭。

通过在包含任何标头之前将 GDIPVER 定义为0x0110,您可以访问 GDIPlus 1.1 功能。例如,在框架中,

define GDIPVER 0x0110
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")