📄 roundmeter.cpp
字号:
pDC->SelectObject(&pNeedleBrush);
pDC->SelectObject(&pPenThin);
rectRound.SetRect(m_ptMeterCenter.x - m_nRadiusFrame / 15,
m_ptMeterCenter.y - m_nRadiusFrame / 15,
m_ptMeterCenter.x + m_nRadiusFrame / 15,
m_ptMeterCenter.y + m_nRadiusFrame / 15);
pDC->Ellipse(rectRound);
pNeedleBrush.DeleteObject();
pPenThin.DeleteObject();
//第五圈
pNeedleBrush.CreateSolidBrush(m_FifthColor);
pPenThin.CreatePen(PS_SOLID, 1, m_FifthLineColor);
pDC->SelectObject(&pNeedleBrush);
pDC->SelectObject(&pPenThin);
rectRound.SetRect(m_ptMeterCenter.x - m_nRadiusFrame / 15 + m_nRadiusFrame / 50,
m_ptMeterCenter.y - m_nRadiusFrame / 15 + m_nRadiusFrame / 50,
m_ptMeterCenter.x + m_nRadiusFrame / 15 - m_nRadiusFrame / 50,
m_ptMeterCenter.y + m_nRadiusFrame / 15 - m_nRadiusFrame / 50);
pDC->Ellipse(rectRound);
pNeedleBrush.DeleteObject();
pPenThin.DeleteObject();
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
}
//绘制仪表实时值
void CRoundMeter::DrawValue(CDC *pDC, CRect &rect)
{
char strCurrentValue[10];
memset(strCurrentValue, 0, sizeof(strCurrentValue));
sprintf(strCurrentValue, "%.1f", m_dCurrentValue);
int nRadiusFrame = m_nRadiusFrame / 2;
CFont pUnitFont, *pOldFont;
pUnitFont.CreateFont(nRadiusFrame / 6, 0, 0, 0, 0, FALSE, FALSE, ANSI_CHARSET,
CLIP_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_PITCH, FF_DONTCARE, _T("Times New Roman"));
pOldFont = (CFont *)pDC->SelectObject(&pUnitFont);
pDC->SetBkMode(TRANSPARENT);
// if(m_dCurrentValue>m_WarnMaxValue || m_dCurrentValue<m_WarnMinValue)
// pDC->SetTextColor(m_WarnTextBackColor);
// else
// pDC->SetTextColor(m_TextBackColor);
//
// CRect rectUnits(int(m_ptMeterCenter.x - nRadiusFrame * 0.30f),
// int(m_ptMeterCenter.y + nRadiusFrame * 0.40f),
// int(m_ptMeterCenter.x + nRadiusFrame * 0.30f + 2),
// int(m_ptMeterCenter.y + nRadiusFrame * 0.60f + 2));
// pDC->DrawText(strCurrentValue, &rectUnits, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
if(m_dCurrentValue>m_WarnMaxValue || m_dCurrentValue<m_WarnMinValue)
pDC->SetTextColor(m_WarnTextColor);
else
pDC->SetTextColor(m_TextColor);
pDC->DrawText(strCurrentValue, m_nrectValue, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
pDC->SelectObject(pOldFont);
pUnitFont.DeleteObject();
}
//重新更新背景
void CRoundMeter::ReconstructControl()
{
if (m_pbitmapOldMeterPlate &&
m_bitmapMeterPlate.GetSafeHandle() &&
m_dcMeterPlate.GetSafeHdc())
{
m_dcMeterPlate.SelectObject(m_pbitmapOldMeterPlate);
m_dcMeterPlate.DeleteDC() ;
m_bitmapMeterPlate.DeleteObject();
}
Invalidate ();
}
double CRoundMeter::GetAngle(CPoint &ptCur)
{
double dAngle = 0;
double xDisc = ptCur.x - m_ptMeterCenter.x;
double yDisc = ptCur.y - m_ptMeterCenter.y;
double tmpVal = yDisc / xDisc;
if (xDisc < 0 && yDisc > 0)
{
if ((-atan(tmpVal) * 180 / PI) > 60)
{
dAngle = 0;
}
else
{
dAngle = 60 + atan(tmpVal) * 180 / PI;
}
}
else if (xDisc < 0 && yDisc <= 0)
{
dAngle = 60 + atan(tmpVal) * 180 / PI;
}
else if (xDisc > 0 && yDisc <= 0)
{
dAngle = 240 + atan(tmpVal) * 180 / PI;
}
else if (xDisc > 0 && yDisc > 0)
{
if ((-atan(tmpVal) * 180 / PI) > 60)
{
dAngle = 300;
}
else
{
dAngle = 240 + atan(tmpVal) * 180 / PI;
}
}
else
{
dAngle = 150;
}
return dAngle;
}
double CRoundMeter::GetValue(double &dAngle)
{
double dCurrentValue;
dCurrentValue = m_dMinValue + dAngle * (m_dMaxValue - m_dMinValue) / 300;
return dCurrentValue;
}
void CRoundMeter::SetCurrentValue(const double dCurrentValue)
{
if (!m_bMouseDrag) //鼠标拖动的时候不更新实时信息
{
m_dCurrentValue = dCurrentValue;
Invalidate();
}
}
COLORREF CRoundMeter::GetMeterBackColor()
{
return m_BackColor;
}
void CRoundMeter::SetMeterBackColor(const COLORREF clBackColor,const COLORREF clBackLineColor)
{
m_BackColor = clBackColor;
m_BackLineColor = clBackLineColor;
ReconstructControl();
}
int CRoundMeter::GetTicks()
{
return m_nTicks;
}
void CRoundMeter::SetTicks(const int nTicks)
{
m_nTicks = nTicks;
m_bReset=TRUE;
ReconstructControl();
}
int CRoundMeter::GetSubTicks()
{
return m_nSubTicks;
}
void CRoundMeter::SetSubTicks(const int nSubTicks)
{
m_nSubTicks = nSubTicks;
m_bReset=TRUE;
ReconstructControl();
}
void CRoundMeter::SetRange(const double dMin, const double dMax ,const double dWarnMin,const double dWarnMax)
{
m_dMinValue = dMin;
m_dMaxValue = dMax;
m_WarnMinValue = dWarnMin;
m_WarnMaxValue = dWarnMax;
ReconstructControl();
}
char * CRoundMeter::GetUnits()
{
return m_strUnits;
}
void CRoundMeter::SetUnits(const char *strUnits)
{
strcpy(m_strUnits, strUnits);
m_bReset=TRUE;
ReconstructControl();
}
//void CRoundMeter::OnLButtonUp(UINT nFlags, CPoint point)
//{
// ReleaseCapture();
//
// CStatic::OnLButtonUp(nFlags, point);
//}
//
//void CRoundMeter::OnLButtonDown(UINT nFlags, CPoint point)
//{
// if (!m_bMouseDrag)
// {
// return;
// }
//
// SetCapture();
//
// double dAngle = GetAngle(point);
// m_dCurrentValue = GetValue(dAngle);
// Invalidate();
//
// CStatic::OnLButtonDown(nFlags, point);
//}
//
//void CRoundMeter::OnMouseMove(UINT nFlags, CPoint point)
//{
// if(nFlags & MK_LBUTTON & m_bMouseDrag)
// {
// double dAngle = GetAngle(point);
// m_dCurrentValue = GetValue(dAngle);
// Invalidate();
// }
//
// CStatic::OnMouseMove(nFlags, point);
//}
BOOL CRoundMeter::OnEraseBkgnd(CDC* pDC)
{
return FALSE;
}
void CRoundMeter::SetMouseDrag(const bool bMouseDrag)
{
m_bMouseDrag = bMouseDrag;
}
bool CRoundMeter::GetMouseDrag() const
{
return m_bMouseDrag;
}
void CRoundMeter::SetNeedColor(const COLORREF clNeedColor, const COLORREF clNeedLineColor, const COLORREF clWarnNeedColor, const COLORREF clWarnNeedLineColor)
{
m_NeedleColor=clNeedColor;
m_NeedleLineColor=clNeedLineColor;
m_WarnNeedleColor=clWarnNeedColor;
m_WarnNeedleLineColor=clWarnNeedLineColor;
ReconstructControl();
}
void CRoundMeter::SetFirstColor(const COLORREF clFirstColor, const COLORREF clFirstLineColor)
{
m_FirstColor=clFirstColor;
m_FirstLineColor=clFirstLineColor;
ReconstructControl();
}
void CRoundMeter::SetSecondColor(const COLORREF clSecondColor, const COLORREF clSecondLineColor)
{
m_SecondColor=clSecondColor;
m_SecondLineColor=clSecondLineColor;
ReconstructControl();
}
void CRoundMeter::SetThirdColor(const COLORREF clThirdColor, const COLORREF clThirdLineColor)
{
m_ThirdColor=clThirdColor;
m_ThirdLineColor=clThirdLineColor;
ReconstructControl();
}
void CRoundMeter::SetFourthColor(const COLORREF clFourthColor, const COLORREF clFourthLineColor)
{
m_FourthColor=clFourthColor;
m_FourthLineColor=clFourthLineColor;
ReconstructControl();
}
void CRoundMeter::SetFifthColor(const COLORREF clFifthColor, const COLORREF clFifthLineColor)
{
m_FifthColor=clFifthColor;
m_FifthLineColor=clFifthLineColor;
ReconstructControl();
}
void CRoundMeter::SetTextColor(const COLORREF clTextColor, const COLORREF clTextBackColor, const COLORREF clWarnTextColor, const COLORREF clWarnTextBackColor)
{
m_TextColor=clTextColor;
m_TextBackColor=clTextBackColor;
m_WarnTextColor=clWarnTextColor;
m_WarnTextBackColor=clWarnTextBackColor;
ReconstructControl();
}
void CRoundMeter::SetTickColor(const COLORREF clTickColor, const COLORREF clSubTickColor)
{
m_TickColor=clTickColor;
m_SubTickColor=clSubTickColor;
ReconstructControl();
}
void CRoundMeter::DrawRgn(CDC *pDC,POINT *Rgn,int ptNum,COLORREF Fillcolor)
{
CBrush *pBrushOld,brushFill;
CPen *pOldPen, penDraw;
brushFill.CreateSolidBrush(Fillcolor);
pBrushOld = pDC->SelectObject(&brushFill);
penDraw.CreatePen(PS_SOLID, 1, Fillcolor);
pOldPen = pDC->SelectObject(&penDraw);
pDC->Polygon(Rgn, ptNum);
pDC->SelectObject(pBrushOld);
pDC->SelectObject(pOldPen);
penDraw.DeleteObject();
brushFill.DeleteObject();
}
void CRoundMeter::SetAngle(int StartAngle, int EndAngle)
{
m_nStartAngle=StartAngle;
m_nEndAngle=EndAngle;
m_bReset=TRUE;
ReconstructControl();
}
int CRoundMeter::CalVeilRgn(POINT* PtRgn,POINT PtCenter,int nRadius,int StartAngle, int EndAngle)
{
int cnt=0;
if(StartAngle==EndAngle)
return 0;
//已开始角为起始点
PtRgn[cnt].x=PtCenter.x+(int)(nRadius*cos(double(StartAngle/180.0)*PI));
PtRgn[cnt].y=PtCenter.y-(int)(nRadius*sin(double(StartAngle/180.0)*PI));
cnt++;
if(0<=StartAngle && StartAngle<90) //开始角在第一象限
{
if(StartAngle>EndAngle)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -