void CClock::SetCurrent(unsigned int h,unsigned int m)
{
m_hour = (h+11)%12 + 1;
m_min = m%60;
}
void CClock::SetCurrent()
{
time_t tt = time(0);
tm* t = localtime(&tt);
SetCurrent(t->tm_hour,t->tm_min);
}
CString CClock::ToString()const
{
CString s;
s.Format("%2d:%.2d",m_hour,m_min);
return s;
}
CClock::CClock()
{
SetCurrent();
m_func = 0;
} |
また、メッセージハンドラを以下のように修正。
void CClock::OnPaint()
{
CPaintDC dc(this);
CRect rc,rct,rr;
int i;
double d,w,h,r = 0.98, rp = 0.85;
CPoint pc,pt,pt2;
CSize sz;
CString s;
GetClientRect(&rc);
w = rc.Width() * 0.5;
h = rc.Height() * 0.5;
dc.Ellipse(&rc);
pc = rc.CenterPoint();
dc.DrawText("12",2,&rct,DT_CALCRECT);
rct.OffsetRect(rc.CenterPoint() - rct.CenterPoint());
sz = CSize(10,10);
for (i=0;i<12;++i) {
d = i*pi/6;
pt.x = long(w * sin(d));
pt.y = long(-h * cos(d));
rr = rct;
sz = CSize(int(pt.x*rp),int(pt.y*rp));
rr.OffsetRect(sz);
s.Format("%d",i?i:12);
dc.DrawText(s,&rr,DT_CENTER | DT_VCENTER);
sz = CSize(int(pt.x*r),int(pt.y*r));
dc.MoveTo(pc+sz);
dc.LineTo(pc+pt);
}
d = (m_hour + m_min/60.)*pi/6;
pt.x = long(w * sin(d));
pt.y = long(-h * cos(d));
sz = CSize(int(pt.x*.6),int(pt.y*.6));
CPen pen1(PS_SOLID,int(max(4,w/20)),RGB(0,0,0)),
pen2(PS_SOLID,int(max(2,w/50)),RGB(0,0,0)),*pOld;
pOld = dc.SelectObject(&pen1);
dc.MoveTo(pc);
dc.LineTo(pc+sz);
d = (m_min)*pi/30;
pt.x = long(w * sin(d));
pt.y = long(-h * cos(d));
sz = CSize(int(pt.x*.9),int(pt.y*.9));
dc.SelectObject(&pen2);
dc.MoveTo(pc);
dc.LineTo(pc+sz);
dc.SelectObject(pOld);
}
void CClock::OnLButtonDown(UINT nFlags, CPoint point)
{
CRect rc;
GetClientRect(&rc);
CPoint pc = rc.CenterPoint();
double r = atan2(point.x-pc.x,-(point.y-pc.y)),rh,rm;
if (r < 0) r += 2*pi;
rh = (m_hour+m_min/60.)*pi/6;
rm = m_min*pi/30;
if (fabs(r-rh) < 10*pi/180) m_moveType = 0;
else if (fabs(r-rm) < 10*pi/180) m_moveType = 1;
else return;
SetCapture();
}
void CClock::OnMouseMove(UINT nFlags, CPoint point)
{
if (GetCapture() != this) return;
CRect rc;
GetClientRect(&rc);
CPoint pc = rc.CenterPoint();
double r = atan2(point.x-pc.x,-(point.y-pc.y));
int pre = m_min;
if (r < 0) r += 2*pi;
switch (m_moveType) {
case 0:
m_hour = int(r*6/pi+11.5)%12+1;
break;
case 1:
m_min = int(r*30/pi+.5)%60;
if (pre < 10 && m_min > 50) m_hour = (m_hour+10)%12+1;
else if (pre > 50 && m_min < 10) m_hour = m_hour%12+1;
break;
}
if (m_func) (*m_func)();
Invalidate();
}
void CClock::OnLButtonUp(UINT nFlags, CPoint point)
{
if (GetCapture() != this) return;
ReleaseCapture();
}
|