朱金灿 GDI+是GDI的升级版本。在VC6.0中并没有配备GDI+的相关文件。那么如何在VC6.0使用GDI+呢?我从网上搜索了一些资料,并结合自己的使用,总结出一下两种方法。下面我各建一个MFC工程介绍这两种做法。 1. 找到GDI+库文件和头文件,把它放到一个文件夹GDI+Files。这个文件夹包括30个头文件和一个库文件。 2.新建一个单文档工程“UseGdiPlus”,把文件夹GDI+Files拷贝进工程文件夹。 3.进行工程设置,选择VC的Project菜单栏的Setting菜单项,在对话框中的C/C++选项卡中的Category一栏中选择General, 在Project Options中加入一个选择:/I "GDI+Files",如下图所示: 在Project->stting->Link->Object/libary中加入GDI+Files\GdiPlus.lib。 4.新建一个.h文件QGdiPlus.h,该头文件的内容如下: #pragma once // Add GDI+ support to MFC or WTL application. // // Include this file in StdAfx.h // // MFC: Add a QGdiPlus variable to your application class to start and stop GDI+. // ATL: Create a QGdiPlus local variable in _tWinMain. // Constructor starts, destructor stops. // Ensure that GdiPlus header files work properly with MFC DEBUG_NEW and STL header files. // Q317799: PRB: Microsoft Foundation Classes DEBUG_NEW Does Not Work with GDI+ #define iterator _iterator #ifdef _DEBUG static int nGdiPlusObjects = 0; namespace Gdiplus { namespace DllExports { #include <GdiplusMem.h> }; #ifndef _GDIPLUSBASE_H #define _GDIPLUSBASE_H class GdiplusBase { public: void (operator delete)(void* in_pVoid) { nGdiPlusObjects--; DllExports::GdipFree(in_pVoid); } void* (operator new)(size_t in_size) { nGdiPlusObjects++; return DllExports::GdipAlloc(in_size); } void (operator delete[])(void* in_pVoid) { nGdiPlusObjects--; DllExports::GdipFree(in_pVoid); } void* (operator new[])(size_t in_size) { nGdiPlusObjects++; return DllExports::GdipAlloc(in_size); } void * (operator new)(size_t nSize, LPCSTR /*lpszFileName*/, int /*nLine*/) { nGdiPlusObjects++; return DllExports::GdipAlloc(nSize); } void operator delete(void* p, LPCSTR /*lpszFileName*/, int /*nLine*/) { nGdiPlusObjects--; DllExports::GdipFree(p); } }; #endif // #ifndef _GDIPLUSBASE_H } #endif // #ifdef _DEBUG #define ULONG_PTR DWORD #include <gdiplus.h> #ifdef _MFC_VER #include <afx.h> #endif #undef iterator using namespace Gdiplus; #pragma comment (lib, "Gdiplus.lib") class QGdiPlus { public: QGdiPlus(): m_Token(0) { Gdiplus::GdiplusStartupInput input; Gdiplus::GdiplusStartup(& m_Token, & input, NULL); } ~QGdiPlus() { Gdiplus::GdiplusShutdown(m_Token); #ifdef _DEBUG #ifdef _MFC_VER if (nGdiPlusObjects > 0) afxDump << _T(">>> GdiPlus Memory Leaks: ") << nGdiPlusObjects << _T(" objects! <<<\n"); else if (nGdiPlusObjects < 0) afxDump << _T(">>> GdiPlus Multiple Deletes: ") << -nGdiPlusObjects << _T(" objects! <<<\n"); #endif #ifdef _ATL_VER #endif #endif } private: // The token we get from GDI+ ULONG_PTR m_Token; }; 添加完代码之后注意在stdafx.加上一句:#include "QGdiPlus.h" 接着我们在App类里添加一个 QGdiPlus m_GdiPlus,这个对象主要是负责装载gdi+和卸载gdi+。 好了,现在我们开始体会GDI的强大功能吧。 在视图类的OnDraw函数里添加代码,实现显示一个透明字符串。代码如下: void CUseGdiPlusView::OnDraw(CDC* pDC) { CUseGdiPlusDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here HDC hdc = pDC->GetSafeHdc(); Graphics mygraphics(hdc);//´´½¨Ò»¸öGraphics¶ÔÏó SolidBrush brush(Color(100,0,0,255));//´´½¨Ò»¸ö±ÊË¢ µÚÒ»ÏîÊÇ͸Ã÷¶È ºóÈýÏîÊÇRGBÖµ FontFamily fontFamily(L"Latha"); //Ñ¡ÔñÒ»ÖÖ×ÖÌå Font font(&fontFamily,1,FontStyleRegular,UnitInch); PointF pointF(5,5); mygraphics.DrawString(L"Hello Word!",-1,&font,pointF,&brush); }

评论