****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);
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定義繪圖函數:(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); }