20.1 三大坐标系:屏幕、客户区和非客户区
20.2 三大派生类:
a)CPaintDC
(客户区标准绘图),内部封装函数是:BeginPaint
和EndPaint
b)CClientDC
(客户区非标准绘图),内部是:::GetDC
和ReleaseDC
(CWnd::GetDC
的功能有写重复)和ReleaseDC
CDC* pDC = this->GetDC(); // CWnd::GetDC(非静态)
pDC->Ellipse(CRect(point.x-10,point.y-5,point.x+10,point.y+5));
this->ReleaseDC(pDC);//忘记了之后容易造成GDI泄漏,在任务管理器中可以观察泄漏情况
CDialogEx::OnLButtonDown(nFlags,point);
c)CWindowDC
(非客户区绘图),内部是:GetWindowDC
和ReleaseDC
d)CMemoryDC
(内存DC
),自己封装
GetDC
创建了一个新的GDI
对象:忘记了ReleaseDC
之后容易造成GDI泄漏??
20.3 基本图形函数:
直线:MoveTo、LineTo,LineTo...
折线:PolyLine
多边形(包括三角形):Polygon
矩形:Rectangle
圆形:Ellipse
圆角矩形:RoundRect
圆弧:Arc
、(ArcTo
也要与MoveTo
连用)
饼形:Pie
GDI对象包括:HDC、HICON、HCURCOR、HPEN、HBRUSH、HFONT、HBITMAP、HRGN、HPALLETE
20.4 GDI对象之一——CPen类对象
两对相反的函数是:CreatePenIndirect
和GetLogPen
,FromHandle
和operator HPEN
CPen::CPen: Constructs a CPen object.
CPen::CreatePen: Creates a logical cosmetic or geometric pen with the specified style, width,
and brush attributes, and attaches it to the CPen object.
CPen::CreatePenIndirect: Creates a pen with the style, width, and color given in a LOGPEN structure, and attaches it to the CPen object.
CPen::FromHandle: Returns a pointer to a CPen object when given a Windows HPEN.
CPen::GetLogPen: Gets a LOGPEN underlying structure.
CPen::operator HPEN
构造
方法一
LOGPEN lp = { PS_DASHDOTDOT ,1,0,RGB(255,0,0)};if (!(HPEN)m_pen)//如果不是因为二义性编译本不会出错{m_pen.CreatePenIndirect(&lp);}
方法二
m_pen.CreatePen(PS_DASHDOTDOT, 1, RGB(255, 0, 255));
方法三
CPen pen(PS_SOLID, 3, RGB(255, 0, 0));
20.5 GDI对象之二——CBrush类对象
两对反函数
operator HBRUSH与FromHandle
CreateBrushIndirect与GetLogBrush
CBrush::CBrush: Constructs a CBrush object.
CBrush::CreateBrushIndirect
Initializes a brush with the style, color, and pattern specified in a LOGBRUSH structure.
CBrush::CreateDIBPatternBrush
Initializes a brush with a pattern specified by a device-independent bitmap (DIB).
CBrush::CreateHatchBrush:栅格
CBrush::CreatePatternBrush:位图
CBrush::CreateSolidBrush:纯色
CBrush::CreateSysColorBrush 系统颜色
CBrush::GetLogBrush Gets a LOGBRUSH structure.
CBrush::operator HBRUSH
20.6 GDI对象之三——CFont类对象
两对反函数:
CreateFontIndirect和GetLogFont
CFont::operator HFONT和CFont::FromHandle
CFont::CFont Constructs a CFont object.
CFont::CreateFont: Initializes a CFont with the specified characteristics.
CFont::CreateFontIndirect:最常用标准创建字体
CFont::CreatePointFont简易创建字体
CFont::GetLogFont获取字体描述
CFont::operator HFONT
LOGFONT
lfFaceName 字体名称
lfHeight:子体大小
lfCharSet:GB2312_CHARSET
lfWeight :粗度(400是Normal,700是粗体)
lfWidth 一般是字体大小的一半
lfItalic 斜体;
lfUnderline 下划线;
lfStrikeOut 删除线;
lfEscapement:360的10倍,比如2700代表270度。
方法一
LOGFONT lf = {24};// lf.lfCharSet = GB2312_CHARSET;// lf.lfWidth = 14;
_tcscpy_s(lf.lfFaceName,_countof(lf.lfFaceName), _T("华文彩云"));
m_font.CreateFontIndirect(&lf);
方法二
if (!m_font.m_hObject)m_font.CreatePointFont(120, _T("宋体"));
输出
CFont* pOldFont = dc.SelectObject(&m_font);dc.SetBkColor(RGB(0, 255, 0));dc.SetBkMode(TRANSPARENT);dc.SetTextColor(RGB(255,0,0));dc.TextOut(100, 100, _T("测试CFont类字体创建函数!"));pOldFont->GetLogFont(&lf);