📄 realtime.cpp
字号:
{
m_clientRect = rt;
SetPixelRect(rt);//设置m_drawRect
m_bM = (m_drawRect.bottom - m_drawRect.top) / 6;
m_tM = (m_drawRect.bottom - m_drawRect.top) / 10;
m_lM = m_rM = (m_drawRect.right - m_drawRect.left) / 8;
m_plotRect.left = m_drawRect.left + m_lM;
m_plotRect.right= m_drawRect.right - m_rM;
m_plotRect.top = m_drawRect.top + m_tM;
m_plotRect.bottom = m_drawRect.bottom - m_bM;
m_scale.dx = (m_scale.xmax- m_scale.xmin) / m_plotRect.Width();
m_scale.dy = (m_scale.ymax- m_scale.ymin) / m_plotRect.Height();
//设置关于辅助图整个区域的矩形
// m_assistRect.SetRect(0, 0,
// m_plotRect.Width() * 2 + m_lM + m_rM, m_drawRect.Height());
m_assistRect.SetRect(0, 0,
m_plotRect.Width() * 2 + m_drawRect.Width(), m_drawRect.Height());
//设置辅助图中关于坐标平面的前半部矩形
// m_assistRect1.SetRect(m_lM, m_tM,
// m_plotRect.Width() + m_lM, m_drawRect.Height() - m_bM);
m_assistRect1.SetRect(m_drawRect.Width(), m_tM,
m_plotRect.Width() + m_drawRect.Width(), m_drawRect.Height() - m_bM);
//设置辅助图中关于坐标平面的后半部矩形
// m_assistRect2.SetRect(m_plotRect.Width() + m_lM, m_tM,
// m_plotRect.Width() * 2 + m_lM, m_drawRect.Height() - m_bM);
m_assistRect2.SetRect(m_plotRect.Width() + m_drawRect.Width(), m_tM,
m_plotRect.Width() * 2 + m_drawRect.Width(), m_drawRect.Height() - m_bM);
//设置辅助图中坐标平面左侧区域(含y轴及刻度值、y轴标题)的矩形
m_assistLRect.SetRect(0, 0, m_lM, m_drawRect.Height());
//设置辅助图中坐标平面右侧区域(含右侧y轴及刻度值、曲线说明信息)的矩形
// m_assistRRect.SetRect(m_plotRect.Width() * 2 + m_lM, 0,
// m_plotRect.Width() * 2 + m_lM + m_rM, m_drawRect.Height());
m_assistRRect.SetRect(m_plotRect.Width() + m_lM, 0, m_drawRect.Width(), m_drawRect.Height());
//设置辅助图中坐标平面上方区域(含曲线图标题)的矩形
m_assistTRect.SetRect(m_lM, 0, m_plotRect.Width() + m_lM, m_tM);
//设置辅助图中坐标平面下方区域(含X轴刻度、刻度值、标题、游标时间、总采样时间)的矩形
m_assistBRect.SetRect(m_lM, m_drawRect.Height() - m_bM, m_plotRect.Width() + m_lM, m_drawRect.Height());
//设置字体高度参考值
m_nSize = m_plotRect.Width() > m_plotRect.Height() ? m_plotRect.Height() : m_plotRect.Width();
}
CRect CRealTime::XScrollBarSize()
{
CRect rect;
rect = m_plotRect;
int nXHigh, nXWidth;
nXHigh = (m_tM / 2 > 15) ? 15 : m_tM / 2;
rect.top -= nXHigh;
rect.bottom = rect.top + nXHigh - 1;
return rect;
}
CRect CRealTime::YScrollBarSize()
{
CRect rect;
rect = m_plotRect;
int nYHigh;
nYHigh = (m_lM / 3 > 15) ? 15 : m_lM / 3;
rect.right += nYHigh;
rect.left = rect.right - nYHigh + 1;
return rect;
}
bool CRealTime::DoPrepare(CDC *pDC, CRect& rt)
{
RecalcRects(rt);
if(m_bmp.GetSafeHandle() != NULL)
m_bmp.DeleteObject();
if(m_dc.GetSafeHdc() != NULL)
m_dc.DeleteDC();
m_dc.CreateCompatibleDC(pDC);
m_bmp.CreateCompatibleBitmap(pDC, m_assistRect.Width(), m_assistRect.Height());
m_dc.SelectObject(&m_bmp);
DrawBkGround(&m_dc, m_assistRect);
m_redrawFlag = true;
if(m_bSetingFailed)
m_bPrepared = false;
else
m_bPrepared = true;
return m_bPrepared;
}
void CRealTime::PrintString(int x, int y, int theta, const char* fmt)
{
WORD Height, Width;
UINT PreSet;
double thta;
PreSet = m_dc.SetTextAlign(TA_LEFT|TA_TOP);
SIZE size;
::GetTextExtentPoint32(m_dc.GetSafeHdc(), fmt, lstrlen(fmt), &size);
Height = (WORD)size.cy;
Width = (WORD)size.cx;
thta = PiV*theta/ConstV;
if(m_strAlign.HAlign == LEFT && m_strAlign.VAlign == TOP)
{
}
else if(m_strAlign.HAlign == LEFT && m_strAlign.VAlign == CENTER)
{
x = (int)(x - Height/2.*sin(thta));
y = (int)(y - Height/2.*cos(thta));
}
else if(m_strAlign.HAlign == CENTER && m_strAlign.VAlign == TOP)
{
x = (int)(x - Width/2. * cos(thta));
y = (int)(y + Width/2. * sin(thta));
}
else if(m_strAlign.HAlign == CENTER && m_strAlign.VAlign == CENTER)
{
x = (int)(x - Width/2. * cos(thta) - Height/2.*sin(thta));
y = (int)(y + Width/2. * sin(thta) - Height/2.*cos(thta));
}
else if(m_strAlign.HAlign == CENTER && m_strAlign.VAlign == BOTTOM)
{
x = (int)(x - Width/2. * cos(thta) - Height*sin(thta));
y = (int)(y + Width/2. * sin(thta) - Height*cos(thta));
}
else if(m_strAlign.HAlign == RIGHT && m_strAlign.VAlign == TOP)
{
x = (int)(x - Width * cos(thta));
y = (int)(y + Width * sin(thta));
}
else if(m_strAlign.HAlign == RIGHT && m_strAlign.VAlign == CENTER)
{
x = (int)(x - Width * cos(thta) - Height/2.*sin(thta));
y = (int)(y + Width * sin(thta) - Height/2.*cos(thta));
}
else if(m_strAlign.HAlign == RIGHT && m_strAlign.VAlign == BOTTOM)
{
x = (int)(x - Width * cos(thta) - Height*sin(thta));
y = (int)(y + Width * sin(thta) - Height*cos(thta));
}
else //if(m_strAlign.HAlign == LEFT && m_strAlign.VAlign == BOTTOM)
{
x = (int)(x - Height*sin(thta));
y = (int)(y - Height*cos(thta));
}
m_dc.TextOut(x, y, fmt);
m_dc.SetTextAlign(PreSet);
}
void CRealTime::DrawXAxisTimeTicks(int x, int y, DWORD sec)
{
DWORD ticks = sec / 1000;
CString tempStr;
if (m_nCyclesPerSec > 1 && (sec % 1000))
{
sec= sec % 1000;
tempStr.Format("%d:%d:%d:%d", ticks/3600, (ticks/60)%60, ticks%60, sec);
}
else
tempStr.Format("%d:%d:%d", ticks/3600, (ticks/60)%60, ticks%60);
PrintString(x, y, 0, tempStr);
}
void CRealTime::PrintTime()
{
int n;
CPen pen;
pen.CreatePen(PS_SOLID, 0, m_nTickColor);
CPen *pOldPen = m_dc.SelectObject(&pen);
n = m_assistBRect.bottom - 1;
SetStringAlign(LEFT, BOTTOM);
ShowTime(m_assistBRect.left, n, "Cursor time", m_nCursorTime);
SetStringAlign(RIGHT, BOTTOM);
ShowTime(m_assistBRect.right, n, "Total lapse time", m_nMaxTime);
m_dc.SelectObject(pOldPen);
}
void CRealTime::ShowTime(int x, int y, const char* Tag, DWORD cTime)
{
CString tempStr;
DWORD sec1, sec2;
sec1 = cTime / 1000; sec2 = cTime % 1000;
if(sec2)
tempStr.Format("%s (%d:%d:%d(%d/1000))", Tag, sec1 / 3600, (sec1 / 60) % 60, sec1 % 60, sec2);
else
tempStr.Format("%s (%d:%d:%d)", Tag, sec1 / 3600, (sec1 / 60) % 60, sec1 % 60);
PrintString(x, y, 0, tempStr);
}
//void CRealTime::XAxis(CDC *pDC)
void CRealTime::XAxis()
{
//非实时绘制时不滚动曲线图且不重绘视图,则不需要重绘X轴及相关信息
if( ! m_bRealTime && m_nOffset == 0 && ! m_redrawFlag)return;
DWORD sec, l_sec;
int xb, xe, yb, ye;
l_sec = (m_nXTicksInLarge * 1000 / m_nCyclesPerSec + 0.5);//每个大刻度对应的时间间隔
//重画整个X轴(1.重画坐标平面辅助图区,2.在辅助图前半部中绘制X轴)
//使用背景色重画相关的辅助图区
CRect rect = m_assistBRect;
if (m_bRealTime && m_nOffset == 0)
{
//实时绘制无滚动时只重绘cusortime、totaltime和x轴标题
rect.top = rect.bottom - m_bM * 2 / 5;
DrawBkGround(&m_dc, rect);
}
else
{
if (m_bRealTime || m_redrawFlag)
;//实时绘制有滚动或重绘视图时要重绘时间轴与相关信息
else
rect.bottom -= m_bM * 2 / 5;//其它情况下(即非实时滚动时不重绘视图)只重绘时间轴
DrawBkGround(&m_dc, rect);
//绘制X轴刻度,并标明刻度值
for(int i = 0; i < m_nXTicks + 1; i ++)
{
sec = (DWORD)(m_scale.xmin + i * 1000 / m_nCyclesPerSec + 0.5);
xb = xe = (int)((sec - m_scale.xmin) / m_scale.dx) + m_lM;
if((sec % l_sec) == 0)
{
//绘制大刻度
if(i < m_nXTicksInLarge / 2)
SetStringAlign(LEFT, BOTTOM);
else
if (i > m_nXTicks - m_nXTicksInLarge / 2)
SetStringAlign(RIGHT, BOTTOM);
else
SetStringAlign(CENTER, BOTTOM);
ye = m_assistBRect.top + m_bM / 7;
DrawXAxisTimeTicks(xb, m_assistBRect.top + m_bM * 3 / 5, sec);
}
else
ye = m_assistBRect.top + m_bM / 14;
yb = m_assistBRect.top;
if (i > 0 && i < m_nXTicks)
{
m_dc.MoveTo(xb, yb);
m_dc.LineTo(xe, ye);
}
}//for
}
//实时绘制或重绘视图时要重绘与时间轴的相关信息
if (m_bRealTime || m_redrawFlag)PrintTime();
// pDC->BitBlt(m_plotRect.left, m_plotRect.bottom, m_plotRect.Width(), m_plotRect.Height(), &m_dc,
// m_lM, m_assistBRect.top, SRCCOPY);
}
void CRealTime::YAxis()
{
if(m_bRealTime || ((! m_redrawFlag) && m_nYOffset == 0 && m_nOffset != 0))return;
DrawBkGround(&m_dc, m_assistLRect);
int xb, yb, xe, ye, yl;
int i;
CString str;
//*******************
double dTemp;
DWORD nUnits, nMin, nSpan, nTemp;
bool bFlag = false;
nUnits = m_nUnitsPerCycle * 100;
dTemp = m_scale.ymin;
if(dTemp < 0)
{
bFlag = true;
dTemp = 0 - dTemp;
}
nMin = (DWORD)((DWORD)dTemp * 100) + (DWORD)((dTemp - (int)dTemp) * 100);
yl = m_plotRect.Height() + m_tM;
xe = m_lM;
nTemp = nMin;
if(bFlag)
{
nTemp += nUnits;
}
else
{
if(nTemp >= nUnits)
nTemp -= nUnits;
else
{
nTemp = nUnits - nTemp;
bFlag = true;
}
}
for(i = 0; i < m_nYTicks + 1; i ++)
{
if(bFlag)
{
if(nTemp > nUnits)
nTemp -= nUnits;
else
{
nTemp = nUnits - nTemp;
bFlag = false;
}
}
else
nTemp += nUnits;
yb = ye = yl - (int)(i * nUnits / (m_scale.dy * 100) + 0.5);
if((nTemp / nUnits) % m_nYTicksInLarge == 0)
{
xb = m_lM - m_lM / 10;
double value = nTemp / 100 + (nTemp % 100) * 0.01;
if(bFlag)value = 0 - value;
str.Format("%0.2f", (float)value);
SetStringAlign(RIGHT, CENTER);
PrintString(xb, yb, 0, str);
}
else
xb = m_lM - m_lM / 20;
if (i > 0 && i < m_nYTicks)
{
m_dc.MoveTo(xb, yb);
m_dc.LineTo(xe, ye);
}
}
}
void CRealTime::DrawPlot()
{
m_dc.Rectangle(CRect(m_lM - 1, m_tM - 1, m_lM + m_plotRect.Width() + 1, m_tM + m_plotRect.Height() + 1));
if(m_nGridShow != NOGRID)
{
CPen pen;
pen.CreatePen(PS_DOT, 0, m_nGridColor);
CPen *pOldPen = m_dc.SelectObject(&pen);
int x, y;
DWORD sec, l_sec;
l_sec = (m_nXTicksInLarge * 1000 / m_nCyclesPerSec + 0.5);
y = m_tM + m_plotRect.Height();
for(int i = 1; i < m_nXTicks; i ++)
{
sec = (DWORD)(m_scale.xmin + i * 1000 / m_nCyclesPerSec + 0.5);
x = (int)((sec - m_scale.xmin) / m_scale.dx) + m_lM;
if(m_nGridShow == BIGGRID)
{
if((sec % l_sec) == 0)
{
m_dc.MoveTo(x, m_tM);
m_dc.LineTo(x, y);
}
}
else
{
m_dc.MoveTo(x, m_tM);
m_dc.LineTo(x, y);
}
}//for(x)
x = m_lM + m_plotRect.Width();
int yl;
yl = m_plotRect.Height() + m_tM;
double dTemp;
DWORD nUnits, nMin, nSpan, nTemp;
bool bFlag = false;
nUnits = m_nUnitsPerCycle * 100;
dTemp = m_scale.ymin;
if(dTemp < 0)
{
bFlag = true;
dTemp = 0 - dTemp;
}
nMin = (DWORD)((DWORD)dTemp * 100) + (DWORD)((dTemp - (int)dTemp) * 100);
nTemp = nMin;
for(int i = 1; i < m_nYTicks; i ++)
{
if(bFlag)
{
if(nTemp > nUnits)
nTemp -= nUnits;
else
{
nTemp = nUnits - nTemp;
bFlag = false;
}
}
else
nTemp += nUnits;
y = yl - (int)(i * nUnits / (m_scale.dy * 100) + 0.5);
if(m_nGridShow == BIGGRID)
{
if((nTemp / nUnits) % m_nYTicksInLarge == 0)
{
m_dc.MoveTo(m_lM, y);
m_dc.LineTo(x, y);
}
}
else
{
m_dc.MoveTo(m_lM, y);
m_dc.LineTo(x, y);
}
}
m_dc.SelectObject(pOldPen);
}
}
void CRealTime::DrawFrame(CDC *pDC)
{
XAxis();
YAxis();
DrawPlot();
//将相关的辅助图区内容复制到屏幕上对应区域
// pDC->BitBlt(m_drawRect.left, m_drawRect.top, m_drawRect.Width(), m_drawRect.Height(), &m_dc,
// 0, 0, SRCCOPY);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -