没事做,玩玩GDI+(入门篇)

GDI+是Microsoft Windows XP 操作系统、Microsoft Windows Server 2003 操作系统的图形引擎,负责在屏幕和打印机上显示和打印信息。

GDI+是一个应用编程接口,通过一组c++类来提供接口的功能。

GDI+是早期Windows版本中包括的图形设备接口GDI的继任者。

 

一、包含GDI+头文件,使用GDI+命名空间

在stdafx.h文件中加入下面代码

//引入GDI+头文件

#include<gdiplus.h>

//使用GDI+的命名空间

using namespace Gdiplus;

 

二、GDI+资源的初始化与销毁

//全局变量,表明对GDI+的一个引用

ULONG_PTR gdiplusToken;

//GDI+系统资源的初始化

GdiplusStartupInput gdiplusStartupInput;

GdiplusStartup(&gdiplusToken, &gdiplusStartupInput,NULL);

 

//销毁GDI+资源

GdiplusShutDown(gdiplusToken);

 

三、GDI+例子

1、使用GDI+输出文本

//建立Graphics对象

Graphics graphics(this->GetDC()->m_hDC);

//设定文本输出使用的画笔、色彩、字体

 Pen pen(Color(255,0,0,255));
 SolidBrush brush(Color(255,0,0,255));
 FontFamily fontFamily(L"宋体");
 Gdiplus::Font font(&fontFamily,24,FontStyleRegular, UnitPixel);

//在窗口中央输出文本

 CRect rect;
 this->GetClientRect(&rect);
 PointF pointF(rect.right/2, rect.bottom/2);
 graphics.DrawString(L"GDI+程序示意",-1,&font, pointF, &brush);

 

2、在打印机中进行GDI+操作
void CGDISINGLEView::OnPrintOut()
{
 this->RedrawWindow();
 DOCINFO docInfo;

 //清空文档信息
 ZeroMemory(&docInfo,sizeof(DOCINFO));
 docInfo.cbSize = sizeof(DOCINFO);

 //打印文档名
 docInfo.lpszDocName = L"GdiplusPrint";

 //建立打印对话框
 PRINTDLG printDlg;
 ZeroMemory(&printDlg, sizeof(PRINTDLG));
 printDlg.lStructSize = sizeof(PRINTDLG);

 //需要返回DC
 printDlg.Flags = PD_RETURNDC;

 //显示打印对话框
 if (!PrintDlg(&printDlg))
 {
  AfxMessageBox(L"建立打印对话框失败");
  return;
 }
 else
 {
  //使用由打印对话框返回的设备环境句柄进行GDI+的所有操作
  //开始记录文档
  StartDoc(printDlg.hDC, &docInfo);
  StartPage(printDlg.hDC);
  //使用打印机设备环境句柄建立绘图平面类
  Graphics graphics(printDlg.hDC);
  //以下所有的输出都在打印机设备句柄中进行
  //加载图片
  Image image(L"sonnic.bmp");
  //绘制图片
  graphics.DrawImage(&image, 0.0f, 0.0f);

  Pen pen(Color(255,0,0,0));
  //画矩形
  graphics.DrawRectangle(&pen,200,500,200,150);
  //画椭圆
  graphics.DrawEllipse(&pen,200,500,200,150);
  //画直线
  graphics.DrawLine(&pen,200,500,400,650);
  //开始打印
  EndPage(printDlg.hDC);
  EndDoc(printDlg.hDC);
 }

 //释放打印资源
 if (printDlg.hDevMode)
  GlobalFree(printDlg.hDevMode);
 if (printDlg.hDevNames)
  GlobalFree(printDlg.hDevNames);
 if (printDlg.hDC)
  GlobalFree(printDlg.hDC);
}

 

================================================================================

我的主页:http://www.qingqingting.com/

我的淘宝:http://kanguolai.taobao.com/

 

 

原文链接: https://www.cnblogs.com/qqting/archive/2010/09/26/1835926.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    没事做,玩玩GDI+(入门篇)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/15518

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午3:26
下一篇 2023年2月7日 下午3:27

相关推荐