我将数字日期显示为窗口标题,在 vc++60 新建--win32 application 项目, 然后新建一个C++文件 输入如下代码
#include<windowsh>
#include<mathh>
#define TWOPI (2314159)
#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;
wndclasscbClsExtra = 0;
wndclasscbWndExtra = 0;
wndclasshbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
wndclasshCursor = NULL;
wndclasshIcon = NULL;
wndclasshInstance = hInstance;
wndclasslpfnWndProc = WindowProc;
wndclasslpszClassName = szClassName;
wndclasslpszMenuName = NULL;
wndclassstyle = 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 msgwParam;
}
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/360i)900);
y = (int)(sin(TWOPI/360i)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->wHour30+pst->wMinute/126;
angle[1] = pst->wMinute6;
angle[2] = pst->wSecond6;
for(int i=hChange0: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秒"),
stwYear,
stwMonth,
stwDay,
stwHour,
stwMinute,
stwSecond);
SetWindowText(hwnd,time);
preSt = st;
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_TIMER:
::GetLocalTime(&st); //每次都要获取当前时间
hChange = stwHour!=preStwHour||stwMinute!=preStwMinute;
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秒"),
stwYear,
stwMonth,
stwDay,
stwHour,
stwMinute,
stwSecond);
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<reg51h>#include<INTRINSH>
#define Key_State_0 0
#define Key_State_1 1
#define Key_State_2 2
#define unlock 0
#define lock 1
#define Init_DATA 999
unsigned char key1_state;
bit key1_debounce_flag;
bit key1_debounce;
sbit Key_1 = P1^0;
sbit seg1 = P1^7;
sbit seg2 = P1^6;
sbit seg3 = P1^5;
sbit seg4 = P1^4;
unsigned char time_1ms_flag,display_flag;
unsigned int add_data;
unsigned char key1_debounce_C;
unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void MCU_init(void)
{
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x18;
EA = 1;
ET0 = 1;
TR0 = 1;
add_data = Init_DATA;
}
void MCU_clock(void)
{
if(time_1ms_flag==1)
{
time_1ms_flag = 0;
display_flag++;
if(display_flag == 4)
{
display_flag = 0;
}
if(key1_debounce_flag==unlock)
{
key1_debounce_C++;
if(key1_debounce_C==20)
{
key1_debounce_C = 0;
key1_debounce_flag = lock;
key1_debounce = 1;
}
}
}
}
void display(void)
{
switch(display_flag)
{
case 0:
seg4 = 0;
P2=table[add_data/1000];
seg1 = 1;
break;
case 1:
seg1 = 0;
P2=table[add_data%1000/100];
seg2 = 1;
break;
case 2:
seg2 = 0;
P2=table[add_data%100/10];
seg3 = 1;
break;
case 3:
seg3 = 0;
P2=table[add_data%10];
seg4 = 1;
break;
}
}
void Key_1_Scan (void)
{
bit key_press;
key_press = Key_1;
switch(key1_state)
{
case Key_State_0:
if(!key_press)
{
key1_state = Key_State_1;
key1_debounce_flag = unlock;
}
break;
case Key_State_1:
if(key1_debounce==1)
{
key1_debounce = 0;
if(!key_press)
{
key1_state = Key_State_2;
}
else
{
key1_state = Key_State_0;
}
}
break;
case Key_State_2:
if(key_press)
{
key1_state = Key_State_0;
add_data++;
if(add_data==10000)
{
add_data = 0;
}
}
break;
}
}
void main (void)
{
MCU_init();
while (1)
{
MCU_clock();
Key_1_Scan();
display();
}
}
void Timer0Interrupt(void) interrupt 1
{
TH0 = 0xFC;
TL0 = 0x18;
time_1ms_flag = 1;
}
去繁存简,mfc timer消息处理 GDI画东西,必要的东西:
mfc 的 timer 一个
dc 一个
需要用到的函数
settimer() 定时100毫秒产生一个timer消息出发ontimer, 在ontimer里面获取当前系统时间 systemtime;
如果时间改变了用dc重画时钟界面, 最简单的 moveto lineto
#include <stdioh>
struct clock {
int hour;
int minute;
int second;
};
typedef struct clock CLOCK;
/
函数功能:时、分、秒时间的更新
函数参数:无
函数返回值:无
/
void Update(CLOCK myclock) {
myclock->second++;
if (myclock->second == 60) { /若second值为60,表示已过1分钟,则 minute值加1/
myclock->second = 0;
myclock->minute++;
}
if (myclock->minute == 60){ /若minute值为60,表示已过1小时,则 hour值加1/
myclock->minute = 0;
myclock->hour++;
}
if (myclock->hour == 24) { /若hour值为24,则hour的值从0开始计时/
myclock->hour = 0;
}
}
/
函数功能:时、分、秒时间的显示
函数参数:无
函数返回值:无
/
void Display(CLOCK myclock) { /用回车符'\r'控制时、分、秒显示的位置/
printf("%2d:%2d:%2d\r", myclock->hour, myclock->minute, myclock->second);
}
/
函数功能:模拟延迟1秒的时间
函数参数:无
函数返回值:无
/
void Delay(void) {
long t;
for (t = 0; t < 290000000; t++) {
/循环体为空语句的循环,起延时作用/
}
}
int main(){
CLOCK myclock;
long i;
myclockhour = myclockminute = myclocksecond = 0; /hour,minute,second赋初值0/
for (i = 0; i < 100000; i++) { /利用循环结构,控制时钟运行的时间/
Update(&myclock); /时钟更新/
Display(&myclock); /时间显示/
Delay(); /模拟延时1秒/
}
return 0;
}
以上就是关于急!!C++编程,高手进!!用Vc++6.0编,模拟时钟转动的程序,要求如下全部的内容,包括:急!!C++编程,高手进!!用Vc++6.0编,模拟时钟转动的程序,要求如下、单片机时钟程序时钟设置问题、《面向对象程序设计》课程设计模拟时钟程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)