GDI
1.01 通过HDC获得CDC指针
void CDemoDlg::OnTest()
{
//获得当前DC的句柄
HDC hDC = ::GetDC(m_hWnd);
//通过HDC获得CDC指针
CDC* pDC = CDC::FromHandle(hDC);
//输出文本
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextAlign(TA_CENTER);
CRect rect;
GetClientRect(rect);
pDC->TextOut(rect.Width() / 2, 10, _T("Hello World!"));
}
1.02 创建屏幕DC
void CDemoDlg::OnTest()
{
CDC* pDC = new CDC();
//创建屏幕DC
pDC->CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
//设置背景模式
pDC->SetBkMode(TRANSPARENT);
//设置文本对齐方式
pDC->SetTextAlign(TA_CENTER);
//获得屏幕长度和高度
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);
//在屏幕输出文本
pDC->TextOut(cx / 2, cy / 2 - 40, _T("Hello World!"));
delete pDC;
}
1.03 选择预定义的GDI对象
void CDemoView::OnDraw(CDC* pDC)
{
//选择画刷
pDC->SelectStockObject(GRAY_BRUSH);
//选择画笔
pDC->SelectStockObject(BLACK_PEN);
//选择字体
pDC->SelectStockObject(ANSI_FIXED_FONT);
CRect rect(0, 0, 100, 100);
//设置背景模式
pDC->SetBkMode(TRANSPARENT);
//绘制椭圆
pDC->Ellipse(rect);
//输出文本
pDC->TextOut(rect.right, rect.bottom, _T("Hello World!"));
}
1.04 获取设备CDC
//获得按钮DC
CDC* pDC = pButton->GetDC();
//获得文本大小
CSize size = pDC->GetTextExtent(strText);
1.05 在长文本末端显示省略号
void CDemoView::OnDraw(CDC* pDC)
{
CString strText = _T("This a long string");
CRect rect(10, 10, 100, 30);
//输出文本,在长文本末端显示省略号
pDC->DrawText(strText, rect, DT_LEFT | DT_END_ELLIPSIS);
}
1.06 设置文本的对齐方式
void CDemoView::OnDraw(CDC* pDC)
{
CRect rect;
GetClientRect(rect);
pDC->MoveTo(rect.Width() / 2, 0);
pDC->LineTo(rect.Width() / 2, rect.Height() / 2);
pDC->MoveTo(0, 3 * rect.Height() / 4);
pDC->LineTo(rect.Width(), 3 * rect.Height() / 4);
//设置透明背景
pDC->SetBkMode(TRANSPARENT);
//设置左对齐
pDC->SetTextAlign(TA_LEFT);
pDC->TextOut(rect.Width() / 2, 0, _T("TA_LEFT"));
//设置中间对齐
pDC->SetTextAlign(TA_CENTER);
pDC->TextOut(rect.Width() / 2, 25, _T("TA_CENTER"));
//设置右对齐
pDC->SetTextAlign(TA_RIGHT);
pDC->TextOut(rect.Width() / 2, 50, _T("TA_RIGHT"));
//设置基线对齐
pDC->SetTextAlign(TA_CENTER | TA_BASELINE);
pDC->TextOut(rect.Width() / 4, 3 * rect.Height() / 4, _T("TA_BASELINE"));
//设置上对齐
pDC->SetTextAlign(TA_CENTER | TA_TOP);
pDC->TextOut(rect.Width() / 2, 3 * rect.Height() / 4, _T("TA_TOP"));
//设置下对齐
pDC->SetTextAlign(TA_CENTER | TA_BOTTOM);
pDC->TextOut(3 * rect.Width() / 4, 3 * rect.Height() / 4, _T("TA_BOTTOM"));
}
1.07 设置背景模式
void CDemoView::OnDraw(CDC* pDC)
{
//设置灰色背景
pDC->SelectStockObject(GRAY_BRUSH);
CRect rect;
GetClientRect(rect);
pDC->Rectangle(rect);
//设置不透明背景
pDC->SetBkMode(OPAQUE);
pDC->TextOut(10, 10, _T("Hello World!"));
//设置透明背景
pDC->SetBkMode(TRANSPARENT);
pDC->TextOut(10, 50, _T("Hello World!"));
}
1.07 绘制多变形
CRect rect;
GetClientRect(rect);
rect.InflateRect(-50, -50, -50, -50);
//绘制直线
pDC->MoveTo(rect.left, rect.top);
pDC->LineTo(rect.right, rect.bottom);
//绘制矩形
pDC->Rectangle(rect);
//绘制圆形或椭圆形
pDC->Ellipse(rect);
//绘制扇形
pDC->Pie(rect,
CPoint(rect.right, rect.CenterPoint().y),
CPoint(rect.CenterPoint().x, rect.right));
//绘制多边形
CPoint point[4];
point[0].x = rect.left + rect.Width() / 2;
point[0].y = rect.top;
point[1].x = rect.right;
point[1].y = rect.top + rect.Height() / 2;
point[2].x = point[0].x;
point[2].y = rect.bottom;
point[3].x = rect.left;
point[3].y = point[1].y;
pDC->Polygon(point, 4);
//绘制圆角矩形
pDC->RoundRect(rect, CPoint(50, 50));
1.08 设置画笔
void CDemoView::OnSetDashStyle()
{
m_nStyle = PS_DASH;
m_Pen.DeleteObject();
//创建画笔
m_Pen.CreatePen(m_nStyle, m_nWidth, m_crColor);
}
1.09 画刷
COLORREF crColor = RGB(255, 0, 0);
//创建实线画刷
m_Brush.CreateSolidBrush(crColor);
void CDemoView::OnCreateSolidBrush()
{
m_nType = 0;
m_Brush.DeleteObject();
COLORREF crColor = RGB(255, 0, 0);
//创建实线画刷
m_Brush.CreateSolidBrush(crColor);
}
void CDemoView::OnCreateHatchBrush()
{
m_nType = 1;
m_Brush.DeleteObject();
COLORREF crColor = RGB(255, 0, 0);
// 创建阴影线画刷
m_Brush.CreateHatchBrush(HS_CROSS, crColor);
}
void CDemoView::OnCreatePatternBrush()
{
m_nType = 3;
m_Brush.DeleteObject();
//创建图案
WORD HatchBits[8] = {0x11, 0x22, 0x44, 0x88, 0x11, 0x22, 0x44, 0x88};
//创建位图
CBitmap bmp;
bmp.CreateBitmap(8, 8, 1, 1, HatchBits);
//创建位图图案画刷
m_Brush.CreatePatternBrush(&bmp);
}
void CDemoView::OnLButtonDown(UINT nFlags, CPoint point)
{
CDC* pDC = GetDC();
//选择画笔
CPen* pOldPen = (CPen*)pDC->SelectStockObject(NULL_PEN);
//选择画刷
CBrush* pOldBrush = (CBrush*)pDC->SelectObject(&m_Brush);
pDC->Ellipse(point.x - 25, point.y - 25, point.x + 25, point.y + 25);
//恢复画笔
pDC->SelectObject(pOldPen);
//恢复画刷
pDC->SelectObject(pOldBrush);
CView::OnLButtonDown(nFlags, point);
}
1.10 使用字体
void CDemoView::OnDraw(CDC* pDC)
{
//初始坐标
CPoint point(10, 10);
for (int n = 0; n < 10; n++)
{
int nHeight = n * 5 + 10;
//创建字体
CFont font;
font.CreateFont(
nHeight,
0,
0,
0,
FW_NORMAL,
FALSE,
FALSE,
0,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS,
_T("Arial"));
//将字体选入设备环境
CFont* pOldFont = pDC->SelectObject(&font);
pDC->TextOut(point.x, point.y, _T("Hello World!"));
pDC->SelectObject(pOldFont);
point.y += nHeight;
}
}
1.11 显示位图资源
void CDemoView::OnDraw(CDC* pDC)
{
//加载位图
CBitmap bitmap;
if (!bitmap.LoadBitmap(IDB_BITMAP))
{
return;
}
//获得位图信息
BITMAP bmpinfo;
bitmap.GetBitmap(&bmpinfo);
//创建内存DC
CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
//将位图装入内存设备环境
CBitmap* pOldBitmap = MemDC.SelectObject(&bitmap);
//获得客户区矩形
CRect rect;
GetClientRect(&rect);
//将位图从内存设备环境拷贝到屏幕设备环境
pDC->BitBlt(
rect.left + (rect.Width() - bmpinfo.bmWidth) / 2,
rect.top + (rect.Height() - bmpinfo.bmHeight) / 2,
bmpinfo.bmWidth,
bmpinfo.bmHeight,
&MemDC,
0,
0,
SRCCOPY);
//恢复原来的位图
MemDC.SelectObject(pOldBitmap);
}
1.12 显示位图文件
void CDemoView::OnDraw(CDC* pDC)
{
//加载位图
HBITMAP hBitmap = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), _T("Demo.BMP"),
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
if (hBitmap == NULL)
{
return;
}
CBitmap* pBitmap = CBitmap::FromHandle(hBitmap);
//获得位图信息
BITMAP bmpinfo;
pBitmap->GetBitmap(&bmpinfo);
//创建内存DC
CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
//将位图装入内存设备环境
CBitmap* pOldBitmap = MemDC.SelectObject(pBitmap);
//获得客户区矩形
CRect rect;
GetClientRect(&rect);
//将位图从内存设备环境拷贝到屏幕设备环境
pDC->StretchBlt(
rect.left,
rect.top,
rect.Width(),
rect.Height(),
&MemDC,
0,
0,
bmpinfo.bmWidth,
bmpinfo.bmHeight,
SRCCOPY);
//恢复原来的位图
MemDC.SelectObject(pOldBitmap);
}
1.13 进行坐标映射
void CDemoView::OnDraw(CDC* pDC)
{
//获得客户区大小
CRect rcClient;
GetClientRect(rcClient);
//设置坐标映射模式
pDC->SetMapMode(MM_ANISOTROPIC);
//设置窗口(逻辑坐标)的原点
pDC->SetWindowOrg(0, 0);
//设置窗口(逻辑坐标)的范围
pDC->SetWindowExt(800, 800);
//设置视口(设备坐标)的原点
pDC->SetViewportOrg(rcClient.right / 2, rcClient.bottom / 2);
//设置视口(设备坐标)的范围
pDC->SetViewportExt(rcClient.right, rcClient.bottom);
pDC->Ellipse(-400, -400, 400, 400);
}
1.14 获得其他程序的图标
void CDemoDlg::OnTest()
{
//获得写字板程序图标
HICON hIcon=::ExtractIcon(AfxGetInstanceHandle(), _T("NotePad.exe"), 0);
CDC* pDC = GetDC();
if (hIcon != NULL)
{
//绘制图标
pDC->DrawIcon(15, 50, hIcon);
}
//销毁图标
DestroyIcon(hIcon);
}