-
Notifications
You must be signed in to change notification settings - Fork 63
/
DlgHist.cpp
152 lines (114 loc) · 2.42 KB
/
DlgHist.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// DlgHist.cpp : 实现文件
//
#include "stdafx.h"
#include "DIPDemo.h"
#include "DlgHist.h"
// CDlgHist 对话框
IMPLEMENT_DYNAMIC(CDlgHist, CDialog)
CDlgHist::CDlgHist(CWnd* pParent /*=NULL*/)
: CDialog(CDlgHist::IDD, pParent)
, m_pdHist(NULL)
{
}
CDlgHist::~CDlgHist()
{
}
void CDlgHist::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CDlgHist, CDialog)
ON_WM_PAINT()
END_MESSAGE_MAP()
// CDlgHist 消息处理程序
void CDlgHist::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CDialog::OnPaint()
// 字符串
CString str;
// 获取绘制坐标的控件
CWnd* pWnd = GetDlgItem(IDC_HIST);
// 指针
CDC* pDC = pWnd->GetDC();
pWnd->Invalidate();
pWnd->UpdateWindow();
pDC->Rectangle(0,0,320,250);
// 创建画笔对象
CPen* pPenRed = new CPen;
// 红色画笔
pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0));
// 创建画笔对象
CPen* pPenBlue = new CPen;
// 蓝色画笔
pPenBlue->CreatePen(PS_SOLID,1,RGB(0,0, 255));
// 创建画笔对象
CPen* pPenGreen = new CPen;
// 绿色画笔
pPenGreen->CreatePen(PS_DOT,1,RGB(0,255,0));
// 选中当前红色画笔,并保存以前的画笔
CGdiObject* pOldPen = pDC->SelectObject(pPenRed);
// 绘制坐标轴
pDC->MoveTo(10,10);
// 垂直轴
pDC->LineTo(10,240);
// 水平轴
pDC->LineTo(310,240);
// 写X轴刻度值
str.Format("0");
pDC->TextOut(10, 243, str);
str.Format("50");
pDC->TextOut(60, 243, str);
str.Format("100");
pDC->TextOut(110, 243, str);
str.Format("150");
pDC->TextOut(160, 243, str);
str.Format("200");
pDC->TextOut(210, 243, str);
str.Format("255");
pDC->TextOut(260, 243, str);
int i;
// 绘制X轴刻度
for (i = 0; i < 256; i += 5)
{
if ((i & 1) == 0)
{
// 10的倍数
pDC->MoveTo(i + 10, 240);
pDC->LineTo(i + 10, 234);
}
else
{
// 5的倍数
pDC->MoveTo(i + 10, 240);
pDC->LineTo(i + 10, 236);
}
}
// 绘制X轴箭头
pDC->MoveTo(305,235);
pDC->LineTo(310,240);
pDC->LineTo(305,245);
// 绘制X轴箭头
pDC->MoveTo(10,10);
pDC->LineTo(5,15);
pDC->MoveTo(10,10);
pDC->LineTo(15,15);
double maxHist = 0;
for (i = 0; i < 256; i++)
{
maxHist = max(maxHist, m_pdHist[i]);
}
pDC->SelectObject(pPenBlue);
for (i = 0; i < 256; i++)
{
pDC->MoveTo(i + 10, 240);
pDC->LineTo(i + 10, int(240 - m_pdHist[i] / maxHist * 240));
};
// 恢复以前的画笔
pDC->SelectObject(pOldPen);
// 删除新的画笔
delete pPenRed;
delete pPenBlue;
delete pPenGreen;
}