知道如何使用Windows计时器之后,可以看看一些有用的计时器应用程序了。时钟是计时器最明显的应用,我们来看两个例子:一个是数字时钟,另一个是模拟时钟。
本节必须掌握的知识点:
第45练:7段数码管数字时钟
第46练:模拟时钟
7.3.1 第45练:7段数码管数字时钟
/*------------------------------------------------------------------
045 WIN32 API 每日一练
第45个例子DIGCLOCK.C:使用计时器---实现7段数码管数字时钟
查表法
OffsetWindowOrgEx函数
GetLocaleInfo函数
(c) www.bcdaren.com, 2020
----------------------------------------------------------------*/
#include <windows.h>
#define ID_TIMER 1
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("DigClock");
…(略)
return msg.wParam;
}
//显示数字
void DisplayDigit (HDC hdc, int iNumber)
{
static BOOL fSevenSegment [10][7] = { //7段数码管表
1, 1, 1, 0, 1, 1, 1, // 0
0, 0, 1, 0, 0, 1, 0, // 1
1, 0, 1, 1, 1, 0, 1, // 2
1, 0, 1, 1, 0, 1, 1, // 3
0, 1, 1, 1, 0, 1, 0, // 4
1, 1, 0, 1, 0, 1, 1, // 5
1, 1, 0, 1, 1, 1, 1, // 6
1, 0, 1, 0, 0, 1, 0, // 7
1, 1, 1, 1, 1, 1, 1, // 8
1, 1, 1, 1, 0, 1, 1 } ; // 9
static POINT ptSegment [7][6] = { //每根数码管六边形的顶点坐标
7, 6, 11, 2, 31, 2, 35, 6, 31, 10, 11, 10,
6, 7, 10, 11, 10, 31, 6, 35, 2, 31, 2, 11,
36, 7, 40, 11, 40, 31, 36, 35, 32, 31, 32, 11,
7 , 36, 11, 32, 31, 32, 35, 36, 31, 40, 11, 40,
6 , 37, 10, 41, 10, 61, 6, 65, 2, 61, 2, 41,
36, 37, 40, 41, 40, 61, 36, 65, 32, 61, 32, 41,
7 , 66, 11, 62, 31, 62, 35, 66, 31, 70, 11, 70 } ;
int iSeg ;
for (iSeg = 0 ; iSeg < 7 ; iSeg++) //绘制数字
if (fSevenSegment [iNumber][iSeg])
Polygon (hdc, ptSegment [iSeg], 6) ; //绘制6边形
}
//显示2个数字,fSuppress---前导0标记值
void DisplayTwoDigits (HDC hdc, int iNumber, BOOL fSuppress)
{
if (!fSuppress || (iNumber / 10 != 0))
DisplayDigit(hdc, iNumber / 10);//不为0绘制第1位数
OffsetWindowOrgEx(hdc, -42, 0, NULL);//原点坐标水平右移42单位宽
DisplayDigit(hdc, iNumber % 10); //绘制第2位数
OffsetWindowOrgEx(hdc, -42, 0, NULL);//再次将原点坐标右移42个单位宽
}
void DisplayColon (HDC hdc) //显示冒号
{
POINT ptColon[2][4] = { 2, 21, 6, 17, 10, 21, 6,
25, 2, 51, 6, 47, 10, 51, 6, 55 };
//绘制多边形-冒号
Polygon(hdc, ptColon[0], 4);
Polygon(hdc, ptColon[1], 4);
OffsetWindowOrgEx(hdc, -12, 0, NULL);//右移原点坐标--冒号12单位宽
}
//显示时间
void DisplayTime (HDC hdc, BOOL f24Hour, BOOL fSuppress)
{
SYSTEMTIME st;
GetLocalTime(&st);//获取当前时间
if (f24Hour)//24小时显示格式
DisplayTwoDigits(hdc, st.wHour, fSuppress);//时
else //12