MFC和GDI+一起使用

VS2010,新建MFC项目,在头文件stdafx.h中添加:

1 #include <gdiplus.h>

2 using namespace Gdiplus; 3 #pragma comment (lib,"Gdiplus.lib")

定义类的变量 ULONG_PTR   gdiplusToken;

在BOOL CGDALDesktopApp::InitInstance()中添加:

1 GdiplusStartupInput gdiplusStartupInput;
2 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

在ExitInstance()中添加

1 int CGDALDesktopApp::ExitInstance() 2 { 3     //TODO: 处理可能已添加的附加资源

4  AfxOleTerm(FALSE); 5  GdiplusShutdown(gdiplusToken); 6     return CWinAppEx::ExitInstance(); 7 }

添加绘制的代码:

 1 void CGDALDesktopView::OnDraw(CDC* pDC)  2 {  3     CGDALDesktopDoc* pDoc = GetDocument();  4  ASSERT_VALID(pDoc);  5     if (!pDoc)  6         return;  7     

 8     //Graphics graphics(*pDC);

 9         Graphics g( pDC->GetSafeHdc() ); 10     //建立画笔

11  Pen pen_black(Color::Black); 12  Pen pen_white(Color::White); 13     //使用画笔绘制图形

14     pen_black.SetWidth(6); //设置画笔宽度

15     pen_black.SetStartCap(LineCapRoundAnchor); //设置开始笔帽

16     pen_black.SetEndCap(LineCapArrowAnchor); //设置结束笔帽 

17     g.DrawLine(&pen_black, 10, 10, 100, 10); 18     Rect rect1(0,0, 150, 80); 19     Rect rect2(10 + 170, 200, 150, 80); 20     Rect rect3(10 + 170*2, 200, 150, 80); 21     Rect rect4(10 + 170*3, 200, 150, 80); 22     g.DrawRectangle(&pen_black, rect1); 23      g.DrawRectangle(&pen_black, rect2); 24      g.DrawRectangle(&pen_black, rect3); 25      g.DrawRectangle(&pen_black, rect4); 26     //建立画刷 27      //实色画刷

28  SolidBrush brush_black(Color::Black); 29  SolidBrush brush_white(Color::White); 30  SolidBrush brush_blue(Color::Blue); 31      g.FillRectangle(&brush_blue, rect1); 32      //网格画刷

33      HatchBrush brush_hatch( HatchStyleDiagonalBrick, Color(255, 255, 0, 0), Color(30, 0, 255, 0)); 34     g.FillRectangle(&brush_hatch, rect2); 35      //贴图画刷

36      Image image(L"res\\qq.gif"); 37      TextureBrush brush_texture(&image); 38      g.FillRectangle(&brush_texture, rect3); 39      //渐变画刷(线形渐变)

40      LinearGradientBrush brush_gradient_line( Point(0, 0), Point(10, 10),Color(255, 255, 0, 0), Color(255, 0, 0, 255)); 41      g.FillRectangle(&brush_gradient_line, rect4); 42      //渐变画刷(路径渐变) 43      //PathGradientBrush...................... 44     //贴图画笔

45      Pen pen_texture(&brush_texture, 30); 46     g.DrawEllipse(&pen_texture, 600, 10, 150 ,150); 47     //启动抗锯齿功能

48      pen_black.SetWidth(1); 49  g.SetSmoothingMode(SmoothingModeAntiAlias); 50      g.DrawLine(&pen_black, 150, 5, 350 , 20); 51     //绘制图像 52     // 不进行缩放

53      g.DrawImage(&image, 10,50); 54      // 使用低质量的插补算法

55  g.SetInterpolationMode(InterpolationModeNearestNeighbor); 56      g.DrawImage( &image, Rect(100,50, 100, 100)); 57      // 使用中等质量的插补算法

58  g.SetInterpolationMode(InterpolationModeHighQualityBilinear); 59      g.DrawImage( &image, Rect(250,50, 100, 100)); 60      // 使用高质量的插补算法

61  g.SetInterpolationMode(InterpolationModeHighQualityBicubic); 62      g.DrawImage( &image, Rect(400,50, 100, 100)); 63     //路径

64  GraphicsPath path1; 65     path1.AddLine(300, 350, 500 ,350); 66     path1.AddArc(300, 300, 200, 100, 0, -180); 67     g.DrawPath(&pen_black, &path1); 68     g.FillPath(&brush_black, &path1); 69     //区域

70  GraphicsPath pathOuter; 71     pathOuter.AddRectangle(Rect(100, 320, 150 ,150)); 72  GraphicsPath pathInner; 73      pathInner.AddEllipse(Rect(150, 360, 90 ,80)); 74      Region rgn(&pathOuter); 75      rgn.Exclude(&pathInner); 76     g.FillRegion(&brush_blue, &rgn); 77     // TODO: 在此处为本机数据添加绘制代码

78 }
View Code

 

你可能感兴趣的:(mfc)