急!!C++编程,高手进!!用Vc++6.0编,模拟时钟转动的程序,要求如下
模拟时钟转动程序要求:能模拟机械钟表行走,还要准确地利用数字显示日期和时间,在屏幕上显示一个活动时钟,按任意键时程序退出...
模拟时钟转动程序 要求: 能模拟机械钟表行走,还要准确地利用数字显示日期和时间,在屏幕上显示一个活动时钟,按任意键时程序退出
展开
1个回答
2013-07-20
展开全部
我将数字日期显示为窗口标题,在 vc++6.0 新建--win32 application 项目, 然后新建一个C++文件 输入如下代码
#include<windows.h>
#include<math.h>
#define TWOPI (2*3.14159)
#define IDTIMER 1
#define ANGLE TWOPI/360
LRESULT CALLBACK WindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
TCHAR szClassName[] = TEXT("analogCloc");
MSG msg;
HWND hwnd;
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
wndclass.hCursor = NULL;
wndclass.hIcon = NULL;
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WindowProc;
wndclass.lpszClassName = szClassName;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
::RegisterClass(&wndclass);
hwnd = ::CreateWindow(szClassName,TEXT("Clock"),WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
::ShowWindow(hwnd,nCmdShow);
::UpdateWindow(hwnd);
while(::GetMessage(&msg,NULL,0,0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}
void setISOTROPIC(HDC hdc,int cxClient,int cyClient)//设置映射模式,使之成为笛卡尔坐标系的映射模式
{
::SetMapMode(hdc,MM_ISOTROPIC);
::SetWindowExtEx(hdc,1000,1000,NULL); // 逻辑单位与设备单位比1/2
::SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL);
::SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL); // 竖坐标向上为正,下为负
}
void drawClock(HDC hdc)
{
int x, y, radius;
::SelectObject(hdc,::CreateSolidBrush(RGB(1,148,138)));
for(int i=0;i<360;i+=6)
{
x = (int)(cos(TWOPI/360*i)*900);
y = (int)(sin(TWOPI/360*i)*900);
radius = !(i%5)?40:10;
Ellipse(hdc,x-radius,y-radius,x+radius,y+radius);
}
}
void drawHands(HDC hdc,SYSTEMTIME *pst,BOOL hChange)
{
int radius[3] = {500,700,850};
int angle[3];
angle[0] = pst->wHour*30+pst->wMinute/12*6;
angle[1] = pst->wMinute*6;
angle[2] = pst->wSecond*6;
for(int i=hChange?0:2;i<3;i++)
{
MoveToEx(hdc,0,0,NULL);
LineTo(hdc,(int)(radius[i]*cos(ANGLE*(90-angle[i]))),
(int)(radius[i]*sin(ANGLE*(90-angle[i]))));
}
}
LRESULT CALLBACK WindowProc(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
TCHAR time[40];
PAINTSTRUCT ps;
HDC hdc;
static int cxClient, cyClient;
SYSTEMTIME st;
static SYSTEMTIME preSt;
BOOL hChange;
switch(message)
{
case WM_CREATE:
::SetTimer(hwnd,IDTIMER,1000,NULL);
::GetLocalTime(&st);
wsprintf(time,TEXT("%d年%d月%d日%d时%d分%d秒"),
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wSecond);
SetWindowText(hwnd,time);
preSt = st;
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_TIMER:
::GetLocalTime(&st); //每次都要获取当前时间
hChange = st.wHour!=preSt.wHour||st.wMinute!=preSt.wMinute;
hdc = GetDC(hwnd);
setISOTROPIC(hdc,cxClient,cyClient);
::SelectObject(hdc,::GetStockObject(WHITE_PEN));
drawHands(hdc,&preSt,hChange);
::SelectObject(hdc,::GetStockObject(BLACK_PEN));
drawHands(hdc,&st,TRUE);
ReleaseDC(hwnd,hdc);
wsprintf(time,TEXT("%d年%d月%d日%d时%d分%d秒"),
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wSecond);
SetWindowText(hwnd,time);
preSt = st; // 更新完毕后记录前一次的状态 return 0;
case WM_KEYDOWN:
case WM_CHAR:
::DestroyWindow(hwnd);
return 0;
case WM_PAINT:
hdc = ::BeginPaint(hwnd,&ps);
setISOTROPIC(hdc,cxClient,cyClient);
drawClock(hdc);
drawHands(hdc,&preSt,TRUE);
::EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
#include<windows.h>
#include<math.h>
#define TWOPI (2*3.14159)
#define IDTIMER 1
#define ANGLE TWOPI/360
LRESULT CALLBACK WindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
TCHAR szClassName[] = TEXT("analogCloc");
MSG msg;
HWND hwnd;
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
wndclass.hCursor = NULL;
wndclass.hIcon = NULL;
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WindowProc;
wndclass.lpszClassName = szClassName;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
::RegisterClass(&wndclass);
hwnd = ::CreateWindow(szClassName,TEXT("Clock"),WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
::ShowWindow(hwnd,nCmdShow);
::UpdateWindow(hwnd);
while(::GetMessage(&msg,NULL,0,0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}
void setISOTROPIC(HDC hdc,int cxClient,int cyClient)//设置映射模式,使之成为笛卡尔坐标系的映射模式
{
::SetMapMode(hdc,MM_ISOTROPIC);
::SetWindowExtEx(hdc,1000,1000,NULL); // 逻辑单位与设备单位比1/2
::SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL);
::SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL); // 竖坐标向上为正,下为负
}
void drawClock(HDC hdc)
{
int x, y, radius;
::SelectObject(hdc,::CreateSolidBrush(RGB(1,148,138)));
for(int i=0;i<360;i+=6)
{
x = (int)(cos(TWOPI/360*i)*900);
y = (int)(sin(TWOPI/360*i)*900);
radius = !(i%5)?40:10;
Ellipse(hdc,x-radius,y-radius,x+radius,y+radius);
}
}
void drawHands(HDC hdc,SYSTEMTIME *pst,BOOL hChange)
{
int radius[3] = {500,700,850};
int angle[3];
angle[0] = pst->wHour*30+pst->wMinute/12*6;
angle[1] = pst->wMinute*6;
angle[2] = pst->wSecond*6;
for(int i=hChange?0:2;i<3;i++)
{
MoveToEx(hdc,0,0,NULL);
LineTo(hdc,(int)(radius[i]*cos(ANGLE*(90-angle[i]))),
(int)(radius[i]*sin(ANGLE*(90-angle[i]))));
}
}
LRESULT CALLBACK WindowProc(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
TCHAR time[40];
PAINTSTRUCT ps;
HDC hdc;
static int cxClient, cyClient;
SYSTEMTIME st;
static SYSTEMTIME preSt;
BOOL hChange;
switch(message)
{
case WM_CREATE:
::SetTimer(hwnd,IDTIMER,1000,NULL);
::GetLocalTime(&st);
wsprintf(time,TEXT("%d年%d月%d日%d时%d分%d秒"),
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wSecond);
SetWindowText(hwnd,time);
preSt = st;
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_TIMER:
::GetLocalTime(&st); //每次都要获取当前时间
hChange = st.wHour!=preSt.wHour||st.wMinute!=preSt.wMinute;
hdc = GetDC(hwnd);
setISOTROPIC(hdc,cxClient,cyClient);
::SelectObject(hdc,::GetStockObject(WHITE_PEN));
drawHands(hdc,&preSt,hChange);
::SelectObject(hdc,::GetStockObject(BLACK_PEN));
drawHands(hdc,&st,TRUE);
ReleaseDC(hwnd,hdc);
wsprintf(time,TEXT("%d年%d月%d日%d时%d分%d秒"),
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wSecond);
SetWindowText(hwnd,time);
preSt = st; // 更新完毕后记录前一次的状态 return 0;
case WM_KEYDOWN:
case WM_CHAR:
::DestroyWindow(hwnd);
return 0;
case WM_PAINT:
hdc = ::BeginPaint(hwnd,&ps);
setISOTROPIC(hdc,cxClient,cyClient);
drawClock(hdc);
drawHands(hdc,&preSt,TRUE);
::EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询