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
| #include "pch.h" #include "Graph.h" #include <stdarg.h> #pragma warning(disable:4244)
GraphControl::GraphControl(): SIZE(0), DELTAX(0), pDC(nullptr), oldPen(nullptr), oldBrush(nullptr) { bgPen.CreatePen(PS_SOLID, 4, RGB(30, 30, 30)); bgBrush.CreateSolidBrush(RGB(30, 30, 30)); gridPen.CreatePen(PS_SOLID, 1, RGB(100, 100, 100)); }
void GraphControl::init(CStatic* graph) { pDC = graph->GetDC(); graph->GetClientRect(&rectPicture); DELTAX = 2; SIZE = rectPicture.Width() / DELTAX; }
void GraphControl::addItem(COLORREF color, double valMin, double valMax) { CPen* pen = new CPen(PS_SOLID, 4, color); pens.push_back(pen); this->valMin.push_back(valMin); this->valMax.push_back(valMax); this->last.push_back(-1); }
void GraphControl::clear() { oldPen = pDC->SelectObject(&bgPen); oldBrush = pDC->SelectObject(&bgBrush); pDC->Rectangle(rectPicture); for (size_t i = 0; i < last.size(); i++)last[i] = 0; pDC->SelectObject(&gridPen); for (size_t i = 10; i < rectPicture.Height(); i += 20) { pDC->MoveTo(0, i); pDC->LineTo(rectPicture.Width(), i); } pDC->SelectObject(oldPen); pDC->SelectObject(oldBrush); }
GraphControl::~GraphControl() { for (size_t i = 0; i < pens.size(); i++) { pens[i]->DeleteObject(); delete pens[i]; } bgPen.DeleteObject(); gridPen.DeleteObject(); bgBrush.DeleteObject(); }
int GraphControl::doubleToCoord(double val, double min, double max) { return rectPicture.Height() - ((double)rectPicture.Height() * (val - min) / (max - min)); }
void GraphControl::updateGraph(int argLength, ...) { va_list args = NULL; va_start(args, argLength);
int w1 = rectPicture.right - rectPicture.left - DELTAX; int w2 = rectPicture.Width() - DELTAX; pDC->BitBlt(0, 0, rectPicture.Width(), rectPicture.Height(), pDC, DELTAX, 0, SRCCOPY);
oldPen = pDC->SelectObject(&bgPen); oldBrush = pDC->SelectObject(&bgBrush); pDC->Rectangle(rectPicture.right - DELTAX + 1, 0, rectPicture.right, rectPicture.bottom + 1); pDC->SelectObject(&gridPen); for (size_t i = 10; i < rectPicture.Height(); i += 20) { pDC->MoveTo(rectPicture.right - DELTAX-1, i); pDC->LineTo(rectPicture.Width(), i); } pDC->SelectObject(oldPen); pDC->SelectObject(oldBrush);
for (size_t i = 0; i < pens.size(); i++) { oldPen = pDC->SelectObject(pens[i]); double val = va_arg(args, double); pDC->MoveTo(rectPicture.right, doubleToCoord(val, valMin[i], valMax[i])); if (last[i] != -1) { int ypos = doubleToCoord(last[i], valMin[i], valMax[i]); pDC->LineTo(rectPicture.right - DELTAX, ypos); } last[i] = val; pDC->SelectObject(oldPen); } }
int GraphControl::getSize() { return this->SIZE; }
|