zoukankan      html  css  js  c++  java
  • 【vs2013】如何在VS的MFC中配置使用GDI+?

    摘自:http://www.cnblogs.com/CSGrandeur/p/3156843.html (已实验,可行)

    1、配置GDI+

    VS2010自带GDI+,直接使用。

    (1)首先要添加头文件和库

    #pragma comment( lib, "gdiplus.lib" )
    #include "gdiplus.h"
    using namespace Gdiplus;

    建议添加到stdafx.h里

    (2)然后添加全局变量

    static ULONG_PTR m_gdiplusToken;

    该成员变量用来保存GDI+被初始化后在应用程序中的GDI+标识。

    (3)在OnInitDialog()之类的初始化函数中,添加:

    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL); 

    (4)结束后卸载GDI+:

    可以手动重载析构函数然后加上这句:

    Gdiplus::GdiplusShutdown(m_gdiplusToken);

     头文件中添加:virtual ~CConFontChDlg();

      一定要加上virtual关键字,否则在主窗口销毁时,将不会调用我们自己创建的析构函数。

      源文件中,在构造函数下面添加:

    CConFontChDlg::~CConFontChDlg()

    {

        ...

    }

      这样就可以在析构函数中,添加我们想添加的代码了。

    配置方法很多博客都有说,也不知道源头在哪,恕我不标注来源了。

    摘自:http://bbs.csdn.net/topics/330171017

       http://www.cnblogs.com/it20120227/archive/2011/12/31/2370903.html

    首先,VS2010中已经有GDI+SDK包的,不需要额外下载
    1:在stdafx.h文件中加入下面3行代码,添加相应的头文件和库
      #pragma comment( lib, "gdiplus.lib" )
      #include "gdiplus.h"
      using namespace Gdiplus;
    2:定义一个全局变量 ULONG_PTR m_gdiplusToken;
    其中,ULONG_PTR是一个DWORD数据类型,该成员变量用来保存GDI+被初始化后在应用程序中的GDI+标识,以便能在应用程序退出后,引用该标识来调用Gdiplus:: GdiplusShutdown来关闭GDI+。
    3:使用GDI+函数前,先,最好放在OnInitDialog()中
     Gdiplus::GdiplusStartupInput gdiplusStartupInput;
      Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL); 

    或者  

    3.在C****App的InitInstance()中加入:
    //初始化GDI+
    GdiplusStartupInput gsi;
    GdiplusStartup(&m_gdiplusToken,&gsi,NULL);
    4.重载ExitInitInstance()函数
      Gdiplus::GdiplusShutdown(m_gdiplusToken);
    这就是基本的配置了



    摘自:http://blog.sina.com.cn/s/blog_4f91596001008otf.html

    在MFC里使用GDI+

    (2008-03-12 11:31:20)
     

    1. 在"stdafx.h"里加入以下:
    #include <gdiplus.h>
    using namespace Gdiplus;
    #pragma comment(lib, "gdiplus.lib")
     
    2. 为CWinApp的派生类增加两个成员:
    ULONG_PTR m_gdiplusToken;
    GdiplusStartupInput m_gdiplusStartupInput;
     
    3. 在该派生类的InitInstance()函数中加入
    GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
     
    4. 在该派生类的ExitInstance()函数中加入
    GdiplusShutdown(m_gdiplusToken);
     
    5. 到此,基本上已经可以用了,例如:
    Graphics g(this->GetSafeHwnd(),TRUE);
    Pen myPen(Color::Red,50);
    myPen.SetWidth(20);
    g.DrawLine(&myPen,50, 50, 145, 365);
     
    6. 但是,假如你用以下代码却不能编译通过:
    Graphics g(this->GetSafeHwnd(),TRUE);
    Pen* myPen = new Pen(Color::Red,50);
    g.DrawLine(myPen,50, 50, 145, 365);
    提示
    error C2660: “Gdiplus::GdiplusBase::operator new” : 函数不接受 3 个参数
    的错误。
     
    7. 要解决此问题,需参考微软的文章,全文如下:

    PRB: Microsoft Foundation Classes DEBUG_NEW Does Not Work with GDI+
    Article ID : 317799
    Last Review : February 12, 2007
    Revision : 2.1

    This article was previously published under Q317799

    SYMPTOMS
    When you build a debug version of a Microsoft Foundation Classes (MFC) application that uses GDI+, you may receive an error message that resembles the following:
    error C2660: 'Gdiplus::GdiplusBase::operator new' : function does not take 3 parameters

    CAUSE
    In debug builds, MFC defines a preprocessor macro that expands the new operator to an overloaded new operator that takes two extra parameters. The extra parameters are the source file name and code line number. MFC can use this information to report memory leaks to the programmer when in debug mode. This works for MFC classes because MFC provides overloads for new that accept the extra parameters.

    However, because this expansion is done by the preprocessor, it affects all usage of the new operator. If any non-MFC classes are used in the project, their new operator is also expanded, even if no suitable overload of new is available in that class. This is what happens in GDI+, and as a result, you receive a compile-time error message.

    WORKAROUND
    To work around this problem, choose one of the following methods:


    " Turn off the preprocessor expansion by commenting out the following lines of code in the source file:
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif


    NOTE: This method has the disadvantage of not using features in MFC that help you track memory allocations and leaks.


    " Provide GDI+ with overloads for new and delete operators by writing some code that accepts and discards the additional parameters. You can paste the following code, which demonstrates this approach, into a new header file and include the new header file instead of the Gdiplus.h file.

    //// Ensure that GdiPlus header files work properly with MFC DEBUG_NEW and STL header files.

    #define iterator _iterator

    #ifdef _DEBUG

    namespace Gdiplus
    {
     namespace DllExports
     {
      #include <GdiplusMem.h>
     };

     #ifndef _GDIPLUSBASE_H
     #define _GDIPLUSBASE_H
     class GdiplusBase
     {
      public:
       void (operator delete)(void* in_pVoid)
       {
        DllExports::GdipFree(in_pVoid);
       }

       void* (operator new)(size_t in_size)
       {
        return DllExports::GdipAlloc(in_size);
       }

       void (operator delete[])(void* in_pVoid)
       {
        DllExports::GdipFree(in_pVoid);
       }

       void* (operator new[])(size_t in_size)
       {
        return DllExports::GdipAlloc(in_size);
       }

       void * (operator new)(size_t nSize, LPCSTR lpszFileName, int nLine)
       {
        return DllExports::GdipAlloc(nSize);
       }

       void operator delete(void* p, LPCSTR lpszFileName, int nLine)
       {
        DllExports::GdipFree(p);
       }

      };
     #endif // #ifndef _GDIPLUSBASE_H
    }
    #endif // #ifdef _DEBUG

    #include <gdiplus.h>
    #undef iterator


    //// Ensure that Gdiplus.lib is linked.
    #pragma comment(lib, "gdiplus.lib")

     
     
    如果问题解决起来不妥或者有更好的解决办法,麻烦请告知,帮助曾经和你一样的入门者,谢谢。
  • 相关阅读:
    linux中bin和sbin目录的主要区别
    C# 值类型 引用类型 作为参数传递区别
    绿色免安装电视直播软件viviplayer
    [转]中科大校长建议停止以行政主导的高校评估
    MDS 7.0 使用中的问题 2(数据交换中图元的丢失)
    怪异的慢递公司一统快递
    [转]全国最好的医院列表
    多普达D600 问题集锦
    推荐PDG阅读器UnicornViewer
    [转]《乒乓世界》封面故事:中国男乒直板三人行
  • 原文地址:https://www.cnblogs.com/ourran/p/4616990.html
Copyright © 2011-2022 走看看