📄 myroundsliderctrl2.cpp
字号:
strValue.Format("%1.2f",(m_nDivNum - i)*m_dPlusValue);
pointerTextSize = m_memDC.GetTextExtent(strValue);
if(i==0){//右边
pointerTextPoint = CPoint(m_centerPoint.x+(int)((m_nSmallRadius+6)*cos((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)),
m_centerPoint.y-(int)((m_nSmallRadius+6)*sin((m_nMinAngle+m_nAngleDiv*i)*pi/180.0))- pointerTextSize.cy/2);
//strValue.Format("%.2f",(m_nDivNum - i)*m_dPlusValue);
//输出字
m_memDC.TextOut(pointerTextPoint.x,pointerTextPoint.y,strValue);
}
else{if(i==m_nDivNum){//左边
pointerTextPoint = CPoint(m_centerPoint.x+(int)((m_nSmallRadius+6)*cos((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)) - pointerTextSize.cx,
m_centerPoint.y-(int)((m_nSmallRadius+6)*sin((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)) - pointerTextSize.cy/2);
//输出字
m_memDC.TextOut(pointerTextPoint.x,pointerTextPoint.y,strValue);
}
else{if(i==m_nDivNum/2){//正上边
pointerTextPoint = CPoint(m_centerPoint.x+(int)((m_nSmallRadius+6)*cos((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)) - pointerTextSize.cx/2,
m_centerPoint.y-(int)((m_nSmallRadius+6)*sin((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)) - pointerTextSize.cy);
//输出字
m_memDC.TextOut(pointerTextPoint.x,pointerTextPoint.y,strValue);
}
else{}
}
}
// strValue.Format("%.2f",(m_nDivNum - i)*m_dPlusValue);
// m_memDC.TextOut(pointerTextPoint.x,pointerTextPoint.y,strValue);
}
}
else{
for(int i=0; i<=m_nDivNum; i++){
if(i%10 == 0){//长刻度
m_memDC.MoveTo(m_centerPoint.x+(int)((m_nSmallRadius)*cos((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)),
m_centerPoint.y-(int)((m_nSmallRadius)*sin((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)));
m_memDC.LineTo(m_centerPoint.x+(int)((m_nSmallRadius+8)*cos((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)),
m_centerPoint.y-(int)((m_nSmallRadius+8)*sin((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)));
//处理长刻度指示值字***
//
strValue.Format("%.2f",(m_nDivNum - i)*m_dPlusValue);
pointerTextSize = m_memDC.GetTextExtent(strValue);
if(i<(m_nDivNum/2)){//右边
pointerTextPoint = CPoint(m_centerPoint.x+(int)((m_nSmallRadius+8)*cos((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)),
m_centerPoint.y-(int)((m_nSmallRadius+8)*sin((m_nMinAngle+m_nAngleDiv*i)*pi/180.0))- pointerTextSize.cy/2);
}
else{if(i>(m_nDivNum/2)){//左边
pointerTextPoint = CPoint(m_centerPoint.x+(int)((m_nSmallRadius+8)*cos((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)) - pointerTextSize.cx,
m_centerPoint.y-(int)((m_nSmallRadius+8)*sin((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)) - pointerTextSize.cy/2);
}
else{//正上边
pointerTextPoint = CPoint(m_centerPoint.x+(int)((m_nSmallRadius+8)*cos((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)) - pointerTextSize.cx/2,
m_centerPoint.y-(int)((m_nSmallRadius+8)*sin((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)) - pointerTextSize.cy);
}
}
m_memDC.TextOut(pointerTextPoint.x,pointerTextPoint.y,strValue);
}
else{//画小刻度
m_memDC.MoveTo(m_centerPoint.x+(int)(m_nSmallRadius*cos((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)),
m_centerPoint.y-(int)(m_nSmallRadius*sin((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)));
m_memDC.LineTo(m_centerPoint.x+(int)((m_nSmallRadius+4)*cos((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)),
m_centerPoint.y-(int)((m_nSmallRadius+4)*sin((m_nMinAngle+m_nAngleDiv*i)*pi/180.0)));
}
}
}
//善后处理 选出字体和笔,删除字体以释放内存
m_memDC.SelectObject(pOldFont);
font.DeleteObject();
m_memDC.SelectObject(pOldPen);
//处理指针****
//准备笔刷
CBrush pointerBrush(m_pointerClr);
//获得位置---位置的处理放在OnLButtonDow,OnLButtonUp,OnMouseMove里
//计算指针圆心
m_pointerCenterPoint = CPoint(
m_centerPoint.x+(int)((double)(m_nSmallRadius-m_nPointerRadius*2)*cos((m_nCurrentAngle)*pi/180.0)),
m_centerPoint.y-(int)((double)(m_nSmallRadius-m_nPointerRadius*2)*sin((m_nCurrentAngle)*pi/180.0))
);
///*//填充指针颜色
CRgn pointerRgn;
pointerRgn.CreateEllipticRgn(
m_pointerCenterPoint.x - m_nPointerRadius,
m_pointerCenterPoint.y - m_nPointerRadius,
m_pointerCenterPoint.x + m_nPointerRadius+2,
m_pointerCenterPoint.y + m_nPointerRadius+2
);
m_memDC.FillRgn(&pointerRgn,&pointerBrush);
//*/
//画指针边框(凹陷状)
DrawCircle(&m_memDC, m_pointerCenterPoint, m_nPointerRadius,
::GetSysColor(COLOR_3DDKSHADOW), ::GetSysColor(COLOR_3DHIGHLIGHT));
// DrawCircle(&dc,currentCenterPoint, m_nPointerRadius,
// ::GetSysColor(COLOR_3DSHADOW),::GetSysColor(COLOR_3DLIGHT));
//输出当前值
strValue.Format("%.2f",m_dCurrentValue);
CSize textSize = m_memDC.GetTextExtent(strValue);
CPoint textPoint(m_centerPoint.x - textSize.cx/2,
m_centerPoint.y - textSize.cy/2);
m_memDC.SetBkMode(OPAQUE);
m_memDC.SetBkColor(RGB(0,0,0));
m_memDC.SetTextColor(m_textClr);
m_memDC.TextOut(textPoint.x, textPoint.y, strValue);
//贴出图
dc.BitBlt(m_clientRect.left, m_clientRect.top,
m_clientRect.Width(), m_clientRect.Height(),
&m_memDC, m_clientRect.left, m_clientRect.top,
SRCCOPY );
//
//try
// Do not call CSliderCtrl::OnPaint() for painting messages
}
void CMyRoundSliderCtrl2::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bLButtonDown = TRUE;
SetCapture();
//
CSliderCtrl::OnLButtonDown(nFlags, point);
}
/////*******处理单击右键做递增**************
void CMyRoundSliderCtrl2::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect smallRect(m_centerPoint.x-m_nSmallRadius,
m_centerPoint.y-m_nSmallRadius,
m_centerPoint.x+m_nSmallRadius,
m_centerPoint.y+m_nSmallRadius);
m_rgn.DeleteObject();
m_rgn.CreateEllipticRgnIndirect(&smallRect);
if(m_rgn.PtInRegion(point)){
//因为往右走,角度增大,实际值却减少
if((m_nCurrentAngle > m_nMinAngle)&&
(m_dCurrentValue < m_dMaxValue))
{
m_nCurrentAngle -= m_nAngleDiv;
m_dCurrentValue += m_dPlusValue;
//迫使重画按钮(发出WM_PAINT)
RedrawWindow();
//
HWND pHwnd;
pHwnd = GetParent()->GetSafeHwnd();
::PostMessage(pHwnd,WM_CHANGEVALUE,GetDlgCtrlID(),m_dCurrentValue);
}
// m_dCurrentValue = (double)(m_nCurrentAngle/(m_nMaxAngle - m_nMinAngle))*(m_dMaxValue - m_dMinValue);
}
//
CSliderCtrl::OnRButtonDown(nFlags, point);
}
//////****消除屏幕闪烁(就是使其不画屏前不清屏)***********
BOOL CMyRoundSliderCtrl2::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
// return CSliderCtrl::OnEraseBkgnd(pDC);//屏蔽掉
return TRUE;//必定要这样用,否则屏幕闪烁厉害
}
///******处理鼠标带动指针动起来*********
void CMyRoundSliderCtrl2::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bLButtonDown)//仅当左键一直按下才需要处理这个函数
{
m_bMouseMove = TRUE;
HRGN smallRgn;
HRGN largeRgn;
int nRangeAngle;
nRangeAngle = 80;
//ScreenToClient(&point);
CClientDC dc(this);
//try means3
CPoint largePoint(//角度小之圆狐点
m_centerPoint.x+(int)((double)(m_nSmallRadius-m_nPointerRadius*2)*cos((m_nCurrentAngle-nRangeAngle)*pi/180.0)),
m_centerPoint.y-(int)((double)(m_nSmallRadius-m_nPointerRadius*2)*sin((m_nCurrentAngle-nRangeAngle)*pi/180.0))
);
CPoint smallPoint(//角度大之圆狐点
m_centerPoint.x+(int)((double)(m_nSmallRadius-m_nPointerRadius*2)*cos((m_nCurrentAngle+nRangeAngle)*pi/180.0)),
m_centerPoint.y-(int)((double)(m_nSmallRadius-m_nPointerRadius*2)*sin((m_nCurrentAngle+nRangeAngle)*pi/180.0))
);
CPoint mediumPoint(//弦之中点
(largePoint.x + smallPoint.x)/2,
(largePoint.y + smallPoint.y)/2);
//开始记录窗体轮廓路径**largeRgn
dc.BeginPath();
dc.MoveTo(largePoint);
dc.LineTo(mediumPoint);
//dc.MoveTo(mediumPoint);//不能有MOVETO
dc.LineTo(m_pointerCenterPoint);
//dc.MoveTo(m_pointerCenterPoint);
dc.AngleArc(m_centerPoint.x,m_centerPoint.y,
(m_nSmallRadius-m_nPointerRadius*2),
m_nCurrentAngle,-nRangeAngle
);
//dc.LineTo(largePoint);
//结束记录窗体轮廓路径
dc.EndPath();
//把所记录的路径转化为窗体轮廓句柄
largeRgn = ::PathToRegion(dc);
//开始记录窗体轮廓路径**smallRgn--60度弦
dc.BeginPath();
dc.MoveTo(smallPoint);
dc.LineTo(mediumPoint);
//dc.MoveTo(mediumPoint);//不能有MOVETO
dc.LineTo(m_pointerCenterPoint);
//dc.MoveTo(m_pointerCenterPoint);
dc.AngleArc(m_centerPoint.x,m_centerPoint.y,
(m_nSmallRadius-m_nPointerRadius*2),
m_nCurrentAngle,nRangeAngle
);
//dc.LineTo(largePoint);
//结束记录窗体轮廓路径
dc.EndPath();
//把所记录的路径转化为窗体轮廓句柄
smallRgn = ::PathToRegion(dc);
//判断****
//顺时针(即向右转)
if((CRgn::FromHandle(largeRgn))->PtInRegion(point))
{
//因为往右走,角度增大,实际值却减少
if((m_nCurrentAngle > m_nMinAngle)&&
(m_dCurrentValue < m_dMaxValue))
{
m_nCurrentAngle -= m_nAngleDiv;
m_dCurrentValue += m_dPlusValue;
RedrawWindow();//迫使重画按钮(发出WM_PAINT)
//
HWND pHwnd;
pHwnd = GetParent()->GetSafeHwnd();
::PostMessage(pHwnd,WM_CHANGEVALUE,GetDlgCtrlID(),m_dCurrentValue);
}
}
//逆时针(即向右转)
if((CRgn::FromHandle(smallRgn))->PtInRegion(point))
{
//因为往左走,角度减少,实际值却增大
if((m_nCurrentAngle < m_nMaxAngle) && (m_dCurrentValue > m_dMinValue))
{
m_nCurrentAngle += m_nAngleDiv;
m_dCurrentValue -= m_dPlusValue;
RedrawWindow();//迫使重画按钮(发出WM_PAINT)
//
HWND pHwnd;
pHwnd = GetParent()->GetSafeHwnd();
::PostMessage(pHwnd,WM_CHANGEVALUE,GetDlgCtrlID(),m_dCurrentValue);
}
}
}
//
// CSliderCtrl::OnMouseMove(nFlags, point);
}
/////*******处理单击左键做递减**************
void CMyRoundSliderCtrl2::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//
ReleaseCapture();
//
m_bLButtonDown = FALSE;
//
if(!m_bMouseMove){
CRect smallRect(m_centerPoint.x-m_nSmallRadius,
m_centerPoint.y-m_nSmallRadius,
m_centerPoint.x+m_nSmallRadius,
m_centerPoint.y+m_nSmallRadius);
m_rgn.DeleteObject();
m_rgn.CreateEllipticRgnIndirect(&smallRect);
if(m_rgn.PtInRegion(point)){
//因为往左走,角度减少,实际值却增大
if((m_nCurrentAngle < m_nMaxAngle) && (m_dCurrentValue > m_dMinValue)){
m_nCurrentAngle += m_nAngleDiv;
m_dCurrentValue -= m_dPlusValue;
//迫使重画按钮(发出WM_PAINT)
RedrawWindow();
//
HWND pHwnd;
pHwnd = GetParent()->GetSafeHwnd();
::PostMessage(pHwnd,WM_CHANGEVALUE,GetDlgCtrlID(),m_dCurrentValue);
}
}
}
//
m_bMouseMove = FALSE;
//
CSliderCtrl::OnLButtonUp(nFlags, point);
}
//返回当前的数据值
double CMyRoundSliderCtrl2::GetCurrentValue()
{
return m_dCurrentValue;
}
void CMyRoundSliderCtrl2::SetCurrentValue(double dCurrentValue)
{
m_dCurrentValue = dCurrentValue;
m_nCurrentAngle = m_nMaxAngle -
(m_dCurrentValue/(m_dMaxValue-m_dMinValue))*
(m_nMaxAngle-m_nMinAngle);
RedrawWindow();//迫使重画按钮(发出WM_PAINT)
}
void CMyRoundSliderCtrl2::SetMaxValue(double dMaxValue)
{
m_dMaxValue = dMaxValue;
//初始化m_dCurrentValue,m_nDivNum,m_nAngleDiv,m_nCurrentAngle
Init();
}
void CMyRoundSliderCtrl2::SetMinValue(double dMinValue)
{
m_dMinValue = dMinValue;
//初始化m_dCurrentValue,m_nDivNum,m_nAngleDiv,m_nCurrentAngle
Init();
}
void CMyRoundSliderCtrl2::SetMaxAngle(int nMaxAngle)
{
m_nMaxAngle = nMaxAngle;
//初始化m_dCurrentValue,m_nDivNum,m_nAngleDiv,m_nCurrentAngle
Init();
}
void CMyRoundSliderCtrl2::SetMinAngle(int nMinAngle)
{
m_nMinAngle = nMinAngle;
//初始化m_dCurrentValue,m_nDivNum,m_nAngleDiv,m_nCurrentAngle
Init();
}
void CMyRoundSliderCtrl2::SetPlusValue(double dPlusValue)
{
m_dPlusValue = dPlusValue;
//初始化m_dCurrentValue,m_nDivNum,m_nAngleDiv,m_nCurrentAngle
Init();
}
void CMyRoundSliderCtrl2::SetScaleTextColor(COLORREF ScaleTextClr)
{
m_scaleTextClr = ScaleTextClr;
}
void CMyRoundSliderCtrl2::SetTextColor(COLORREF textClr)
{
m_textClr = textClr;
}
void CMyRoundSliderCtrl2::SetBkClr(COLORREF bkClr)
{
m_bkClr = bkClr;
}
void CMyRoundSliderCtrl2::SetScaleColor(COLORREF scaleClr)
{
m_scaleClr = scaleClr;
}
void CMyRoundSliderCtrl2::SetPointerColor(COLORREF pointerClr)
{
m_pointerClr = pointerClr;
}
void CMyRoundSliderCtrl2::OnSize(UINT nType, int cx, int cy)
{
CSliderCtrl::OnSize(nType, cx, cy);
GetClientRect(&m_clientRect);
//
ChangeSize();
// TODO: Add your message handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -