#include<Windows.h>
#include<tchar.h>
#include<math.h>
typedef struct Time{
int hour,min,sec;
}TimeStructure;
BOOLEAN InitWindowClass(HINSTANCE hInstance,int nCmdShow);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HFONT CreateMyFont(HDC hDC,int nWeight,int nCharHeight,int nCharWidth);
void AdjustTime(TimeStructure *x);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
MSG msg;
if(!InitWindowClass(hInstance,nCmdShow)){
MessageBox(NULL,L"创建窗口失败!",_T("创建窗口"),NULL);
return 1;
}
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){
HDC hDC;
PAINTSTRUCT ps;
HBRUSH hBrush;
HPEN hPen;
RECT clientRect;
static TimeStructure x;
float sita = 0;
int xOrg,yOrg,rSec,rMin,rHour,rClock,xBegin,xEnd,yBegin,yEnd;
HFONT hF;
const wchar_t textbuf_1[] = {L"正聪科技"};
const wchar_t textbuf_2[] = {L"版权所有"};
const wchar_t textbuf_3[] = {L"123456789101112"};
int nCharLen_1 = wcslen(textbuf_1);
int nCharLen_2 = wcslen(textbuf_2);
int nCharHeight;
int nCharWidth;
int nWeight;
int point_x;
int point_y;
switch(message){
case WM_CREATE:
SetTimer(hWnd,9999,1000,NULL);
break;
case WM_PAINT:
AdjustTime(&x);
hDC = BeginPaint(hWnd,&ps);
GetClientRect(hWnd,&clientRect);
hPen = (HPEN)GetStockObject(BLACK_PEN);
hBrush = CreateSolidBrush(RGB(220,255,220)); //画表盘的画刷颜色
SelectObject(hDC,hPen);
SelectObject(hDC,hBrush);
xOrg = (clientRect.left + clientRect.right) / 2; //表盘中心x坐标
yOrg = (clientRect.top + clientRect.bottom) / 2; //表盘中心y坐标
rClock = min(xOrg,yOrg) - 50; //表盘半径
rSec = rClock * 6 / 7; //秒针半径
rMin = rClock * 5 / 6; //分针半径
rHour = rClock * 2 / 3; //时针半径
Ellipse(hDC, xOrg - rClock, yOrg - rClock, xOrg + rClock, yOrg + rClock); //画表盘
for(int i = 0; i < 60; i++){
if(i%5){ //以下这段画非整点的表盘刻度
hPen = CreatePen(PS_SOLID, 2, RGB(255,0,0));
SelectObject(hDC,hPen);
xBegin = xOrg + (int)(rClock * sin(2 * 3.1415926 * i / 60));
yBegin = yOrg + (int)(rClock * cos(2 * 3.1415926 * i / 60));
MoveToEx(hDC,xBegin,yBegin,NULL);
xEnd = xOrg + (int)((rClock - 20) * sin(2 * 3.1415926 * i / 60));
yEnd = yOrg + (int)((rClock - 20) * cos(2 * 3.1415926 * i / 60));
}
else{ //以下这段画整点的表盘刻度
hPen = CreatePen(PS_SOLID,5,RGB(255,0,0));
SelectObject(hDC,hPen);
xBegin = xOrg + (int)(rClock * sin(2 * 3.1415926 * i / 60));
yBegin = yOrg + (int)(rClock * cos(2 * 3.1415926 * i / 60));
MoveToEx(hDC,xBegin,yBegin,NULL);
xEnd = xOrg + (int)((rClock - 25) * sin(2 * 3.1415926 * i / 60));
yEnd = yOrg + (int)((rClock - 25) * cos(2 * 3.1415926 * i / 60));
}
LineTo(hDC,xEnd,yEnd);
DeleteObject(hPen);
}
for(int i = 1; i <= 9; i++){ //画刻度1~9
nCharHeight = 35;
nCharWidth = 25;
nWeight = 200;
point_x = xOrg + (int)((rClock - 50) * sin((i * 360 / 12) * 3.1415926 / 180)) - 10;
point_y = yOrg - (int)((rClock - 50) * cos((i * 360 / 12) * 3.1415926 / 180)) - 15;//这两行的减10减15是根据最终成像结果微调的
SetTextColor(hDC,RGB(0,0,0));
SetBkColor(hDC,RGB(220,255,220));
hF = CreateMyFont(hDC,nWeight,nCharHeight,nCharWidth);
SelectObject(hDC,hF);
TextOut(hDC,point_x,point_y,&textbuf_3[i - 1],1);//因为字符串的下标是从0开始的,所以要减1
DeleteObject(hF);
}
for(int i = 10; i <= 12; i++){ //画刻度10~12
nCharHeight = 35;
nCharWidth = 25;
nWeight = 200;
point_x = xOrg + (int)((rClock - 50) * sin((i * 360 / 12) * 3.1415926 / 180)) - 25;
point_y = yOrg - (int)((rClock - 50) * cos((i * 360 / 12) * 3.1415926 / 180)) - 15;//这两行的减25减15是根据最终成像结果微调的
SetTextColor(hDC,RGB(0,0,0));
SetBkColor(hDC,RGB(220,255,220));
hF = CreateMyFont(hDC,nWeight,nCharHeight,nCharWidth);
SelectObject(hDC,hF);
TextOut(hDC,point_x,point_y,&textbuf_3[(10 - 1) + 2 * (i - 10)],2);//因为字符串的下标是从9开始的,所以要减1,然后每隔2个字符读出一个两位数
DeleteObject(hF);
}
for(int i = 0; i < nCharLen_1; i++){
nCharHeight = 40;
nCharWidth = 20;
nWeight = 700;
point_x = xOrg - int(8.5 * nCharWidth) + 2 * i * nCharWidth;
point_y = yOrg - 2 * nCharHeight;
SetTextColor(hDC,RGB(0,0,255)); //设置文字的颜色
SetBkColor(hDC,RGB(220,255,220));//设置文字背景色
hF = CreateMyFont(hDC,nWeight,nCharHeight,nCharWidth);
SelectObject(hDC,hF);
TextOut(hDC,point_x,point_y,&textbuf_1[i],2);
DeleteObject(hF);
}
for(int i = 0; i < nCharLen_2; i++){
nCharHeight = 40;
nCharWidth = 20;
nWeight = 700;
point_x = xOrg + int(0.5 * nCharWidth) + 2 * i * nCharWidth;
point_y = yOrg - 2 * nCharHeight;
SetTextColor(hDC,RGB(0,0,255)); //设置文字的颜色
SetBkColor(hDC,RGB(220,255,220));//设置文字背景色
hF = CreateMyFont(hDC,nWeight,nCharHeight,nCharWidth);
SelectObject(hDC,hF);
TextOut(hDC,point_x,point_y,&textbuf_2[i],2);
DeleteObject(hF);
}
hPen = CreatePen(PS_SOLID,2,RGB(255,0,0));
SelectObject(hDC,hPen);
sita = (float)(2 * 3.1415926 * x.sec / 60);
xBegin = xOrg + (int)(rSec * sin(sita));
yBegin = yOrg - (int)(rSec * cos(sita));
xEnd = xOrg + (int)(rClock * sin(sita + 3.1415926f) / 8); //除以8表示表针的尾端在表盘半径1/8处,下同
yEnd = yOrg - (int)(rClock * cos(sita + 3.1415926f) / 8);
MoveToEx(hDC,xBegin,yBegin,NULL);
LineTo(hDC,xEnd,yEnd);
hPen = CreatePen(PS_SOLID,5,RGB(0,0,0));
SelectObject(hDC,hPen);
sita = (float)(2 * 3.1415926 * (x.min + (float)x.sec / 60) / 60);
xBegin = xOrg + (int)(rMin * sin(sita));
yBegin = yOrg - (int)(rMin * cos(sita));
xEnd = xOrg + (int)(rClock * sin(sita + 3.1415926f) / 8);
yEnd = yOrg - (int)(rClock * cos(sita + 3.1415926f) / 8);
MoveToEx(hDC,xBegin,yBegin,NULL);
LineTo(hDC,xEnd,yEnd);
hPen = CreatePen(PS_SOLID,10,RGB(0,0,0));
SelectObject(hDC,hPen);
sita = (float)(2 * 3.1415926 * (x.hour + (float )x.min / 60) / 12);
xBegin = xOrg + (int)(rHour * sin(sita));
yBegin = yOrg - (int)(rHour * cos(sita));
xEnd = xOrg + (int)(rClock * sin(sita + 3.1415926f) / 8);
yEnd = yOrg - (int)(rClock * cos(sita + 3.1415926f) / 8);
MoveToEx(hDC,xBegin,yBegin,NULL);
LineTo(hDC,xEnd,yEnd);
DeleteObject(hPen);
DeleteObject(hBrush);
EndPaint(hWnd,&ps);
break;
case WM_TIMER:
if(wParam == 9999)
InvalidateRect(hWnd,NULL,true);
break;
case WM_SIZE:
InvalidateRect(hWnd,NULL,true);
break;
case WM_DESTROY:
KillTimer(hWnd,9999); //这是我加上去的,我认为销毁窗口前先要销毁定时器
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
break;
}
return 0;
}
void AdjustTime(TimeStructure *x){
SYSTEMTIME systemtime;
GetLocalTime(&systemtime); //获取本地系统时间
x->hour = systemtime.wHour;
x->min = systemtime.wMinute;
x->sec = systemtime.wSecond;
if(x->sec == 60){
x->sec = 0;
x->min++;
if(x->min == 60){
x->min = 0;
x->hour++;
if(x->hour == 12)
x->hour = 0;
}
}
}
BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow){
WNDCLASSEX wcex;
HWND hWnd;
TCHAR szWindowClass[] = L"窗口示例";
TCHAR szTitle[] = L"模拟时钟";
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = 0;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_APPLICATION));
if(!RegisterClassEx(&wcex))
return FALSE;
hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
250,0, //调节窗口看这里:窗口左上角坐标
800,730, //调节窗口看这里:窗口右下角坐标
NULL,
NULL,
hInstance,
NULL
);
if(!hWnd)
return FALSE;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
HFONT CreateMyFont(HDC hDC,int nWeight,int nCharHeight,int nCharWidth){
return CreateFont( //定义字体句柄
nCharHeight, //字体高度
nCharWidth, //字体宽度
0, //文本倾斜度为0,表示水平
0, //字体倾斜度为0
nWeight, //字体粗度,数值越大字体越粗
0, //不是斜体字
0, //无下划线
0, //无删除线
GB2312_CHARSET, //表示所用的字符集为GB2312_CHARSET
OUT_DEFAULT_PRECIS, //输出精度为缺省值
CLIP_DEFAULT_PRECIS, //裁剪精度为缺省值
DEFAULT_QUALITY, //输出质量为缺省值
DEFAULT_PITCH|FF_DONTCARE, //字间距和字体系列使用缺省值
L"正聪字体"); //字体名称;
}
Windows下的动态时钟v3.0
猜你喜欢
转载自blog.csdn.net/sunshineman1986/article/details/79535383
今日推荐
周排行