
程序代码:
#include <Windows.h>
#include <strsafe.h>
#include <math.h>
#define PI (3.1415)
#define ID (1000)
#define BUF_SIZE (50)
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Clock");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
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 0;
}
hwnd = CreateWindow(szAppName, TEXT("Clock Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
static int cxClient;//客户区x方向的长度
static int cyClient;//客户区y方向的长度
static unsigned int g_H;//时
static unsigned int g_M;//分
static unsigned int g_S;//秒
static TCHAR szBuffer[BUF_SIZE];
void ShowClock(HWND, HDC);
void TimerProc(HWND);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;//
PAINTSTRUCT ps;
switch (msg)
{
case WM_CREATE:
SetTimer(hwnd, ID, 1000, NULL);
case WM_SIZE:
cxClient = LOWORD(lParam);//GetSystemMetrics(SM_CXMAXIMIZED);
cyClient = HIWORD(lParam);//GetSystemMetrics(SM_CYMAXIMIZED);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
ShowClock(hwnd, hdc);
EndPaint(hwnd, &ps);
return 0;
case WM_TIMER:
TimerProc(hwnd);
return 0;
case WM_KEYDOWN://按键
case WM_DESTROY://退出
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
void TimerProc(HWND hwnd)
{
++g_S;
if (g_S < 60)
{
goto g_Out;
}
g_S = 0;
++g_M;
if (g_M < 60)
{
goto g_Out;
}
g_M = 0;
++g_H;
if (g_H < 24)
{
goto g_Out;
}
g_H = 0;
g_Out:
InvalidateRect(hwnd, NULL, FALSE);
}
//显示
void ShowClock(HWND hwnd, HDC hdc)
{
POINT rCenter;
unsigned int r;
HPEN hPen;
HBRUSH hBrush;
rCenter.x = cxClient/2;
rCenter.y = cyClient/2;
r = (min(rCenter.x, rCenter.y) * 9) / 10;
if (g_H >= 12)
{
StringCchPrintfW(szBuffer, 50, TEXT("PM %02d : %02d : %02d"), g_H, g_M, g_S);
}
else
{
StringCchPrintfW(szBuffer, 50, TEXT("AM %02d : %02d : %02d"), g_H, g_M, g_S);
}
SetTextAlign(hdc, TA_RIGHT| TA_BOTTOM);
TextOut(hdc, cxClient, cyClient, szBuffer, lstrlen(szBuffer));
hPen = (HPEN)SelectObject(hdc, CreatePen(PS_DASHDOTDOT, 0, RGB(0x80, 0x80, 0x80)));
Ellipse(hdc, rCenter.x - r, rCenter.y - r, rCenter.x + r, rCenter.y + r);
SelectObject(hdc, hPen);
hBrush = (HBRUSH)SelectObject(hdc, GetStockObject(GRAY_BRUSH));
Ellipse(hdc, rCenter.x - r/50, rCenter.y - r/50, rCenter.x + r/50, rCenter.y + r/50);
MoveToEx(hdc, rCenter.x, rCenter.y, NULL);
LineTo(hdc, rCenter.x + r * sin((g_S*PI)/30.0), rCenter.y - r * cos((g_S*PI)/30.0) );
SelectObject(hdc, hBrush);
}