45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:如何自制小巧的时钟?

如何自制小巧的时钟?

2016-09-09 09:04:10 来源:www.45fan.com 【

如何自制小巧的时钟?

这是本人正式参加工作以后(三个月前)接到的第一个编程任务,只是用来测试我的编程能力,完整的是编写一个月历配上时钟,这边只是提供了时钟的程序,虽然有些简陋但是已经有时钟的样子!其实程序本身也没有什么技术含量,不过对于我的意义挺大,故写入我的博客,与大家分享一下!我当时大概用了一个小时吧,编出来挺有成就感的,程序员最大的乐趣也就这么多了!
时钟的关键其实不足获得时间,而在于怎么画那个钟面!还是能小小的考察一下逻辑能力的!


以下为主要代码:

void CTimeDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default

CTime time;
time=time.GetCurrentTime();
m_iHour=time.GetHour();
m_iMinute=time.GetMinute();
m_iSecond=time.GetSecond();

InvalidateRect(m_rtClockRect,FALSE);
CDialog::OnTimer(nIDEvent);
}


void CTimeDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this);


m_rtClockRect.top=150;
m_rtClockRect.left=430;
m_rtClockRect.right=530;
m_rtClockRect.bottom=250;
POINT ptCenter,ptSecond,ptMunite,ptHour;

ptCenter.x=m_rtClockRect.left+(m_rtClockRect.right-m_rtClockRect.left)/2;
ptCenter.y=m_rtClockRect.top+(m_rtClockRect.bottom-m_rtClockRect.top)/2;

dc.Ellipse(&m_rtClockRect);

ptSecond.x=ptCenter.x+50*sin(m_iSecond*pi/30);
ptSecond.y=ptCenter.y-50*cos(m_iSecond*pi/30);

ptMunite.x=(ptCenter.x+50*sin(m_iMinute*pi/30)*0.8);//*0.8;
ptMunite.y=(ptCenter.y-50*cos(m_iMinute*pi/30)*0.8);//*0.8;


ptHour.x=(ptCenter.x+50*sin(m_iHour*pi/6+m_iMinute*pi/360)*0.6);//*0.6;
ptHour.y=(ptCenter.y-50*cos(m_iHour*pi/6+m_iMinute*pi/360)*0.6);//*0.6;


CPen pen,*oldpen;
pen.CreatePen(PS_SOLID,2,RGB(100,100,100));
oldpen=dc.SelectObject(&pen);
dc.MoveTo(ptCenter);
dc.LineTo(ptSecond);
dc.MoveTo(ptCenter);
dc.LineTo(ptMunite);
dc.MoveTo(ptCenter);
dc.LineTo(ptHour);
dc.SelectObject(oldpen);


POINT ptPos1[12],ptPos2[12],ptPos3[12];
for(int iPos=0;iPos<12;iPos++)
{
ptPos1[iPos].x=ptCenter.x+50*sin(iPos*pi/6);
ptPos1[iPos].y=ptCenter.y-50*cos(iPos*pi/6);

ptPos2[iPos].x=ptCenter.x+50*sin(iPos*pi/6)*0.87;
ptPos2[iPos].y=ptCenter.y-50*cos(iPos*pi/6)*0.87;

ptPos3[iPos].x=ptCenter.x+50*sin(iPos*pi/6)*1.2-8;
ptPos3[iPos].y=ptCenter.y-50*cos(iPos*pi/6)*1.2-8;


dc.MoveTo(ptPos2[iPos]);
dc.LineTo(ptPos1[iPos]);


CString str;
if(iPos==0)
{
str.Format("%d",12);
}
else
{
str.Format("%d",iPos);
}
dc.SetBkMode(TRANSPARENT);
dc.TextOut(ptPos3[iPos].x,ptPos3[iPos].y,str);
}


POINT ptPos4[60],ptPos5[60];
for(iPos=0;iPos<60;iPos++)
{
ptPos4[iPos].x=ptCenter.x+50*sin(iPos*pi/30);
ptPos4[iPos].y=ptCenter.y-50*cos(iPos*pi/30);

ptPos5[iPos].x=ptCenter.x+50*sin(iPos*pi/30)*0.93;
ptPos5[iPos].y=ptCenter.y-50*cos(iPos*pi/30)*0.93;


dc.MoveTo(ptPos4[iPos]);
dc.LineTo(ptPos5[iPos]);

}

CDialog::OnPaint();
ShowDate();
}
}
 

本文地址:http://www.45fan.com/dnjc/73905.html
Tags: 一个 时钟 小巧
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部