画图使用
void CMFCDCView::OnDrawCdc()
{
// TODO: Add your command handler code here
CDC dc;
dc.CreateDC("DISPLAY",NULL,NULL,NULL);
dc.TextOut(50,50,"Hello CDC");
dc.DeleteDC();
}
void CMFCDCView::OnDrawWindowdc()
{
// TODO: Add your command handler code here
CWindowDC dc(AfxGetMainWnd());
dc.TextOut(0,0,"Hello CWindowDC");
}
void CMFCDCView::OnDrawClientdc()
{
// TODO: Add your command handler code here
CClientDC dc(this);
dc.TextOut(0,0,"Hello CClientDC");
}
void CMFCDCView::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.TextOut(0,100,"Hello CPaintDC");
if (m_hMetaFile)
{
dc.PlayMetaFile(m_hMetaFile);
}
}
void CMFCDCView::OnDrawMetafiledc()
{
// TODO: Add your command handler code here
CMetaFileDC dcMeta;
//创建
dcMeta.Create("c:\\meta.dat");
//添加绘制命令
dcMeta.TextOut(100,200,"Hello Meta");
dcMeta.MoveTo(200,200);
dcMeta.LineTo(400,200);
//...
//关闭
m_hMetaFile=dcMeta.Close();
//显示
CClientDC dc(this);
dc.PlayMetaFile(m_hMetaFile);
//销毁
//DeleteMetaFile(hMetaFile);
}
画线
void CMFCDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//记录鼠标按下的位置
m_ptBegin=point;
CView::OnLButtonDown(nFlags, point);
}
void CMFCDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
CBrush *pOldBrush=(CBrush*)dc.SelectObject(
CBrush::FromHandle((HBRUSH)
dc.SelectStockObject(NULL_BRUSH)));
switch(m_nDrawType)
{
case 1://直线
dc.MoveTo(m_ptBegin);
dc.LineTo(point);
break;
case 2://矩形
dc.Rectangle(m_ptBegin.x,m_ptBegin.y,
point.x,point.y);
break;
case 3://椭圆
dc.Ellipse(m_ptBegin.x,m_ptBegin.y,
point.x,point.y);
break;
}
dc.SelectObject(pOldBrush);
CView::OnLButtonUp(nFlags, point);
}
void CMFCDrawView::OnDrawLine()
{
// TODO: Add your command handler code here
m_nDrawType=1;
}
void CMFCDrawView::OnDrawRect()
{
// TODO: Add your command handler code here
m_nDrawType=2;
}
void CMFCDrawView::OnDrawEllipic()
{
// TODO: Add your command handler code here
m_nDrawType=3;
}
画图对象的使用
void CMFCGdiObjectView::OnDrawPen()
{
// TODO: Add your command handler code here
CClientDC dc(this);
//创建画笔
CPen pen(PS_SOLID,5,RGB(255,0,0));
//将新建画笔选入到当前设备
CPen *pOldPen=(CPen*)dc.SelectObject(&pen);
//使用画笔绘制
dc.Rectangle(50,50,150,150);
//将原来的画笔选入
dc.SelectObject(pOldPen);
//删除画笔
pen.DeleteObject();
}
void CMFCGdiObjectView::OnDrawBrush()
{
// TODO: Add your command handler code here
CClientDC dc(this);
//创建画刷
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP2);
CBrush brush(&bmp);
//CBrush brush(HS_BDIAGONAL,RGB(0,0,255));
//将画刷选入当前设备
CBrush *pOldBrush=(CBrush*)
dc.SelectObject(&brush);
//使用画刷
dc.Rectangle(200,50,300,300);
//将原来的画刷选入
dc.SelectObject(pOldBrush);
//删除画刷
brush.DeleteObject();
}
void CMFCGdiObjectView::OnDrawFont()
{
// TODO: Add your command handler code here
CClientDC dc(this);
//创建字体
CFont font;
font.CreatePointFont(500,"黑体");
//选入到dc
CFont *pOldFont=(CFont *)dc.SelectObject(&font);
//使用
dc.TextOut(200,200,"Hello CFont");
//选出
dc.SelectObject(pOldFont);
//删除
font.DeleteObject();
}
void CMFCGdiObjectView::OnDrawBitmap()
{
// TODO: Add your command handler code here
CClientDC dc(this);
//从指定路径加载位图
HBITMAP hBmp=(HBITMAP)
LoadImage(NULL,"F:\\6582c1c0.bmp",
IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
CBitmap bmp;
//将句柄附加到对象上
bmp.Attach(hBmp);
//加载位图资源
//bmp.LoadBitmap(IDB_BITMAP3);
//创建和当前dc兼容的dc
CDC dcBmp;//兼容dc
dcBmp.CreateCompatibleDC(&dc);
//将位图选入到兼容dc,dcBmp
dcBmp.SelectObject(&bmp);
//从兼容dc拷贝位图到当前dc
//dc.BitBlt(0,0,300,200,&dcBmp,0,0,SRCCOPY);
//位图可以根据给定的位置伸缩
dc.StretchBlt(0,0,800,600,&dcBmp,0,0,428,247,
SRCCOPY);
}
void CMFCGdiObjectView::OnDrawPalette()
{
// TODO: Add your command handler code here
LOGPALETTE *pLogPalette=NULL;
//计算调色板所占内存空间大小
int nLen=
sizeof(LOGPALETTE)+sizeof(PALETTEENTRY)*256;
//分配内存空间
pLogPalette=(LOGPALETTE *)malloc(nLen);
//赋值
pLogPalette->palVersion=0x300;
pLogPalette->palNumEntries=256;
for (int i=0;i<256;i++)
{
pLogPalette->palPalEntry[i].peBlue=i;
pLogPalette->palPalEntry[i].peGreen=0;
pLogPalette->palPalEntry[i].peRed=0;
}
//创建调色板
CPalette pal;
pal.CreatePalette(pLogPalette);
free(pLogPalette);
//在绘图设备中使用调色板
CClientDC dc(this);
//选入
CPalette *pOldPalette=
dc.SelectPalette(&pal,FALSE);
//加载调色板
dc.RealizePalette();
//使用调色板中的颜色
CPen pen(PS_SOLID,10,PALETTEINDEX(200));
CPen * pOldPen=(CPen*)dc.SelectObject(&pen);
dc.MoveTo(100,100);
dc.LineTo(300,300);
dc.SelectPalette(pOldPalette,FALSE);
dc.SelectObject(pOldPen);
}
void CMFCGdiObjectView::OnDrawRgn()
{
// TODO: Add your command handler code here
//构造基本的几何图形
CRgn rgn1;
rgn1.CreateEllipticRgn(200,200,500,500);
CRgn rgn2;
rgn2.CreateEllipticRgn(300,200,600,500);
//将两个图形进行几何运算
rgn1.CombineRgn(&rgn1,&rgn2,RGN_XOR );
//绘图图形
CClientDC dc(this);
CBrush brush(RGB(0,0,255));
dc.FillRgn(&rgn1,&brush);
//填充图形的边框
//CBrush brushFrame(RGB(255,0,0));
//dc.FrameRgn(&rgn1,&brushFrame,1,5);
//设置rgn1为框架窗口的区域
//AfxGetMainWnd()->SetWindowRgn(rgn1,TRUE);
}
一 MFC绘图
1 MFC绘图相关类
CDC-绘图设备类
CreateDC(
LPCTSTR lpszDriverName, //设备驱动的名称
LPCTSTR lpszDeviceName, //设备的名称
LPCTSTR lpszOutput, //电脑与设备连接的接口名称
const void* lpInitData //设备的参数
);
CWindowDC-封装了整个窗口的DC,包括标题、边框和客户区。
CClientDC-封装了窗口的客户区DC
CPaintDC-封装了窗口的客户区DC,只能用在WM_PAINT消息处理
函数中。
CMetaFileDC-封装了Windows图元文件的处理。
CGdiObject-绘图对象类
CPen-画笔
CBrush-画刷
CFont-字体
CBitmap-位图
CPalette-调色板
为什么使用调色板?
调色板使用步骤:
1 创建调色板
1.1 结构
typedef struct tagLOGPALETTE {
WORD palVersion; //系统版本号,0x300
WORD palNumEntries;//颜色的数量
PALETTEENTRY palPalEntry[1];//颜色表数组
} LOGPALETTE;
typedef struct tagPALETTEENTRY { // pe
BYTE peRed;
BYTE peGreen;
BYTE peBlue;
BYTE peFlags;
} PALETTEENTRY;
CPalette::CreatePalette
2 将调色板选到当前dc
CDC::SelectPalette
3 加载调色板
CDC::RealizePalette
4 使用调色板(使用的是准备好的颜色)
PALETTEINDEX
5 选出调色板
CDC::SelectPalette
CRgn-区域(构造一个不规则的区域)
1 作用:基于基本的几何图形,构建复杂的几何图形
2 使用
1 使用一系列的CreateXXX函数创建基本的图形
2 使用CombineRgn将两个图形进行几何运算
3 绘制/填充几何图形
CDC::FillRgn-填充
CDC::FrameRgn-填充边框
4 可以使用CRgn创建不规则窗体
CWnd::SetWindowRgn
2 MFC绘图的实现