//矢量图和位图的区别
//介绍DC、了解DC
//CBrush brush(#ff0000);
//CClientDC dc(this->GetParent()->GetParent());
//CWindowDC dc(this);
//dc.FillRect(CRect(0,0,500,500),&brush);
//基本图形的绘制
//线段
pDC->MoveTo(20,30);
pDC->LineTo(300,30);
//矩形
pDC->Rectangle(20,50,300,200);
//椭圆
pDC->Ellipse(20,50,300,200);
CPen pen, *pOldPen;
CBrush brush, *pOldBrush;
//1.使用画笔
pen.CreatePen(PS_SOLID,6,#ff0000);
pOldPen=pDC->SelectObject(&pen);
pDC->MoveTo(20,250);
pDC->LineTo(300,250);
pDC->SelectObject(pOldPen);
//2.使用画刷(填充)
//创建一个蓝色的画刷
brush.CreateSolidBrush(#0000ff);
pOldBrush=pDC->SelectObject(&brush);
pDC->Rectangle(20,300,300,400);
pDC->SelectObject(pOldBrush);
//创建一个蓝色的网格画刷
brush.DeleteObject();
brush.CreateHatchBrush(HS_CROSS,#0000ff);
pOldBrush=pDC->SelectObject(&brush);
pDC->Rectangle(20,420,300,520);
pDC->SelectObject(pOldBrush);
//创建一个位图画刷
brush.DeleteObject();
CBitmap bitmap_brush;
bitmap_brush.LoadBitmap(IDB_BITMAP1);
brush.CreatePatternBrush(&bitmap_brush);
pOldBrush=pDC->SelectObject(&brush);
pDC->Rectangle(20,540,300,640);
pDC->SelectObject(pOldBrush);
//空画刷
brush.DeleteObject();
brush.CreateStockObject(NULL_BRUSH);
pDC->Rectangle(20,660,300,760);
pDC->SelectObject(pOldBrush);
//3.使用位图
//定义位图
CBitmap bitmap;
//保存位图信息;
BITMAP bmp;
//建立兼容设备
CDC dcCompatible;
dcCompatible.CreateCompatibleDC(pDC);
//从资源中装入位图
bitmap.LoadBitmap(IDB_BITMAP1);
//将位图选入设备
dcCompatible.SelectObject(&bitmap);
//获取位图信息
bitmap.GetBitmap(&bmp);
//拷贝
pDC->BitBlt(600,0,bmp.bmWidth,bmp.bmHeight,&dcCompatible,0,0,SRCCOPY);
//从文件中获取位图
HBITMAP hBitmap;
hBitmap=(HBITMAP)LoadImage(AfxGetInstanceHandle(),
"res\\ball.bmp", //实际位图文件的路径
IMAGE_BITMAP,0, 0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);//从文件中装入位图
bitmap.Detach();
bitmap.Attach(hBitmap);
bitmap.GetBitmap(&bmp);
dcCompatible.SelectObject(&bitmap);
//绘制非透明位图(将dcCompatible上的内容拷贝到pDC中)
pDC->BitBlt(600,40,bmp.bmWidth,bmp.bmHeight,&dcCompatible,0,0,SRCCOPY);
pDC->StretchBlt(600,100,bmp.bmWidth*2,bmp.bmHeight*2,&dcCompatible,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
//绘制透明位图(将dcCompatible上的内容拷贝到pDC中,同时去掉最后一个参数指定的颜色)
pDC->TransparentBlt(600,200,bmp.bmWidth,bmp.bmHeight,&dcCompatible,0,0,bmp.bmWidth,bmp.bmHeight,#ff0000);
//4.输出文字
pDC->SetTextColor(#ff0000);
pDC->SetBkMode(TRANSPARENT);
CFont font;//当前字体
font.CreateFont(32,0,0,0,FW_NORMAL,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY,DEFAULT_PITCH | FF_SWISS,"宋体");
CFont *pOldFont=pDC->SelectObject(&font);
pDC->TextOut(50,320,"输出文字");
CSize cs;
cs=pDC->GetTextExtent("输出文字");
pDC->TextOut(50+cs.cx,320,"abcdefg");
pDC->SelectObject(pOldFont);
//复杂图形
int step=30;
int center_x=700;
int center_y=500;
float x,y;
x=center_x-300;
y=sin(x/600*6*3.14)*100;
pDC->MoveTo(x,center_y+y);
while (x<center_x+300)
{
y=sin(x/600*6*3.14)*100;
pDC->LineTo(x,center_y+y);
x+=step;
}
posted on 2012-05-06 23:19
意吟 阅读(1065)
评论(0) 编辑 收藏 引用 所属分类:
基础 、
MFC