关于GDI+的一些使用基础设置

一、新建一个MFC的单文档工程,例如工程名字叫GDIPLUSTEST1。

二、在工程的stdafx.h头文件中添加入

#include "gdiplus.h"
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")或者在工程的属性->连接器->输入->附加依赖项中写入 gdiplus.lib

三、在工程的应用类中CGDIPLUSTEST1App的声明中添加一个成员变量

private:
 ULONG_PTR m_gdiplusToken;

再重写一个虚函数int ExitInstance();并实现

int CGDIPLUSTEST1App::ExitInstance()
{
 Gdiplus::GdiplusShutdown(m_gdiplusToken);//关闭GDI+
 return CWinApp::ExitInstance();
}

四、在应用类CGDIPLUSTEST1App的初始化函数

BOOL CGDIPLUSTEST1App::InitInstance()

 {

  Gdiplus::GdiplusStartupInput gdiplusStartupInput;
   Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);//开起GDI+

   CWinAppEx::InitInstance();

      ..........

 }

五、然后可以VIEW的ondraw函数中使用GDI+绘图

void CGDIPLUSTEST1View::OnDraw(CDC* pDC)
{
 CGDIPLUSTEST1Doc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 if (!pDoc)
  return;

 // TODO: 在此处为本机数据添加绘制代码
 Graphics graphics(pDC->m_hDC);

 GraphicsPath path;//构造一个路径
 path.AddEllipse(50, 50, 200, 100);
 //使用路径构造一个画刷
 PathGradientBrush pthGrBrush(&path);
 //将路径中心颜色设为蓝色
 pthGrBrush.SetCenterColor(Color(255, 0, 0, 255));
 
 //设置路径周围的颜色为蓝色,但alpha值为0
 Color colors[] = {Color(0,0,0,255)};
 INT count = 1;
 pthGrBrush.SetSurroundColors(colors, &count);

 graphics.FillRectangle(&pthGrBrush, 50, 50, 200, 100);
 
}

原文地址:https://www.cnblogs.com/lisuyun/p/3397765.html