本文主要是介绍如何用MFC画出直线、虚线、折线、圆、椭圆、矩形、弧形(附上源码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
我创建的工程名字是默认的,叫MFCApplication3
首先在MFCApplication3Dlg.h这个文件中添加构造说明:
public:CPen m_pen[5];CPoint m_point[5];
public:void DrawLine(CDC *pDC);void DrawPolyline(CDC *pDC);void DrawPolygon(CDC *pDC);void DrawRect(CDC *pDC);void DrawRoundRect(CDC *pDC);void DrawEllipse(CDC *pDC);void DrawArc(CDC *pDC);void DrawAngleArc(CDC *pDC);
然后在MFCApplication3Dlg.cpp这个文件里面更改些东西和加点程序:
首先找到这个CMFCApplication3Dlg::CMFCApplication3Dlg(CWnd* pParent /*=NULL*/): CDialogEx(CMFCApplication3Dlg::IDD, pParent),这个创建的时候就自动产生了,改成这样:
CMFCApplication3Dlg::CMFCApplication3Dlg(CWnd* pParent /*=NULL*/): CDialogEx(CMFCApplication3Dlg::IDD, pParent)
{m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);//初始化画笔、点变量数组m_pen[0].CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); //红色实线,1像素宽---参数:样式、宽度、颜色m_pen[1].CreatePen(PS_SOLID, 6, RGB(0, 255, 0)); //绿色实线,6像素宽m_pen[2].CreatePen(PS_DASH, 1, RGB(255, 0, 0)); //红色虚线,必须为一个像素宽m_pen[3].CreatePen(PS_DOT, 1, RGB(0, 0, 255)); //蓝色点线,必须为一个像素宽m_pen[4].CreatePen(PS_DASHDOTDOT, 1, RGB(255, 0, 0));//红色双点虚线,必须为一个像素宽//绘制多边形的点数组m_point[0].x = 10;m_point[0].y = 100;m_point[1].x = 10;m_point[1].y = 120;m_point[2].x = 100;m_point[2].y = 105;m_point[3].x = 170;m_point[3].y = 120;m_point[4].x = 170;m_point[4].y = 100;
}
然后再继续找到这个函数void CMFCApplication3Dlg::OnPaint(),将里面的东西改成这样:
void CMFCApplication3Dlg::OnPaint()
{CPaintDC dc(this); // 用于绘制的设备上下文CBrush brush(RGB(190, 190, 190));//画刷dc.SelectObject(&brush);//将画刷选入DCDrawLine(&dc);DrawPolyline(&dc);DrawPolygon(&dc);DrawRect(&dc);DrawRoundRect(&dc);DrawEllipse(&dc);DrawArc(&dc);DrawAngleArc(&dc);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();}
}
然后继续在刚刚的同一个的MFCApplication3Dlg.cpp : 实现文件中,添加绘图函数:
//绘制直线函数
void CLineTestDlg::DrawLine(CDC *pDC)
{CPen *oldPen=pDC->SelectObject(&m_pen[0]);//保存DC原始画笔for(int i=0;i<5;i++) {pDC->SelectObject(&m_pen[i]); //将画笔选入DCpDC->MoveTo(20,20+i*15); //设置DC当前点pDC->LineTo(170,20+i*15); //绘制直线}pDC->SelectObject(oldPen); //回复DC原画笔
}//绘制折线
void CLineTestDlg::DrawPolyline(CDC *pDC)
{CPen *oldPen=pDC->SelectObject(&m_pen[0]);//保存DC原始画笔for(int i=0;i<5;i++){for(int j=0;j<5;j++){m_point[j].y+=35; //每次下移35个像素}pDC->SelectObject(&m_pen[i]);pDC->Polyline(m_point,5);}pDC->SelectObject(oldPen);
}//绘制多边形
void CLineTestDlg::DrawPolygon(CDC *pDC)
{for(int j=0;j<5;j++)m_point[j].y+=75; //数组中的点下移75个像素CPen *oldPen=pDC->SelectObject(&m_pen[0]);//保存DC原始画笔for(int i=0;i<5;i++){for(int j=0;j<5;j++){m_point[j].y+=35; //每次绘制图形下移35个像素}pDC->SelectObject(&m_pen[i]);pDC->Polygon(m_point,5); //绘制多边形}pDC->SelectObject(oldPen);
}//绘制矩形
void CLineTestDlg::DrawRect(CDC *pDC)
{CRect rect(150,20,220,80);CPen *oldPen=pDC->SelectObject(&m_pen[0]);for(int i=0;i<5;i++){rect.OffsetRect(80,0);//每次绘图右移80个像素pDC->SelectObject(&m_pen[i]);pDC->Rectangle(&rect);//绘制矩形}pDC->SelectObject(oldPen);
}//绘制圆角矩形
void CLineTestDlg::DrawRoundRect(CDC *pDC)
{CRect rect(150,150,220,230);CPen *oldPen=pDC->SelectObject(&m_pen[0]);for(int i=0;i<5;i++){rect.OffsetRect(80,0);//每次绘图右移80个像素pDC->SelectObject(&m_pen[i]);pDC->RoundRect(&rect,CPoint(15,15));}pDC->SelectObject(oldPen);
}//绘制椭圆
void CLineTestDlg::DrawEllipse(CDC *pDC)
{CRect rect(150,260,220,310);CPen *oldPen=pDC->SelectObject(&m_pen[0]);for(int i=0;i<5;i++){rect.OffsetRect(80,0);pDC->SelectObject(&m_pen[i]);pDC->Ellipse(&rect);}pDC->SelectObject(oldPen);
}//绘制弧线
void CLineTestDlg::DrawArc(CDC *pDC)
{CRect rect(150,400,220,460);CPoint ptStart(170,440);CPoint ptEnd(210,400);CPen *oldPen=pDC->SelectObject(&m_pen[0]);for(int i=0;i<5;i++){rect.OffsetRect(80,0);ptStart+=CPoint(80,0);ptEnd+=CPoint(80,0);pDC->SelectObject(&m_pen[i]);pDC->Arc(&rect,ptStart,ptEnd);}pDC->SelectObject(oldPen);
}//绘制直线与弧线
void CLineTestDlg::DrawAngleArc(CDC *pDC)
{CRect rect(150,500,220,560);CPoint ptCenter(185,600);CPoint ptStart(150,630);int r=30;float angleStart=0;float angleEnd=180;CPen *oldPen=pDC->SelectObject(&m_pen[0]);for(int i=0;i<5;i++){rect.OffsetRect(80,0);ptStart+=CPoint(80,0);pDC->MoveTo(ptStart);pDC->SelectObject(&m_pen[i]);pDC->AngleArc(ptCenter.x,ptCenter.y,r,angleStart,angleEnd);}pDC->SelectObject(oldPen);
}
然后点击运行,图就出来了,里面什么都有,大家想用哪个就用哪个吧。
结束了!MFC太难了,好多都看不懂了,哎,还是需要学习啊。
这篇关于如何用MFC画出直线、虚线、折线、圆、椭圆、矩形、弧形(附上源码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!