/*--------------------------------------
DIGCLOCK.C -- Digital Clock
(c) Charles Petzold, 1998
--------------------------------------*/ #include <Windows.h> #define ID_TIMER 1 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain( __in HINSTANCE hInstance
, __in_opt HINSTANCE hPrevInstance
, __in LPSTR lpCmdLine
, __in int nShowCmd )
{
static TCHAR szAppName[] = TEXT("DigClock");
HWND hwnd;
MSG msg;
WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("Program requires Windows NT!")
, szAppName, MB_ICONERROR);
return ;
} hwnd = CreateWindow(szAppName, TEXT("Digital Clock")
, WS_OVERLAPPEDWINDOW
, CW_USEDEFAULT, CW_USEDEFAULT
, CW_USEDEFAULT, CW_USEDEFAULT
, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return msg.wParam;
} void DisplayDigit(HDC hdc, int iNumber)
{
static BOOL fSevenSegment[][] =
{
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , //
}; static POINT ptSegment[][] =
{
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , }; for (int iSeg(); iSeg != ; ++iSeg)
{
if (fSevenSegment[iNumber][iSeg])
Polygon(hdc, ptSegment[iSeg], );
}
} void DisplayTwoDigits(HDC hdc, int iNumber, BOOL fSuppress)
{
if (!fSuppress || (iNumber / != ))
DisplayDigit(hdc, iNumber / ); OffsetWindowOrgEx(hdc, -, , NULL);
DisplayDigit(hdc, iNumber % );
OffsetWindowOrgEx(hdc, -, , NULL);
} void DisplayColon(HDC hdc)
{
POINT ptColon[][] = { , , , , , , , ,
, , , , , , , }; Polygon(hdc, ptColon[], );
Polygon(hdc, ptColon[], ); OffsetWindowOrgEx(hdc, -, , NULL);
} void DisplayTime(HDC hdc, BOOL f24Hour, BOOL fSuppress)
{
SYSTEMTIME st; GetLocalTime(&st); if (f24Hour)
DisplayTwoDigits(hdc, st.wHour, fSuppress);
else
DisplayTwoDigits(hdc, (st.wHour %= ) ? st.wHour : , fSuppress); DisplayColon(hdc);
DisplayTwoDigits(hdc, st.wMinute, FALSE);
DisplayColon(hdc);
DisplayTwoDigits(hdc, st.wSecond, FALSE);
} LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL f24Hour, fSuppress;
static HBRUSH hBrushRed;
static int cxClient, cyClient;
HDC hdc;
PAINTSTRUCT ps;
TCHAR szBuffer[]; switch (message)
{
case WM_CREATE:
hBrushRed = CreateSolidBrush(RGB(, , ));
SetTimer(hwnd, ID_TIMER, , NULL); //fall through
case WM_SETTINGCHANGE:
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ITIME, szBuffer, );
f24Hour = (szBuffer[] == TEXT('')); GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ITLZERO, szBuffer, );
fSuppress = (szBuffer[] == TEXT('')); InvalidateRect(hwnd, NULL, TRUE);
return ; case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return ; case WM_TIMER:
InvalidateRect(hwnd, NULL, TRUE);
return ; case WM_PAINT:
hdc = BeginPaint(hwnd, &ps); SetMapMode(hdc, MM_ISOTROPIC);
SetWindowExtEx(hdc, , , NULL);
SetViewportExtEx(hdc, cxClient, cyClient, NULL); SetWindowOrgEx(hdc, , , NULL);
SetViewportOrgEx(hdc, cxClient / , cyClient / , NULL);
SelectObject(hdc, GetStockObject(NULL_PEN));
SelectObject(hdc, hBrushRed); DisplayTime(hdc, f24Hour, fSuppress); EndPaint(hwnd, &ps);
return ; case WM_DESTROY:
KillTimer(hwnd, ID_TIMER);
DeleteObject(hBrushRed);
PostQuitMessage();
return ;
} return DefWindowProc(hwnd, message, wParam, lParam);
}

DIGCLOCK.C

第八章 计时器(DIGCLOCK)-LMLPHP

05-11 20:44