****Dlg.h頭文件添加:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
//為工程添加畫筆、點(diǎn)變量數(shù)組
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構(gòu)造函數(shù)修改:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
CLineTestDlg::CLineTestDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CLineTestDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//初始化畫筆、點(diǎn)變量數(shù)組
m_pen[0].CreatePen(PS_SOLID,1,RGB(255,0,0)); //紅色實(shí)線,1像素寬---參數(shù):樣式、寬度、顏色
m_pen[1].CreatePen(PS_SOLID,6,RGB(0,255,0)); //綠色實(shí)線,6像素寬
m_pen[2].CreatePen(PS_DASH,1,RGB(255,0,0)); //紅色虛線,必須為一個(gè)像素寬
m_pen[3].CreatePen(PS_DOT,1,RGB(0,0,255)); //藍(lán)色點(diǎn)線,必須為一個(gè)像素寬
m_pen[4].CreatePen(PS_DASHDOTDOT,1,RGB(255,0,0));//紅色雙點(diǎn)虛線,必須為一個(gè)像素寬
//繪制多邊形的點(diǎn)數(shù)組
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函數(shù)?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void CLineTestDlg::OnPaint()
{
CPaintDC dc(this); // 用于繪制的設(shè)備上下文
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); // 用于繪制的設(shè)備上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast
// 使圖標(biāo)在工作區(qū)矩形中居中
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;
// 繪制圖標(biāo)
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}