#include "stdio.h"
#include "windows.h"
#include "conio.h"
#include "time.h"
#include "stdlib.h"
#include "graphics.h"
IMAGE img;
FILE *fp;
time_t rawtime;
struct tm * timeinfo;
struct times
{
int hour;
int min;
int sec;
}timea;
void init() //建立文件并初始化
{
printf("未建立文件,请初始化……\n\n");
if((fp=fopen("D:\\time","wb"))==NULL) //建立文件并打开 只写
{
printf("error!!can't open the file\n");
return;
}
printf("现在请出入时间:\n");
printf("\t小时为:");
scanf("%d",&timea.hour);
if(timea.hour<0||timea.hour>=24)
{
printf("输入错误请重新输入:");
scanf("%d",&timea.hour);
}
printf("\t分钟为:");
scanf("%d",&timea.min);
if(timea.min<0||timea.min>=60)
{
printf("输入错误请重新输入:");
scanf("%d",&timea.min);
}
printf("\t秒为:");
scanf("%d",&timea.sec);
if(timea.sec<0||timea.sec>=24)
{
printf("输入错误请重新输入:");
scanf("%d",&timea.sec);
}
fwrite(&timea,sizeof(struct times),1,fp);
fclose(fp);
}
void load()//读入数据
{
if((fp=fopen("D:\\time","r"))==NULL)
{
printf("error!!can't open the file\n");
return;
}
fread(&timea,sizeof(struct times),1,fp);
fclose(fp);
}
void Hide()
{
char tempBuf[256];
GetConsoleTitle(tempBuf, 256);
printf("%s\n", tempBuf);
HWND hWndDos = FindWindow(NULL, tempBuf);
ShowWindow(hWndDos,SW_HIDE);
}
void shutdown()
{
system("shutdown -s -t 30");
}
void now_time()
{
time ( &rawtime );
timeinfo = localtime ( &rawtime );
}
void main()
{
initgraph(500,350);
loadimage(&img,"图片\\title.jpg");
putimage(0,0,&img);
Sleep(3000);
closegraph();
char b;
if((fp=fopen("D:\\time","r"))==NULL)
{
init();
printf("储存完毕!!\n\n");
}
A:
load();
printf("您设定的时间为:%2d:%2d:%2d\n",timea.hour,timea.min,timea.sec);
now_time();
printf("当前时间为:%2d:%2d:%2d\n",timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
printf("需要重新设置请按s\n隐藏界面并确定时间请按h\n退出系统请按q\n现在关机请按t\n");
b=getch();
if(b=='s')
{
init();
printf("储存完毕,请按任意键重新选择\n");
getch();
goto A;
}
if(b=='q')
{
exit(0);
}
if(b=='h')
{
Hide();
while(1)
{
now_time();
printf("当前时间为:%2d:%2d:%2d\n",timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
// if(((timea.hour)<=(timeinfo->tm_hour))&&((timea.min)<=(timeinfo->tm_min))&&((timea.sec)<=(timeinfo->tm_sec)))
if(timeinfo->tm_hour>timea.hour)break;
if(timeinfo->tm_hour==timea.hour)
{
if(timeinfo->tm_min>timea.min)break;
if(timeinfo->tm_min==timea.min)
if(timeinfo->tm_sec>=timea.sec)
break;
}
}
printf("\a\a\a\a");
shutdown();
}
if(b=='t')
{
shutdown();
}
else
{
printf("不存在的选择,请重新选择!\n");
goto A;
}
}