#include "GdiPlus.h"
// 使用GDI+ 命名空间
using namespace Gdiplus;
// 与GDI+ 相关的其它头文件,如:GraphicsPath类所在的头文件
#include "GdiplusBase.h"
#include "GdiPlusPath.h"
// 导入GDI+ lib文件
#pragma comment(lib, "GdiPlus.lib")
// GDI+ 资源的初始化与销毁
// 全局变量,表明对GDI+的一个引用
ULONG_PTR m_gdiplusToken;
// 在使用GDI+ 的类或窗口的构造函数里初始化GDI+的资源
CTestGraphicsView::CTestGraphicsView()
{
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
// TODO: Initial GDI+ related handler 初始化GDI+相关句柄
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
}
// 在使用GDI+ 的类或窗口的析构函数里销毁GDI+的资源
CTestGraphicsView::~CTestGraphicsView()
{
Gdiplus::GdiplusShutdown(m_gdiplusToken);
}
///////////////构造Graphics对象//////////////////////
------------------------------------在单文档的OnDraw()函数里构造Graphics对象-------------------------------
void CTestGraphicsView::OnDraw(CDC* pDC)
{
CTestGraphicsDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
Graphics g(pDC->m_hDC);
GraphicsPath path;
path.AddRectangle(Rect(40,10,200,50));
g.DrawPath(&Pen(Color.Red),&path);
}
---------------------------------------在对话框的OnPaint()函数里构造Graphics对象--------------------------
void CTestGraphicsView::OnPaint()
{
CPaintDC dc(this); // device context for painting
OnDrawGraph(&dc);
}
void CTestGraphicsView::OnDrawGraph(CDC *pDC)
{
Graphics g(pDC->m_hDC);
GraphicsPath path;
path.AddRectangle(Rect(40,10,200,50));
g.DrawPath(&Pen(Color.Red),&path);
}