摘要
绝大多数的电子产品都使用了七段数码显示,如果软件也能模拟出这种效果该有多好?在本文之前,VC知识库在线杂志曾有两篇文章介绍过如何实现这种效果,有一篇的实现方法较为简单,但绘出的数字不够逼真,而另一篇实现的效果虽然逼真,但必须依赖位图资源,并且无法设置前景色和背景色等。笔者经过仔细的研究与试验,终于找到了较好的解决办法。本文将详细讲述七段数码显示的数字时钟的实现。
实现原理
我们知道,时钟的显示由时、分、秒及冒号组成,因此我们可以用以下函数来实现:DrawHour,DrawMinute,DrawSecond和Draw2Dot.由于时、分、秒都由两个数字组成(小于10的前面加0),因此可以再把问题分解,用DrawSingleNunber来画单个数字。单个数字又该怎么画呢?下面看七段数码的组成,我们用1到7的标号来表示每一段。
由于每个数字都是由这七段拼凑而成,因此我们可以在DrawSingleNumber函数中用switch语句,根据不同的数字去画不同的段,接下来的工作就是如何去画这七段了,每一段都是一个具有颜色填充的多边形。
void CDigitalClock::DrawSingleNumber(int nNum,int nLeft) { switch (nNum) { case 0: DrawSection1(nLeft); DrawSection2(nLeft); DrawSection3(nLeft); DrawSection4(nLeft); DrawSection5(nLeft); DrawSection6(nLeft); break; case 1: DrawSection2(nLeft); DrawSection3(nLeft); break; case 2: DrawSection1(nLeft); DrawSection2(nLeft); DrawSection4(nLeft); DrawSection5(nLeft); DrawSection7(nLeft); break; case 3: DrawSection1(nLeft); DrawSection2(nLeft); DrawSection3(nLeft); DrawSection4(nLeft); DrawSection7(nLeft); break; case 4: DrawSection2(nLeft); DrawSection3(nLeft); DrawSection6(nLeft); DrawSection7(nLeft); break; case 5: DrawSection1(nLeft); DrawSection3(nLeft); DrawSection4(nLeft); DrawSection6(nLeft); DrawSection7(nLeft); break; case 6: DrawSection1(nLeft); DrawSection3(nLeft); DrawSection4(nLeft); DrawSection5(nLeft); DrawSection6(nLeft); DrawSection7(nLeft); break; case 7: DrawSection1(nLeft); DrawSection2(nLeft); DrawSection3(nLeft); break; case 8: DrawSection1(nLeft); DrawSection2(nLeft); DrawSection3(nLeft); DrawSection4(nLeft); DrawSection5(nLeft); DrawSection6(nLeft); DrawSection7(nLeft); break; case 9: DrawSection1(nLeft); DrawSection3(nLeft); DrawSection4(nLeft); DrawSection2(nLeft); DrawSection6(nLeft); DrawSection7(nLeft); break; default: ; } } |