表盘类ClockScale设计
头文件设计:
#if !defined(AFX_CLOCKSCALE_H__2AAE7F83_2E6F_4E93_84EE_98026E963141__INCLUDED_)
#define AFX_CLOCKSCALE_H__2AAE7F83_2E6F_4E93_84EE_98026E963141__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ClockScale.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CClockScale frame with splitter
#ifndef __AFXEXT_H__
#include
#define PI 3.1415926;
#endif
class CClockScale
{
protected:
// protected constructor used by dynamic creation
// Attributes
protected:
public:
CClockScale();
COLORREF m_color;
UINT m_stytle;
MAJORTYPE m_style;
CPoint m_ptMiddle;
UINT m_nPointWidth;
int m_logColor;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CClockScale)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CClockScale();
void CClockScale::SetScaleStyle(MAJORTYPE type);
void CClockScale::SetScaleColor(COLORREF colorref);
void CClockScale::SetLogColor(int color);
MAJORTYPE CClockScale::GetScaleStyle();
COLORREF CClockScale::GetScaleColor();
int CClockScale::GetLogColor();
void CClockScale::DrawScale(CDC *PdC,CPoint &ptMiddle);
CPoint CClockScale::ComputerFacePoint(UINT min,int nFaceLength);
void CClockScale::DrawFacePoint(CDC *pDC,const CPoint &ptFace,BOOL bMajor);
void CClockScale::DrawMajor(CDC *pDC,MAJORTYPE type,CRect rectPoint);
// Generated message map functions
//{{AFX_MSG(CClockScale)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CLOCKSCALE_H__2AAE7F83_2E6F_4E93_84EE_98026E963141__INCLUDED_)
.cpp文件设计
// ClockScale.cpp : implementation file
//
#include "stdafx.h"
#include "DeskClock.h"
#include "ClockScale.h"
#include
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClockScale
CClockScale::CClockScale()
{
m_color=RGB(0,0,255);
m_style=TYPE_RECT;
m_logColor=1;
}
CClockScale::~CClockScale()
{
}
void CClockScale::SetScaleStyle(MAJORTYPE type)
{
m_style=type;
}
void CClockScale::SetScaleColor(COLORREF colorref)
{
m_color=colorref;
}
void CClockScale::SetLogColor(int color)
{
m_logColor=color;
}
MAJORTYPE CClockScale::GetScaleStyle()
{
return m_style;
}
COLORREF CClockScale::GetScaleColor()
{
return m_color;
}
int CClockScale::GetLogColor()
{
return m_logColor;
}
void CClockScale::DrawScale(CDC *pDc,CPoint &ptMiddle)
{
m_ptMiddle.x=ptMiddle.x;
m_ptMiddle.y=ptMiddle.y;
if(m_ptMiddle.y<0)
{
m_ptMiddle.y=0;
}
UINT nRidius=min(m_ptMiddle.x,m_ptMiddle.y);
m_nPointWidth=(int)nRidius/20;
//绘制LOGo
CBitmap maskbmp,logbmp;
maskbmp.LoadBitmap(IDB_LOGMASK);
logbmp.LoadBitmap(IDB_LOG);
CDC MaskDC,memDC;
MaskDC.CreateCompatibleDC(pDc);
MaskDC.SelectObject(&maskbmp);
memDC.CreateCompatibleDC(pDc);
memDC.SelectObject(&logbmp);
if(m_nPointWidth>8)
{
if(m_logColor==0)
{
pDc->BitBlt(m_ptMiddle.x-25,ptMiddle.y-nRidius*0.7,
96,96,&MaskDC,0,0,SRCAND);
pDc->BitBlt(m_ptMiddle.x-25,ptMiddle.y-nRidius*0.7,
96,96,&MaskDC,0,0,MERGEPAINT);
}
else if(m_logColor==1)
{
pDc->BitBlt(m_ptMiddle.x-25,ptMiddle.y-nRidius*0.7,
96,96,&MaskDC,0,0,MERGEPAINT);
pDc->BitBlt(m_ptMiddle.x-25,ptMiddle.y-nRidius*0.7,
96,96,&MaskDC,0,0,SRCAND);
}
}
if(m_nPointWidth<2)
{
m_nPointWidth=2;
}
CPoint ptFace;
CBrush brPoint(m_color);
CBrush *pbrOld=pDc->SelectObject(&brPoint);
int nFaceLength=MulDiv(nRidius,9,10);
for(int nMin=0;nMin<60;nMin++)
{
BOOL bHour=FALSE;
ptFace=ComputerFacePoint(nMin,nFaceLength);
if(nMin%5==0)
{
bHour=true;
}
DrawFacePoint(pDc,ptFace,bHour);
}
pDc->SelectObject(pbrOld);
return;
}
CPoint CClockScale::ComputerFacePoint(UINT min,int nFaceLength)
{
CPoint ptCalc;
double fDegrees=180+((15+min)%60)*6;
double fAngle=fDegrees/180;
ptCalc.x=m_ptMiddle.x+(int)(cos(fAngle*3.1415926)*nFaceLength);
ptCalc.y=m_ptMiddle.y+(int)(sin(fAngle*3.1415926)*nFaceLength);
return(ptCalc);
}
void CClockScale::DrawFacePoint(CDC *pDC,const CPoint &ptFace,BOOL bMajor)
{
CRect rectPoint(ptFace.x,ptFace.y,ptFace.x,ptFace.y);
if(bMajor)
{
rectPoint.InflateRect((m_nPointWidth/2)+2,(m_nPointWidth/2)+2);
DrawMajor(pDC,m_style,rectPoint);
}
else
{
if(m_nPointWidth>2)
{
rectPoint.InflateRect(1,1);
pDC->Draw3dRect(&rectPoint,GetSysColor(COLOR_BTNHIGHLIGHT),GetSysColor(COLOR_BTNSHADOW));
}
}
return;
}
void CClockScale::DrawMajor(CDC *pDC,MAJORTYPE type,CRect rectPoint)
{
CPen *oldPen;
CPoint ptRhombus[4];
CPen penBorder(PS_SOLID,1,RGB(255,255,255));
switch(type)
{
case TYPE_ELLIPSE:
pDC->Ellipse(rectPoint);
break;
case TYPE_RECT:
pDC->Rectangle(&rectPoint);
pDC->Draw3dRect(&rectPoint,GetSysColor(COLOR_BTNHIGHLIGHT),GetSysColor(COLOR_BTNSHADOW));
break;
case TYPE_RHOMBUS:
oldPen=pDC->SelectObject(&penBorder);
ptRhombus[1].x=rectPoint.left+rectPoint.Width()/2;
ptRhombus[1].y=rectPoint.top;
ptRhombus[0].x=rectPoint.left;
ptRhombus[0].y=rectPoint.top+rectPoint.Height()/2;
ptRhombus[3].x=rectPoint.left+rectPoint.Width()/2;
ptRhombus[3].y=rectPoint.bottom;
ptRhombus[2].x=rectPoint.right;
ptRhombus[2].y=rectPoint.top+rectPoint.Height()/2;
pDC->Polygon(ptRhombus,4);
pDC->SelectObject(oldPen);
break;
case TYPE_TRIANGLE:
oldPen=pDC->SelectObject(&penBorder);
ptRhombus[0].x=rectPoint.left;
ptRhombus[0].y=rectPoint.top+rectPoint.Height()/2;
ptRhombus[1].x=rectPoint.left+rectPoint.Width()/2;
ptRhombus[1].y=rectPoint.top;
ptRhombus[2].x=rectPoint.right;
ptRhombus[2].y=rectPoint.top+rectPoint.Height()/2;
pDC->Polygon(ptRhombus,3);
pDC->SelectObject(oldPen);
break;
}
}
/////////////////////////////////////////////////////////////////////////////
// CClockScale message handlers
主窗口OnPaint()调用实现效果
CRect rectClient;
CPoint m_pMiddle;
GetClientRect(&rectClient);
CPaintDC dc(this);
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CBitmap bmp;
//创建于设备兼容的位图,大小同客户区一样
bmp.CreateCompatibleBitmap(&dc,rectClient.Width(),rectClient.Height());
//将位图选入内存设备
memDC.SelectObject(&bmp);
//填充背景色
CBrush bkBrush(m_colorBk);
memDC.FillRect(&rectClient,&bkBrush);
//时钟参数
m_pMiddle.x=rectClient.Width()/2;
m_pMiddle.y=rectClient.Height()/2-15;
m_nRidius=min(m_pMiddle.x,m_pMiddle.y);
m_nPointWidth=(int)m_nRidius/20;
//绘制表盘
m_ClockScale.DrawScale(&memDC,m_pMiddle);
//
//m_oTime=CTime::GetCurrentTime();
//int m_nMinute=m_oTime.GetMinute();
//m_ClockHand.DrawHand(&memDC,m_nMinute,MINUTE_HAND,m_pMiddle,m_oTime);
dc.StretchBlt(0,0,rectClient.Width(),rectClient.Height(),&memDC,0,0,rectClient.Width(),
rectClient.Height(),SRCCOPY);