首页 > 代码库 > MFC 直线 虚线 折线 圆 椭圆 矩形 弧形
MFC 直线 虚线 折线 圆 椭圆 矩形 弧形
****Dlg.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);
****Dlg.cpp构造函数修改:
CLineTestDlg::CLineTestDlg(CWnd* pParent /*=NULL*/) : CDialogEx(CLineTestDlg::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; }***Dlg.cpp onpaint函数
void CLineTestDlg::OnPaint() { CPaintDC dc(this); // 用于绘制的设备上下文 CBrush brush(RGB(190,190,190));//画刷 dc.SelectObject(&brush);//将画刷选入DC DrawLine(&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(); } }定义绘图函数:
//绘制直线函数 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]); //将画笔选入DC pDC->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); }
运行效果:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。