文章目录
- GDI技术
- GDI 对象
- 画笔对象
- 画刷对象
- 位图对像
- 创建一个位图
- 字体对象
- 区域对象
- 区域组合
GDI技术
GDI(graphics Device Interface):图形设备接口,用于绘图。
In Windows CE, as in Windows-based desktop platforms, the graphics device interface (GDI) controls the display of text and graphics. You use GDI to draw lines, curves, closed figures, text, and bit images.
DC
DC: device context.设备上下文
void CGDIDlg::OnPaint()
{
//if (IsIconic())//判断窗口是不是最小化
//{
// CPaintDC dc(this); // 用于绘制的设备上下文
// SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// // 使图标在工作区矩形中居中
// int cxIcon = GetSystemMetrics(SM_CXICON);//获取系统图形信息
// int cyIcon = GetSystemMetrics(SM_CYICON);
// CRect rect;
// GetClientRect(&rect);
// int x = (rect.Width() - cxIcon + 1) / 2;
// int y = (rect.Height() - cyIcon + 1) / 2;
// // 绘制图标
// dc.DrawIcon(x, y, m_hIcon);
//}
//else
//{
// CDialogEx::OnPaint();
//}
//客户区绘图设备
CPaintDC dc(this);
//绘制线:默认起点(0,0),除非改变起点
dc.MoveTo(100,100);//设置新的起点
dc.LineTo(500, 100);//终点变成下一次新的起点
dc.MoveTo(100, 100);
dc.LineTo(400, 200);
//绘制矩形
dc.Rectangle(100, 255,300,355);
//绘制圆和椭圆
dc.Ellipse(100, 100, 200, 200);
//绘制圆角矩形
dc.RoundRect(250, 150, 450, 250, 10, 10);//方法一
CRect rect = { 250,150,450,250 };
dc.RoundRect(&rect, CPoint(10, 10));//方法二
//绘制三维矩形
//CRect rect = { 250,150,450,250 };
dc.Draw3dRect(&rect, RGB(255, 0, 0), RGB(255, 255, 0));
//绘制矩形边缘
dc.DrawEdge(&rect, BDR_RAISEDINNER,BF_RECT);
//绘制楔形/扇形
CPoint pt1(50, 50), pt2(100, 100);
dc.Pie(&rect, pt1, pt2);
//圆弧 不是封闭
dc.Arc(&rect, pt1, pt2);
//绘制圆弧
dc.ArcTo(&rect, pt1, pt2);
//绘制多边形
CPoint pts[10] = {
CPoint(181,71),
CPoint(208,147),
CPoint(288,150),
CPoint(225,199),
CPoint(247,277),
CPoint(181,231),
CPoint(115,275),
CPoint(139,199),
CPoint(74,150),
CPoint(155,148)
};
dc.Polygon(pts,sizeof(pts)/sizeof(pts[0]));
//绘制多边线
dc.Polyline(pts, sizeof(pts) / sizeof(pts[0]));
dc.PolylineTo(pts, sizeof(pts) / sizeof(pts[0]));
//绘制多组多边形
int ns[] = { 3,4,3 };
dc.PolyPolygon(pts, ns, 3);
DWORD ns1[] = { 3,4,3 };//每组几个点
dc.PolyPolyline(pts, ns1, 3);
//绘制贝尔赛曲线
CPoint pts1[3] = {
CPoint(181,71),
CPoint(208,147),
CPoint(288,150)
};
dc.PolyBezierTo(pts1, 3);
//设置背景
dc.SetBkColor(RGB(255, 0, 0));
//设置没有背景
dc.SetBkMode(TRANSPARENT);
//设置文字颜色
dc.SetTextColor(RGB(255, 0, 255));
//文本输出
dc.TextOut(0, 100, _T("绘图"));
CString str = _T("绘制文本");
dc.DrawText(str,&rect,DT_CENTER);
//绘制框架矩形
CBrush bruch(RGB(255, 0, 255));
dc.FrameRect(&rect,&bruch);
//反转矩形区域颜色
dc.InvertRect(&rect);
//绘制图标
dc.DrawIcon(CPoint(100, 100), m_hIcon);
::DrawIconEx(dc.m_hDC, 100, 100, m_hIcon,32,32,0,NULL,DI_IMAGE|DI_MASK);
//绘制弦
dc.Chord(&rect,pt1,pt2);
//绘制焦点
CRect rect1(100, 100, 200, 200);
dc.DrawFocusRect(&rect1);
//绘制点
dc.SetPixel(0, 100, RGB(255, 0, 0));
//获取某个点的颜色
dc.SetPixel(100, 100, RGB(255, 0, 0));
COLORREF color = dc.GetPixel(100, 100);
//填充
//CRect rect1(100, 100, 200, 200);
dc.FillSolidRect(rect1, RGB(255, 255, 0));
//获取dc
dc.m_hDC;
HDC hDC = dc.GetSafeHdc();
}
GDI 对象
画笔对象
//1.画笔对象
CPaintDC dc(this);
//CPen pen(PS_SOLID,2,RGB(255,0,0));
CPen pen;
//pen.CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
LOGPEN logPen = { PS_SOLID,{5,0},RGB(0,255,0) };
pen.CreatePenIndirect(&logPen);
dc.SelectObject(&pen);//使用GDI对象
dc.Rectangle(100, 50, 400, 300);
//Win32 方式创建一支画笔
HPEN hPen=::CreatePen(PS_SOLID, 2, RGB(255, 255, 0));
CPen* pPen=CPen::FromHandle(hPen);
CPen* olderPen=dc.SelectObject(pPen);
dc.Rectangle(0, 0, 100, 100);
//注意:一定要还原默认画笔
dc.SelectObject(olderPen);
画刷对象
//画刷对象
CBrush b1(RGB(255, 0, 0));
dc.SelectObject(&b1);//
dc.Rectangle(100, 100, 300, 200);
CBrush b2(HS_BDIAGONAL,RGB(255, 0, 0));
dc.SelectObject(&b2);//
dc.Ellipse(200, 200, 400, 400);
//画图
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP3);
CBrush b3(&bmp);
dc.SelectObject(&b3);//
dc.Rectangle(100, 100, 300, 200);
CBrush b4;
b4.CreateSolidBrush(RGB(255,255,0));
dc.SelectObject(&b4);
dc.Rectangle(100, 100, 300, 200);
CBrush b5;
b5.CreateHatchBrush(HS_VERTICAL, RGB(255, 255, 0));
//通过画刷结构
LOGBRUSH logBrush = { BS_SOLID,RGB(0,255,0),HS_VERTICAL };
CBrush b6;
b6.CreateBrushIndirect(&logBrush);
//系统画刷
CBrush b7;
b7.CreateSysColorBrush(COLOR_3DDKSHADOW);
dc.SelectObject(&b7);
//获取画刷结构
LOGBRUSH logBrush;
b7.GetLogBrush(&logBrush);
//从句柄获取画刷对象
HBRUSH hBrush = ::CreateSolidBrush(RGB(255,0,0));
CBrush* pBrush=CBrush::FromHandle(hBrush);
位图对像
//位图对象
CPaintDC dc(this);
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP3);
//获取图片信息
BITMAP logBmp;
bmp.GetBitmap(&logBmp);
//显示图片
//创建内存DC
CDC memDC;//绘图设备类
memDC.CreateCompatibleDC(&dc);
memDC.SelectObject(&bmp);
//贴图
//dc.BitBlt(0, 0, 500, 354, &memDC, 0, 0, SRCCOPY);
//拉伸贴图
dc.SetStretchBltMode(HALFTONE);//拉伸算法
//dc.StretchBlt(0,0,logBmp.bmWidth*3, logBmp.bmHeight*3,&memDC,0,0, logBmp.bmWidth,logBmp.bmHeight, SRCCOPY);
//上下反转
//dc.StretchBlt(0, 0, logBmp.bmWidth * 3, logBmp.bmHeight * 3, &memDC, logBmp.bmWidth, logBmp.bmHeight, -logBmp.bmWidth, -logBmp.bmHeight, SRCCOPY);
//贴满窗口
CRect rect;
GetClientRect(&rect);
dc.StretchBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, logBmp.bmWidth, logBmp.bmHeight, SRCCOPY);
贴满窗口(使用WM_SIZE 消息)
void CGDIObjectDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
Invalidate(TRUE);//前景和背景同时刷新
}
创建一个位图
//创建一个位图
CPaintDC dc(this);
CBitmap bmp2;
bmp2.CreateCompatibleBitmap(&dc, 300, 200);//默认创建的这张图是纯黑
//创建内存DC
CDC memDC2;
memDC2.CreateCompatibleDC(&dc);
memDC2.SelectObject(&bmp2);
memDC2.FillSolidRect(0, 0, 300, 200, RGB(255, 255, 255));
//绘制一个椭圆
memDC2.Ellipse(0, 0, 300, 200);
//保存图片
CImage img;
img.Attach(bmp2);
img.Save(L"D:\\a.bmp");
img.Detach();
//显示图片
dc.BitBlt(0, 0, 300, 200, &memDC2, 0, 0, SRCCOPY);
字体对象
//字体对象
CPaintDC dc(this);
dc.SetBkMode(TRANSPARENT);//设置背景透明
CFont font;
font.CreatePointFont(120, L"宋体");
CFont* pOldFont=dc.SelectObject(&font);
CString str=L"快点下班";
dc.TextOut(20,20,str);
CFont font2;
font2.CreateFont(16, 0, 0, 0, FW_BOLD, TRUE, TRUE, FALSE, DEFAULT_CHARSET, OUT_DEVICE_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH,L"楷体");
dc.SelectObject(&font2);
CString str1 = L"快点下班";
dc.TextOut(20, 100, str1);
dc.SelectObject(pOldFont);
LOGFONT logFont;
CFont* pFont = GetFont();
pFont->GetLogFont(&logFont);
logFont.lfHeight = 18;
wcscpy(logFont.lfFaceName,L"黑体");
CFont font3;
font3.CreateFontIndirect(&logFont);
dc.SelectObject(&font3);
dc.TextOut(20, 200, str1);
区域对象
//创建矩形区域
CRgn rgn1;
CPaintDC dc(this);
rgn1.CreateRectRgn(100, 100, 400, 300);
dc.SelectObject(&rgn1);
dc.Rectangle(50, 50, 350, 200);
//创建圆形区域
CRgn rgn2;
CPaintDC dc(this);
rgn2.CreateEllipticRgn(120,150,500,550.350);
dc.SelectObject(&rgn2);
dc.Ellipse(50, 50, 350, 200);
区域组合
BOOL CGDIObjectDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
//创建矩形区域
CRgn rgn1;
CPaintDC dc(this);
rgn1.CreateRectRgn(100, 100, 400, 300);
//创建圆形区域
CRgn rgn2;
rgn2.CreateEllipticRgn(120, 150, 500, 550.350);
rgn1.CombineRgn(&rgn1, &rgn2, RGN_OR);
//设置窗口为不规则窗口
SetWindowRgn(rgn1, TRUE);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
填充颜色(设置无边框)
//创建矩形区域
CRgn rgn1;
CPaintDC dc(this);
rgn1.CreateRectRgn(100, 100, 400, 300);
//创建圆形区域
CRgn rgn2;
rgn2.CreateEllipticRgn(120, 150, 500, 550.350);
rgn1.CombineRgn(&rgn1, &rgn2, RGN_OR);
dc.SelectObject(&rgn1);
CBrush brush(RGB(255, 0, 255));
dc.FillRgn(&rgn1,&brush);
设置点击命中消息
//点击命中消息
LRESULT CGDIObjectDlg::OnNcHitTest(CPoint point)
{
LRESULT result = CDialogEx::OnNcHitTest(point);
if (result == HTCLIENT)//点击客户区
{
result = HTCAPTION;//只有标题才可以移动
}
return result;
}