控制响铃

    技术2022-05-19  23

    /**  * 简易闹钟  * @Author xxingup@gmail.com  * 2010-06-14  */ #include <iostream> #include <windows.h> #include <time.h> #include <mmsystem.h> #pragma comment(lib, "winmm.lib") typedef unsigned int uint32; #define LOG_FILE "log.txt" #define SOUND_FILE "msg.wav" //#include <stdafx.h> //#include <AFXPRIV.H> #include   <tchar.h> #include   <Wincon.h> #define   _WIN32_WINNT   0x0500 /**  * 闹钟对象  */ struct MyAlarmClock {     MyAlarmClock(uint32 _interval) : Interval(_interval)     {         NextTime = (uint32)time(NULL) + Interval * 60;     }     uint32 Interval; //间隔时间(分钟)     uint32 NextTime; //下次响铃时间(秒)     void Run();     void Alarm();     void SaveLog(); }; int main() {     //::SetWindowText(GetConsoleWindow(), "简易闹钟");     uint32 interval;     printf("设置闹铃间隔时间(分钟):");     scanf("%u", &interval);     MyAlarmClock clock(interval);     clock.Run();     return 0; } /**  * 运行时钟  */ void MyAlarmClock::Run() {     time_t t(NextTime);     tm * next = localtime(&t);     printf("下次响铃时间:%2u:%2u:%2u/n", next->tm_hour, next->tm_min, next->tm_sec);     while(1)     {         time_t t = time(NULL);         tm * now = localtime(&t);         printf("/r %2u:%2u:%2u", now->tm_hour, now->tm_min, now->tm_sec);         if((uint32)t >= NextTime)         {             SaveLog();             NextTime = (uint32)t + Interval * 60;             Alarm();         }         Sleep(1000);     } } /**  * 响铃,保证 SOUND_FILE 和程序在同一目录  */ void MyAlarmClock::Alarm() {     printf("时间到!!!/n");     for(int i = 0; i < 10; i++)//响铃10次         ::PlaySound(SOUND_FILE, NULL, SND_SYNC);     time_t t(NextTime);     tm * next = localtime(&t);     printf("下次响铃时间:%2u:%2u:%2u/n", next->tm_hour, next->tm_min, next->tm_sec); } /**  * 记录响铃时间  */ void MyAlarmClock::SaveLog() {     FILE * f = fopen(LOG_FILE, "a");     if(!f)     {         printf("打开文件`%s`失败/n", LOG_FILE);         return;     }     time_t t(NextTime);     tm * now = localtime(&t);     fprintf(f, "/n响铃时间:%u-%u-%u %u:%u:%u"         , now->tm_year + 1900         , now->tm_mon + 1         , now->tm_mday         , now->tm_hour         , now->tm_min         , now->tm_sec);     fclose(f); }

     

     

     

     

     

    //#include <MMSystem.h> //AnsiString SoundFileName = "path//yoursound.wav"; //::sndPlaySound(SoundFileName.c_str(), SND_ASYNC|SND_NODEFAULT); // //第一步:SetTimer设置一个定时器,每10秒调用一次OnTimer函数 //第二步:OnTimer函数中查看系统时间,如果在某个范围内(到达指定时间),则循环调用MessagePeek函数,发出系统警告声,或自己写一个闹铃的函数。 //用纯控制台给你写了个简单的闹钟程序,你看看行不?可以自己添加一些功能,我把闹钟的部分分出一条线程了,/可以在主线程或者闹钟线程中任意添加功能/ #include <stdio.h> #include <afx.h> CString ringTime = ""; void ring()//控制响铃 {     for (int i = 10;i>0;i--)     {         printf("/a");         Sleep(500);         printf("/a");         Sleep(500);         printf("/a");         Sleep(500);         printf("/a/a/a/a");         Sleep(i*200);     } } void gThread()//闹钟执行线程 {     CString timeS = "";     printf("闹钟时间:%s/n",ringTime);     while(true)     {         CTime time = CTime::GetCurrentTime();         printf("/b/b/b/b/b/b/b/b/b/b/b/b/b/b/b/b/b/b/b/b/b/b/b/b/b/b");         timeS = time.Format("%Y年%m月%d日%H时%M分%S秒");         printf("%s",timeS);         if(timeS == ringTime){printf("/n时间到!");ring();break;}         Sleep(1000);     } } void main() {     DWORD threadID;     HANDLE threads;     int n,y,m,d,hh,mm,ss;     CTime time = CTime::GetCurrentTime();     CString    timeS = time.Format("%Y年%m月%d日%H时%M分%S秒");     printf("当前日期时间:%s/n",timeS);     printf("输入闹钟日期时间:(yyyy-mm-dd,hh:MM:ss)");     scanf("%d-%d-%d,%d:%d:%d",&y,&m,&d,&hh,&mm,&ss);     ringTime.Format("d年d月d日%d时d分d秒",y,m,d,hh,mm,ss);     threads = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)gThread,0,0,(LPDWORD)&threadID);//开始闹钟线程     scanf("%d",&n);     TerminateThread(threads,NULL); }


    最新回复(0)