Repository navigation
Expand file tree
/
Copy pathGraphCtl.cpp
More file actions
193 lines (138 loc) · 4.35 KB
/
Copy pathGraphCtl.cpp
File metadata and controls
193 lines (138 loc) · 4.35 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// GraphCtl.cpp: реализация CGraphCtl
#include "pch.h"
#include "GraphCtl.h"
// CGraphCtl
STDMETHODIMP CGraphCtl::get_MinX(SHORT* pVal)
{
*pVal = m_MinX;
return S_OK;
}
STDMETHODIMP CGraphCtl::put_MinX(SHORT newVal)
{
if (newVal < -5) return Error("The value must be greater than -5.");
if (newVal >= m_MaxX) return Error("The minimum value of X must be less than the maximum value of X.");
m_MinX = newVal;
FireViewChange();
return S_OK;
}
STDMETHODIMP CGraphCtl::get_MaxX(SHORT* pVal)
{
*pVal = m_MaxX;
return S_OK;
}
STDMETHODIMP CGraphCtl::put_MaxX(SHORT newVal)
{
if (newVal <= m_MinX) return Error("The maximum value of X must be greater than the minimum value of X.");
m_MaxX = newVal;
FireViewChange();
return S_OK;
}
STDMETHODIMP CGraphCtl::get_MinY(SHORT* pVal)
{
*pVal = m_MinY;
return S_OK;
}
STDMETHODIMP CGraphCtl::put_MinY(SHORT newVal)
{
if (newVal < -5) return Error("The value must be greater than -5.");
if (newVal >= m_MaxY) return Error("The minimum value of Y must be less than the maximum value of Y.");
m_MinY = newVal;
FireViewChange();
return S_OK;
}
STDMETHODIMP CGraphCtl::get_MaxY(SHORT* pVal)
{
*pVal = m_MaxY;
return S_OK;
}
STDMETHODIMP CGraphCtl::put_MaxY(SHORT newVal)
{
if (newVal <= m_MinY) return Error("The maximum value of Y must be greater than the minimum value of Y.");
m_MaxY = newVal;
FireViewChange();
return S_OK;
}
void CGraphCtl::CalcPoints(const RECT& rc)
{
m_points.clear(); // Очищаем предыдущие точки
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
for (int x = 0; x < width; ++x)
{
// Пример функции: y = sin(x), масштабируем для визуализации
double normalizedX = (x - width / 2.0) * 0.05; // Нормализация координаты X
double functionValue = sin(normalizedX); // Значение функции (пример sin(x))
POINT pt;
pt.x = x;
pt.y = height / 2 - static_cast<int>(functionValue * 50); // Масштабирование для отрисовки
m_points.push_back(pt); // Добавляем точку в список
}
}
void CGraphCtl::DrawAxes(HDC& hdc, RECT& rc)
{
HBRUSH hBrushBackground = CreateSolidBrush(RGB(240, 240, 240)); // Светло-серый фон
FillRect(hdc, &rc, hBrushBackground);
DeleteObject(hBrushBackground);
HPEN hAxisPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 0)); // Чёрные оси
SelectObject(hdc, hAxisPen);
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
// Горизонтальная ось
MoveToEx(hdc, 0, height / 2, NULL);
LineTo(hdc, width, height / 2);
// Вертикальная ось
MoveToEx(hdc, width / 2, 0, NULL);
LineTo(hdc, width / 2, height);
DeleteObject(hAxisPen);
}
void CGraphCtl::DrawGraph(HDC& hdc)
{
// 4. Рисуем функцию
HPEN hGraphPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); // Красная линия для функции
SelectObject(hdc, hGraphPen);
if (!m_points.empty())
{
MoveToEx(hdc, m_points[0].x, m_points[0].y, NULL);
for (size_t i = 1; i < m_points.size(); ++i)
{
LineTo(hdc, m_points[i].x, m_points[i].y);
}
}
DeleteObject(hGraphPen);
}
LRESULT CGraphCtl::OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
if (m_LButtonClick == FALSE && m_RButtonClick == TRUE)
{
m_LButtonClick = TRUE;
m_RButtonClick = FALSE;
m_ShowGraph = TRUE;
Fire_ShowGraph(TRUE);
bHandled = TRUE;
FireViewChange();
}
else
{
bHandled = TRUE;
return Error("График уже нарисован");
}
return 0;
}
LRESULT CGraphCtl::OnRButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
if (m_RButtonClick == FALSE && m_LButtonClick==TRUE)
{
m_RButtonClick = TRUE;
m_LButtonClick = FALSE;
m_ShowGraph = FALSE;
Fire_ShowGraph(FALSE);
bHandled = TRUE;
FireViewChange();
}
else
{
bHandled = TRUE;
return Error("График уже скрыт");
}
return 0;
}