读书人

时钟不按系统时间动的有关问题。

发布时间: 2012-03-13 11:21:12 作者: rapoo

时钟不按系统时间动的问题。。求助~~
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define pi 3.1415926
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG Message;
WNDCLASS WndClass;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,"END");
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=WndProc;
WndClass.lpszClassName="WinFill";
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&WndClass);
HWND hWnd;
hWnd=CreateWindow("WinFill", //生成窗口
"时 钟 ",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hWnd,nCmdShow); //显示窗口
UpdateWindow(hWnd);

while(GetMessage(&Message,0,0,0)) //消息循环
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}

SYSTEMTIME iptime;
int m=1,n=60;
int p,q=12;
//VOID WINAPI GetLocalTime(LPSYSTEMTIME lpSystemTime); //获取当前系统时间函
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{

HDC hdc;
::PAINTSTRUCT paint;
switch(message)
{
case WM_PAINT:
{

hdc=BeginPaint(hwnd,&paint);
HPEN hpen=::CreatePen(0,3,RGB(0,255,0));
::SelectObject(hdc,hpen);
Ellipse(hdc,200,200,400,400);
for(p=0;p<60;p++)
{
double Rad=2*pi*p/60;
MoveToEx(hdc,(int)(300+100*(double)sin(Rad)),(int)(300-100*(double)cos(Rad)),NULL);
LineTo(hdc,(int)(300+90*(double)sin(Rad)),(int)(300-90*(double)cos(Rad)));
}
HPEN hpen1=::CreatePen(0,3,RGB(255,0,0));
::SelectObject(hdc,hpen1);
for(p=0;p<q;p++)
{
double Rad=2*pi*p/q;
MoveToEx(hdc,(int)(300+100*(double)sin(Rad)),(int)(300-100*(double)cos(Rad)),NULL);
LineTo(hdc,(int)(300+80*(double)sin(Rad)),(int)(300-80*(double)cos(Rad)));
}
HPEN spen=::CreatePen(0,3,RGB(0,0,0));
::SelectObject(hdc,hpen1);
MoveToEx(hdc,300,300,NULL);

GetLocalTime(&iptime);
HPEN hpen3=::CreatePen(PS_DOT,3,RGB(255,0,255));
::SelectObject(hdc,hpen3);

double hour=(iptime.wHour*3600+iptime.wMinute*60.0+iptime.wSecond)/3600/(30/pi);
::MoveToEx(hdc,300,300,0);
LineTo(hdc,int(300+20*cos(hour)),int(300+20*sin(hour)));
double minu=(iptime.wMinute*60+iptime.wSecond)/60/(30/pi);//为什么把后面改成/60/30*pi后分钟不转了??
HPEN hpen2=::CreatePen(0,3,RGB(255,255,0));
::SelectObject(hdc,hpen2);
::MoveToEx(hdc,300,300,0);
LineTo(hdc,int(300+40*cos(minu)),int(300+40*sin(minu)));
double x=iptime.wSecond*pi/30;
::MoveToEx(hdc,300,300,0);
LineTo(hdc,int(300+80*cos(x)),int(300+80*sin(x)));

if(1)
{

Sleep(1000);
InvalidateRect(hwnd,NULL,1);
m++;
}
EndPaint(hwnd,&paint);


};break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);


}
return 0;
}
问题一:不按系统时间转动
问题二:见注释。。

[解决办法]

C/C++ code
//#include "StdAfx.h"#include <windows.h>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <math.h>#define pi 3.1415926SYSTEMTIME iptime;int m=1,n=60;int p,q=12;  LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){      MSG Message;    WNDCLASS WndClass;    WndClass.cbClsExtra=0;    WndClass.cbWndExtra=0;    WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));    WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);    WndClass.hIcon=LoadIcon(NULL,"END");    WndClass.hInstance=hInstance;    WndClass.lpfnWndProc=WndProc;    WndClass.lpszClassName="WinFill";    WndClass.lpszMenuName=NULL;    WndClass.style=CS_HREDRAW|CS_VREDRAW;    RegisterClass(&WndClass);    HWND hWnd;    hWnd=CreateWindow("WinFill", //生成窗口        "时 钟 ",        WS_OVERLAPPEDWINDOW,        CW_USEDEFAULT,        0,        CW_USEDEFAULT,        0,        NULL,        NULL,        hInstance,        NULL);        ShowWindow(hWnd,nCmdShow); //显示窗口    UpdateWindow(hWnd);    SetTimer(hWnd,1,1000,NULL);        while(GetMessage(&Message,0,0,0)) //消息循环    {          TranslateMessage(&Message);        DispatchMessage(&Message);    }    return Message.wParam;}//VOID WINAPI GetLocalTime(LPSYSTEMTIME lpSystemTime); //获取当前系统时间函LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){           HDC hdc;    ::PAINTSTRUCT paint;    switch(message)    {        case WM_PAINT:            {            hdc=BeginPaint(hwnd,&paint);            HPEN hpen=::CreatePen(0,3,RGB(0,255,0));            ::SelectObject(hdc,hpen);            Ellipse(hdc,200,200,400,400);            for(p=0;p<60;p++)            {                double Rad=2*pi*p/60;                MoveToEx(hdc,(int)(300+100*(double)sin(Rad)),(int)(300-100*(double)cos(Rad)),NULL);                LineTo(hdc,(int)(300+90*(double)sin(Rad)),(int)(300-90*(double)cos(Rad)));            }            HPEN hpen1=::CreatePen(0,3,RGB(255,0,0));            ::SelectObject(hdc,hpen1);            for(p=0;p<q;p++)            {                double Rad=2*pi*p/q;                MoveToEx(hdc,(int)(300+100*(double)sin(Rad)),(int)(300-100*(double)cos(Rad)),NULL);                LineTo(hdc,(int)(300+80*(double)sin(Rad)),(int)(300-80*(double)cos(Rad)));            }            HPEN spen=::CreatePen(0,3,RGB(0,0,0));            ::SelectObject(hdc,hpen1);            MoveToEx(hdc,300,300,NULL);                        //GetLocalTime(&iptime);            HPEN hpen3=::CreatePen(PS_DOT,3,RGB(255,0,255));            ::SelectObject(hdc,hpen3);                        double hour=(iptime.wHour*3600+iptime.wMinute*60.0+iptime.wSecond)/3600/(30/pi);            ::MoveToEx(hdc,300,300,0);            LineTo(hdc,int(300+20*cos(hour)),int(300+20*sin(hour)));            double minu=(iptime.wMinute*60+iptime.wSecond)/60/(30/pi);//为什么把后面改成/60/30*pi后分钟不转了??            HPEN hpen2=::CreatePen(0,3,RGB(255,255,0));            ::SelectObject(hdc,hpen2);            ::MoveToEx(hdc,300,300,0);            LineTo(hdc,int(300+40*cos(minu)),int(300+40*sin(minu)));            double x=iptime.wSecond*pi/30;            ::MoveToEx(hdc,300,300,0);            LineTo(hdc,int(300+80*cos(x)),int(300+80*sin(x)));            char pstrtime[50];            sprintf(pstrtime,"%d:%d:%d",iptime.wHour,iptime.wMinute,iptime.wSecond);            ::TextOut(hdc,0,0,pstrtime,strlen(pstrtime));                /*        if(1)            {                                Sleep(1000);                InvalidateRect(hwnd,NULL,1);                m++;            }   */            EndPaint(hwnd,&paint);            break;            }    case WM_DESTROY:        PostQuitMessage(0);          break;      case WM_TIMER:        GetLocalTime(&iptime);        InvalidateRect(hwnd,NULL,1);        break;     default:          return DefWindowProc(hwnd,message,wParam,lParam);    }    return 0;} 

读书人网 >C语言

热点推荐