#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include "resource.h"
#define PI 3.14159
using namespace std;
// Returns number of elements
#define dim(x) (sizeof(x) / sizeof(x[0]))//计算tagMESSAGEMAP数组的大小
struct tagMESSAGEMAP // Structure associates messages with a function.
{
UINT Code;
LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);//定义了一个函数指针
};
// Window procedures
LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);//回调函数
// Message handlers
LRESULT OnPaintMain (HWND, UINT, WPARAM, LPARAM);
LRESULT OnDestroyMain (HWND, UINT, WPARAM, LPARAM);
LRESULT OnLButtonDown (HWND, UINT, WPARAM, LPARAM);
LRESULT OnChar (HWND, UINT, WPARAM, LPARAM);
LRESULT OnTimer (HWND, UINT, WPARAM, LPARAM);
LRESULT OnCreate (HWND, UINT, WPARAM, LPARAM);
LRESULT OnShowWindow(HWND, UINT, WPARAM, LPARAM);
// Message dispatch table for MainWindowProc
const struct tagMESSAGEMAP MainMessages[] = {
WM_PAINT, OnPaintMain,
WM_DESTROY, OnDestroyMain,
WM_LBUTTONDOWN, OnLButtonDown,
WM_CHAR, OnChar,
WM_TIMER, OnTimer,
};
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow) // show state
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon=::LoadIcon(hInstance,(LPSTR)IDI_MAIN);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=MainWndProc;
wndcls.lpszClassName="ItJob2010";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hWnd;
hWnd = ::CreateWindow( "ItJob2010","时钟",WS_OVERLAPPEDWINDOW,
0,0,450,400,NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,SW_SHOWNORMAL);
UpdateWindow(hWnd);
::SetTimer(hWnd, 1, 1000, NULL);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
int i;
//
// Search message list to see if we need to handle this
// message. If in list, call procedure.
//
for (i = 0; i < dim(MainMessages); i++)
{
if (wMsg == MainMessages[i].Code)
{
if( (*(MainMessages[i].Fxn))(hWnd, wMsg, wParam, lParam) == 0)
return 0;
}
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
LRESULT OnPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
//获取资源
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd,&ps);
//创建画笔
HPEN pen = ::CreatePen(PS_SOLID,1,RGB(0,0,0));
HPEN oldpen = (HPEN)SelectObject(hdc,pen);
//创建画刷
HBRUSH brush = (HBRUSH)GetStockObject(NULL_BRUSH);//使园背景透明,不至于遮挡住圆心处显示的时间文本
HBRUSH oldbrush = (HBRUSH)::SelectObject(hdc,brush);
::SetBkMode(hdc,TRANSPARENT);//设置文本背景为透明
::SetTextColor(hdc,RGB(125,125,125));//设置文本颜色
//获取圆心坐标
POINT pto;
RECT rc;
GetClientRect(hWnd, &rc);
int iR = min(rc.right - rc.left, rc.bottom-rc.top)/2-40;
pto.x = (rc.right-rc.left)/2;
pto.y = (rc.bottom-rc.top)/2-30;
//显示时间
static char stime[] = "23:59:59";
SYSTEMTIME tm;
::GetLocalTime(&tm);
sprintf(stime, "%.2d:%.2d:%.2d ", tm.wHour, tm.wMinute, tm.wSecond);
::TextOut(hdc, pto.x-20, pto.y+170, (LPCSTR)stime, strlen(stime));
// sprintf(stime, "%.4d/%.2d/%.2d ",tm.wYear,tm.wMonth,tm.wDay);
// ::TextOut(hdc, pto.x-30, pto.y-20, (LPCSTR)stime, strlen(stime));
//获取计算机名
//3. 利用API函数,获取计算机名称,CPU数量,CPU类型,物理内存总数等系统信息。
//参考函数:GetComputerName() GetSystemInfo() GlobalMemoryStatus()
//画圆
Ellipse(hdc,pto.x-iR,pto.y-iR,pto.x+iR,pto.y+iR);
MoveToEx(hdc, pto.x, pto.y, (LPPOINT)NULL);
//秒针走法
DeleteObject(pen);
pen = ::CreatePen(PS_SOLID,2,RGB(255,255,255));
::SelectObject(hdc,pen);
POINT pts;
pts.x = pto.x + sin(tm.wSecond*(PI/30))*(iR*0.90f);
pts.y = pto.y - cos(tm.wSecond*(PI/30))*(iR*0.90f);
LineTo(hdc, pts.x, pts.y);
//分针走法
DeleteObject(pen);
pen = ::CreatePen(PS_SOLID,3,RGB(0,0,200));
::SelectObject(hdc,pen);
POINT ptm;
MoveToEx(hdc, pto.x, pto.y, (LPPOINT)NULL);
ptm.x = pto.x + sin((tm.wMinute + tm.wSecond/60.0f)*(PI/30))*(iR*0.70f);
ptm.y = pto.y - cos((tm.wMinute + tm.wSecond/60.0f)*(PI/30))*(iR*0.70f);
LineTo(hdc, ptm.x, ptm.y);
//时针走法
DeleteObject(pen);
pen = ::CreatePen(PS_SOLID,4,RGB(255,0,0));
::SelectObject(hdc,pen);
POINT pth;
MoveToEx(hdc, pto.x, pto.y, (LPPOINT)NULL);
pth.x = pto.x + sin((tm.wHour %12 + tm.wMinute/60.0f)*(PI/6))*(iR*0.50f);
pth.y = pto.y - cos((tm.wHour %12 + tm.wMinute/60.0f)*(PI/6))*(iR*0.50f);
LineTo(hdc, pth.x, pth.y);
//画秒针刻度
int j = 0;
for(int i=0; i<60; i++)
{
DeleteObject(pen);
pen = ::CreatePen(PS_SOLID,2,RGB(0,255,0));
::SelectObject(hdc,pen);
POINT pt0,pt1;
pt0.x = pto.x + sin(i*(PI/30))*iR*0.95f;
pt0.y = pto.y - cos(i*(PI/30))*iR*0.95f;
pt1.x = pto.x + sin(i*(PI/30))*iR*1.00f;
pt1.y = pto.y - cos(i*(PI/30))*iR*1.00f;
MoveToEx(hdc,pt0.x,pt0.y,(LPPOINT)NULL);
LineTo(hdc,pt1.x,pt1.y);
//画时刻度
if(0 == i%5)
{
DeleteObject(pen);
pen = ::CreatePen(PS_SOLID,4,RGB(255,0,0));
::SelectObject(hdc,pen);
pt0.x = pto.x + sin(i*(PI/30))*iR*0.93f;
pt0.y = pto.y - cos(i*(PI/30))*iR*0.93f;
pt1.x = pto.x + sin(i*(PI/30))*iR*1.00f;
pt1.y = pto.y - cos(i*(PI/30))*iR*1.00f;
MoveToEx(hdc,pt0.x,pt0.y,(LPPOINT)NULL);
LineTo(hdc,pt1.x,pt1.y);
//写时刻度数字
if(0 == j)
{
static char sNum[] = "12";
sprintf(sNum,"%d",12);
::SetTextColor(hdc,RGB(255,255,255));
TextOut(hdc,pto.x + sin(i*(PI/30))*iR*0.95f-7,pto.y - cos(i*(PI/30))*iR*0.90f,sNum,strlen(sNum));
j++;
}
else
{
static char sNum[] = "12";
sprintf(sNum,"%d",j);
::SetTextColor(hdc,RGB(255,255,255));
TextOut(hdc,pto.x + sin(i*(PI/30))*iR*0.87f-5,pto.y - cos(i*(PI/30))*iR*0.87f-7,sNum,strlen(sNum));
j++;
}
}
}
//8. 释放设备相关资源
SelectObject(hdc, oldbrush);
DeleteObject(brush);
SelectObject(hdc, oldpen);
DeleteObject(pen);
EndPaint(hWnd,&ps);
return 0;
}
LRESULT OnCreate(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
//在这里设置定时器,不要在WinMain中去做,我们应尽量少的在WinMain中写代码,
//WinMain中应尽量维持原有代码不动,以便于后续MFC的实现。
::SetTimer(hWnd, 123, 1000, NULL);
return 0;
}
LRESULT OnDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
KillTimer(hWnd, 123); //销毁定时器
PostQuitMessage(0);
return 0;
}
LRESULT OnLButtonDown (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
//MessageBox(hWnd,"mouse clicked","ItJob",0);
HDC hdc;
hdc=GetDC(hWnd);
TextOut(hdc,0,50,"时钟程序",strlen("时钟程序"));
ReleaseDC(hWnd,hdc);
return 0;
}
LRESULT OnChar (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hWnd,szChar,"xsafd",0);
return 0;
}
LRESULT OnTimer (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
RECT rc;
::GetClientRect(hWnd, &rc);
::InvalidateRect(hWnd, &rc,TRUE);
return 0;
}
LRESULT OnShowWindow(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
return 0;
}