如何用MFC畫出直線、虛線、折線、圓、橢圓、矩形、弧形(附上原始碼)

塵封的記憶0發表於2017-02-18

我建立的工程名字是預設的,叫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<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]);        //將畫筆選入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太難了,好多都看不懂了,哎,還是需要學習啊。

相關文章