1、项目属性》连接器》输入》附加依赖项,添加“‘gdiplus.lib’”

2、头文件引入

1 #include <gdiplus.h>
2 
3 Gdiplus::GdiplusStartupInput m_GdiInput;
4 ULONG_PTR m_GDIToken;

 

3、程序入口和结束记得初始化和退出

 1 BOOL CGDIPLUSDrawApp::InitInstance()
 2 {
 3     //GDI+初始化
 4     GdiplusStartup(&m_GDIToken, &m_GdiInput, NULL);
 5 
 6         ……………………
 7 
 8     Gdiplus::GdiplusShutdown(m_GDIToken);
 9 
10     return FALSE;
11 }

4、一些基本GDI+操作

 1 void CGDIPLUSDrawDlg::OnPaint()
 2 {
 3     if (IsIconic())
 4     {
 5         CPaintDC dc(this); // 用于绘制的设备上下文
 6 
 7         SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
 8 
 9         // 使图标在工作区矩形中居中
10         int cxIcon = GetSystemMetrics(SM_CXICON);
11         int cyIcon = GetSystemMetrics(SM_CYICON);
12         CRect rect;
13         GetClientRect(&rect);
14         int x = (rect.Width() - cxIcon + 1) / 2;
15         int y = (rect.Height() - cyIcon + 1) / 2;
16 
17         // 绘制图标
18         dc.DrawIcon(x, y, m_hIcon);
19     }
20     else
21     {
22         CDialogEx::OnPaint();
23 
24         CClientDC dc(this);
25         Gdiplus::Graphics grx(dc.GetSafeHdc());
26         //获取窗口客户区区域
27         CRect rc;
28         GetClientRect(&rc);
29 
30         Gdiplus::Image image(_T("res\\main.png"));
31         grx.DrawImage(&image, Gdiplus::Rect(rc.left, rc.top, rc.Width(), rc.Height()));
32 
33         //矢量绘图
34         Gdiplus::Pen hPen(Gdiplus::Color(255, 255, 0, 0));
35         grx.DrawRectangle(&hPen, Gdiplus::Rect(20, 20, 50, 60));
36         grx.DrawArc(&hPen, 20, 20, 50, 60, 0, 10);
37 
38         //画刷
39         Gdiplus::HatchBrush hatch(Gdiplus::HatchStyleVertical, Gdiplus::Color(255, 0, 255, 0));
40         grx.FillRectangle(&hatch, Gdiplus::Rect(60,70,50,50));
41 
42         //位图做画刷来画文字
43         Gdiplus::Image txtimage(_T("res\\main.png"));
44         Gdiplus::TextureBrush tbrush(&txtimage);
45         Gdiplus::FontFamily family(_T("宋体"));
46         Gdiplus::Font font(&family, 30);
47         CString strImag = _T("位图画刷,画文本");
48         grx.DrawString((LPCTSTR)strImag, strImag.GetLength(), &font, Gdiplus::PointF(100, 50), &tbrush);
49 
50         //渐变画刷
51         Gdiplus::LinearGradientBrush lingrbrush(Gdiplus::Point(0, 0), Gdiplus::Point(rc.right, rc.bottom), Gdiplus::Color(255, 255, 0, 0), Gdiplus::Color(255, 0, 0, 255));
52         Gdiplus::Color clrs[] = {
53             Gdiplus::Color(255, 255, 0, 0),
54             Gdiplus::Color(255, 255, 255, 0),
55             Gdiplus::Color(255, 0, 0, 255),
56             Gdiplus::Color(255, 0, 255, 0)
57         };
58         Gdiplus::REAL pos[] = {
59             0.0f,
60             0.33f,
61             0.66f,
62             1.0f
63         };
64         lingrbrush.SetInterpolationColors(clrs, pos, 4);
65         grx.FillRectangle(&lingrbrush, Gdiplus::Rect(rc.left - 20, rc.top - 20, rc.Width() - 40, rc.Height() - 40));
66 
67         grx.ReleaseHDC(dc.GetSafeHdc());
68     }
69 }

 

posted on 2015-10-20 17:09  俗剑仙·情如梦  阅读(295)  评论(0编辑  收藏  举报