GDI绘图类示例代码-CFont CPen CBrush

事先定义枚举类型标示绘制类型:

enum {Fonts, Pens, Brushes} m_Display;

CFont类用法:

void CGDISampleView::ShowFonts(CDC* pDC)
{
 LOGFONT logFont;//定义构建CFont对象的结构体
 logFont.lfHeight = 8;
 logFont.lfWidth = 0;
 logFont.lfEscapement = 0;
 logFont.lfOrientation = 0;
 logFont.lfWeight = FW_NORMAL;
 logFont.lfItalic = 0;
 logFont.lfUnderline = 0;
 logFont.lfStrikeOut = 0;
 logFont.lfCharSet = ANSI_CHARSET;
 logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
 logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
 logFont.lfQuality = PROOF_QUALITY;
 logFont.lfPitchAndFamily = VARIABLE_PITCH|FF_ROMAN;
 strcpy(logFont.lfFaceName , "Times New Roman");

 UINT position = 0;

 for(UINT x = 0; x<8 ; ++x)
 {
  logFont.lfHeight = 16 +(x * 8);
  CFont font;

  font.CreateFontIndirect(&logFont);
  CFont * oldFont = pDC ->SelectObject(&font);//存储旧的字体句柄

  position += logFont.lfHeight;
  pDC ->TextOut(20, position, "A Sample Font.");

  pDC ->SelectObject(oldFont);//还原设备环境
 }
}

CPen用法:

void CGDISampleView::ShowPens(CDC *pDC)
{
 UINT position = 10;
 INT32
 for(UINT x=0; x<16; ++x)
 {
  CPen pen(PS_SOLID, x*2+1, RGB(0,0,255));
  CPen* oldPen = pDC ->SelectObject(&pen);

  position += x*2+10;
  pDC ->MoveTo(20,position);
  pDC ->LineTo(400,position);

  pDC ->SelectObject(oldPen);
 }
}

CBrush类用法:

void CGDISampleView::ShowBrushes(CDC *pDC)

 UINT position = 0;
 CPen pen(PS_SOLID, 5, RGB(255,0,255));
 CPen* oldPen = pDC ->SelectObject(&pen);

 for(UINT x=0; x<7; ++x)//x标识画刷的七种样式
 {
  CBrush *brush;
  
  if(x == 6)
   brush = new CBrush(RGB(0,255,0));
  else
   brush = new CBrush(x,RGB(0,160,0));//设置画刷样式

  CBrush *oldBrush = pDC ->SelectObject(brush);

  position += 50;
  pDC ->Rectangle(20,position,400,position+40);
  
  pDC ->SelectObject(oldBrush);
  delete brush;
 }

 pDC ->SelectObject(oldPen);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值