VS2008 基于对话框的工程项目
环境初始化
1、在“stdafx.h”加入头文件
#include <GdiPlus.h>
#pragma comment(lib,"gdiplus.lib")
using namespace Gdiplus;
2、在XXApp类的
1)头文件:
增加变量:
ULONG_PTR m_gdiplusToken;
2)源文件:
InitInstance()函数增加初始化语句
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
3)重载函数virtual int ExitInstance();
在函数里增加以下语句:
Gdiplus::GdiplusShutdown(m_gdiplusToken);
3、创建GDI+操作对象(通过CDC对象创建)
在OnPaint里实现
CPaintDC dc(this);
Graphics graphics(dc.GetSafeHdc());或者Graphics graphics(dc.m_hDC);
4、画图、画线、加载图片
1)创建画笔Pen对象
Pen pen(Color(0,0,0), 2);
2)创建画刷对象
SolidBrush sb(Color(205, 205, 205));
3)画矩形
FillRectangle(&sb, x1, y1, x2, y2);
4)画线
DrawLine(&pen, x1, y1, x2, y2);
5)写字(GDI+使用UNICODE格式)
FontFamily fontFamily(L"幼圆");
Gdiplus::Font font(&fontFamily, 12, FontStyleRegular, UnitPoint);
PointF pF(pX, pY+30);
DrawString(CT2W(m_szCode.c_str()), (int)wcslen(CT2W(m_szCode.c_str())), &font, pF, &sb)
6)显示图片
string szAppPath;
GetAppPath(szAppPath);
string szbgFile2 = szAppPath;
szbgFile2.append("\\1.png");
CA2W szUnicodePath2(szbgFile2.c_str());
Gdiplus::Image im2(szUnicodePath2.m_psz);
pGraphics->DrawImage(&im2,m_rc.left+5,m_rc.top+10,0,0,im2.GetWidth(), im2.GetHeight(),UnitPixel);