事先定义枚举类型标示绘制类型:
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);
}