+ -
当前位置:首页 → 问答吧 → 时钟不按系统时间动的问题。。求助~~

时钟不按系统时间动的问题。。求助~~

时间:2011-12-07

来源:互联网

#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;
}
问题一:不按系统时间转动
问题二:见注释。。

作者: A1157599735   发布时间: 2011-12-07

你获取的时间只显示了一次,你要实时显示系统时间,就要经常去获取时间,然后显示,
一般是设置一个定时器 SetTimer ,然后响应 WM_TIMER 消息

作者: agoago_2009   发布时间: 2011-12-08

引用 1 楼 agoago_2009 的回复:

你获取的时间只显示了一次,你要实时显示系统时间,就要经常去获取时间,然后显示,
一般是设置一个定时器 SetTimer ,然后响应 WM_TIMER 消息

???
可不可以帮我改下??

作者: A1157599735   发布时间: 2011-12-08

C/C++ code
//#include "StdAfx.h"
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>


#define pi 3.1415926

SYSTEMTIME 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;
}


钟表的指针还有问题,指向的结果不对,应该是你在 LineTo 的时候,坐标没有对

作者: agoago_2009   发布时间: 2011-12-08