如何用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);//将画刷选入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(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]);        //将画笔选入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);
}

然后点击运行,图就出来了,里面什么都有,大家想用哪个就用哪个吧。

如何用MFC画出直线、虚线、折线、圆、椭圆、矩形、弧形(附上源码)_第1张图片

结束了!MFC太难了,好多都看不懂了,哎,还是需要学习啊。

你可能感兴趣的:(C#开发,mfc,源码)