#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <math.h>
#include <time.h>
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(linker, "/SUBSYSTEM:Windows")
void GetPos(double degree, int len, int* x, int* y)
{
*x = len * sin(degree);
*y = len * cos(degree);
}
void DrawClock(HWND hw, HDC h, int hour, int minute, int second)
{
HDC bufdc = CreateCompatibleDC(h);
HBITMAP buf = CreateCompatibleBitmap(bufdc, 200, 200);
RECT cr;
HBRUSH bBg = CreateSolidBrush(RGB(255, 255, 255));
GetClientRect(hw, &cr);
SelectObject(bufdc, bBg);
SelectObject(bufdc, buf);
FillRect(bufdc, &cr, bBg);
{
int sx, sy, ex, ey;
int i;
for(i = 0; i < 12; ++i) {
GetPos(2*3.14159/12*i, 85, &sx, &sy);
GetPos(2*3.14159/12*i, 95, &ex, &ey);
MoveToEx(bufdc, 100+sx, 100-sy, 0);
LineTo(bufdc, 100+ex, 100-ey);
}
}
{
int hx, hy, mx, my, sx, sy;
GetPos(2*3.14159 / 12 * (hour + (double)minute / 60 + (double)second / 3600), 50, &hx, &hy);
GetPos(2*3.14159 / 60 * (minute + (double)second / 60), 65, &mx, &my);
GetPos(2*3.14159 / 60 * second, 80, &sx, &sy);
MoveToEx(bufdc, 100, 100, NULL);
LineTo(bufdc, 100+hx, 100-hy);
MoveToEx(bufdc, 100, 100, NULL);
LineTo(bufdc, 100+mx, 100-my);
MoveToEx(bufdc, 100, 100, NULL);
LineTo(bufdc, 100+sx, 100-sy);
}
BitBlt(h, 0, 0, 200, 200, bufdc, 0, 0, SRCCOPY);
DeleteDC(bufdc);
DeleteObject(bBg);
DeleteObject(buf);
}
LRESULT CALLBACK WndProc(HWND h, UINT m, WPARAM w, LPARAM l)
{
switch(m) {
case WM_CREATE:
SetTimer(h, 1, 1000, 0);
return 0;
case WM_ERASEBKGND:
return 1;
case WM_TIMER:
InvalidateRect(h, 0, FALSE);
return 0;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC dc = BeginPaint(h, &ps);
{
time_t t;
struct tm* pst;
time(&t);
pst = localtime(&t);
DrawClock(h, dc, pst->tm_hour, pst->tm_min, pst->tm_sec);
}
EndPaint(h, &ps);
return 0;
}
case WM_DESTROY:
KillTimer(h, 1);
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(h, m, w, l);
}
}
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmd, INT nShow)
{
WNDCLASS wc;
ZeroMemory(&wc, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInst;
wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
wc.lpszClassName = TEXT("MYCLOCK");
if (RegisterClass(&wc) != 0) {
MSG m;
HWND hw = CreateWindow(TEXT("MYCLOCK"), TEXT("Clock"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, 0);
if (hw != NULL) {
RECT r;
GetWindowRect(hw, &r);
r.right = r.left + 200;
r.bottom = r.top + 200;
AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
MoveWindow(hw, r.left, r.top, r.right - r.left, r.bottom - r.top, FALSE);
ShowWindow(hw, SW_SHOWNORMAL);
UpdateWindow(hw);
while( GetMessage(&m, 0, 0, 0) > 0) {
TranslateMessage(&m);
DispatchMessage(&m);
}
return 0;
}
return 1;
} else
return 1;
}
#include <windows.h>
#include <math.h>
#include <time.h>
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(linker, "/SUBSYSTEM:Windows")
void GetPos(double degree, int len, int* x, int* y)
{
*x = len * sin(degree);
*y = len * cos(degree);
}
void DrawClock(HWND hw, HDC h, int hour, int minute, int second)
{
HDC bufdc = CreateCompatibleDC(h);
HBITMAP buf = CreateCompatibleBitmap(bufdc, 200, 200);
RECT cr;
HBRUSH bBg = CreateSolidBrush(RGB(255, 255, 255));
GetClientRect(hw, &cr);
SelectObject(bufdc, bBg);
SelectObject(bufdc, buf);
FillRect(bufdc, &cr, bBg);
{
int sx, sy, ex, ey;
int i;
for(i = 0; i < 12; ++i) {
GetPos(2*3.14159/12*i, 85, &sx, &sy);
GetPos(2*3.14159/12*i, 95, &ex, &ey);
MoveToEx(bufdc, 100+sx, 100-sy, 0);
LineTo(bufdc, 100+ex, 100-ey);
}
}
{
int hx, hy, mx, my, sx, sy;
GetPos(2*3.14159 / 12 * (hour + (double)minute / 60 + (double)second / 3600), 50, &hx, &hy);
GetPos(2*3.14159 / 60 * (minute + (double)second / 60), 65, &mx, &my);
GetPos(2*3.14159 / 60 * second, 80, &sx, &sy);
MoveToEx(bufdc, 100, 100, NULL);
LineTo(bufdc, 100+hx, 100-hy);
MoveToEx(bufdc, 100, 100, NULL);
LineTo(bufdc, 100+mx, 100-my);
MoveToEx(bufdc, 100, 100, NULL);
LineTo(bufdc, 100+sx, 100-sy);
}
BitBlt(h, 0, 0, 200, 200, bufdc, 0, 0, SRCCOPY);
DeleteDC(bufdc);
DeleteObject(bBg);
DeleteObject(buf);
}
LRESULT CALLBACK WndProc(HWND h, UINT m, WPARAM w, LPARAM l)
{
switch(m) {
case WM_CREATE:
SetTimer(h, 1, 1000, 0);
return 0;
case WM_ERASEBKGND:
return 1;
case WM_TIMER:
InvalidateRect(h, 0, FALSE);
return 0;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC dc = BeginPaint(h, &ps);
{
time_t t;
struct tm* pst;
time(&t);
pst = localtime(&t);
DrawClock(h, dc, pst->tm_hour, pst->tm_min, pst->tm_sec);
}
EndPaint(h, &ps);
return 0;
}
case WM_DESTROY:
KillTimer(h, 1);
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(h, m, w, l);
}
}
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmd, INT nShow)
{
WNDCLASS wc;
ZeroMemory(&wc, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInst;
wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
wc.lpszClassName = TEXT("MYCLOCK");
if (RegisterClass(&wc) != 0) {
MSG m;
HWND hw = CreateWindow(TEXT("MYCLOCK"), TEXT("Clock"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, 0);
if (hw != NULL) {
RECT r;
GetWindowRect(hw, &r);
r.right = r.left + 200;
r.bottom = r.top + 200;
AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
MoveWindow(hw, r.left, r.top, r.right - r.left, r.bottom - r.top, FALSE);
ShowWindow(hw, SW_SHOWNORMAL);
UpdateWindow(hw);
while( GetMessage(&m, 0, 0, 0) > 0) {
TranslateMessage(&m);
DispatchMessage(&m);
}
return 0;
}
return 1;
} else
return 1;
}