#include <Windows.h>
#define ID_TIMER 1
void DisplayDight(HDC hdc,int Number)
{
static BOOL f[10][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 p[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 i;
for(i = 0;i<7;i++)
{
if(f[Number][i])
{
Polygon(hdc,p[i],6);
}
}
}
void DisplayTwoDigits(HDC hdc,int Number,BOOL f)
{
if(!f||(Number/10!=0))
{
DisplayDight(hdc,Number/10);
OffsetWindowOrgEx(hdc,-42,0,NULL);
DisplayDight(hdc,Number%10);
OffsetWindowOrgEx(hdc,-42,0,NULL);
}
}
void DisplayColon(HDC hdc)
{
POINT p[2][4]={2,21,6,17,10,21,6,25,2,51,6,47,10,51,6,55};
Polygon(hdc,p[0],4);
Polygon(hdc,p[1],4);
OffsetWindowOrgEx(hdc,-12,0,NULL);
}
void DisplayTime(HDC hdc,BOOL f24,BOOL fs)
{
SYSTEMTIME st;
GetLocalTime(&st);
if(f24)
{
DisplayTwoDigits(hdc,st.wHour,fs);
}
else
{
DisplayTwoDigits(hdc,(st.wHour%=12)?st.wHour:12,fs);
}
DisplayColon(hdc);
DisplayTwoDigits(hdc,st.wMinute,FALSE);
DisplayColon(hdc);
DisplayTwoDigits(hdc,st.wSecond,FALSE);
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
static BOOL f24,fs;
static HBRUSH hbrush;
static int cx,cy;
HDC hdc;
PAINTSTRUCT ps;
TCHAR sz[2];
switch(uMsg)
{
case WM_CREATE:
{
hbrush = CreateSolidBrush(RGB(255,0,0));
SetTimer(hwnd,ID_TIMER,1000,NULL);
}break;
case WM_SETTINGCHANGE:
{
GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_ITIME,sz,2);
f24 = (sz[0]=='1');
GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_ITLZERO,sz,2);
fs = (sz[0]=='0');
InvalidateRect(hwnd,NULL,TRUE);
}break;
case WM_SIZE:
{
cx = LOWORD(lParam);
cy = HIWORD(lParam);
}break;
case WM_TIMER:
{
InvalidateRect(hwnd,NULL,TRUE);
}
case WM_CHAR:
{
}break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
SetMapMode(hdc,MM_ISOTROPIC);
SetWindowExtEx(hdc,276,72,NULL);
SetViewportExtEx(hdc,cx,cy,NULL);
SetWindowOrgEx(hdc,138,36,NULL);
SetViewportOrgEx(hdc,cx/2,cy/2,NULL);
SelectObject(hdc,GetStockObject(NULL_PEN));
SelectObject(hdc,hbrush);
DisplayTime(hdc,f24,fs);
EndPaint(hwnd,&ps);
}break;
case WM_DESTROY:
{
KillTimer(hwnd,ID_TIMER);
DeleteObject(hbrush);
PostQuitMessage(0);
}break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL,IDC_HAND);
wndclass.hIcon = LoadIcon(NULL,IDI_QUESTION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WindowProc;
wndclass.lpszClassName = "2013_11_27(2)_demo";
wndclass.lpszMenuName =NULL;
wndclass.style = CS_HREDRAW |CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,"创建窗口失败","失败",MB_OK);
}
HWND hwnd = CreateWindow("2013_11_27(2)_demo","time的demo",WS_OVERLAPPEDWINDOW|WS_VISIBLE,0,0,500,500,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
MSG msg;
BOOL flag;
while((flag = GetMessage(&msg,hwnd,0,0))!=0)
{
if(flag==-1)
{
PostQuitMessage(0);
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}