clockdlg.cpp

来自「个人计算机定时自动执行软件,是 一个值得看看的程序。」· C++ 代码 · 共 2,235 行 · 第 1/5 页

CPP
2,235
字号

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void CClockDlg::FreeData()//释放数据
{
	for(int i=0; i<m_MissionArray.GetSize(); i++)
		delete (MISSION*)m_MissionArray[i];
	m_MissionArray.RemoveAll();//frees all memory used for pointer storage

}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void CClockDlg::LoadData()//从文件中读取数据(包括新增任务后,数据更新也用这个函数)
{
	char out_path[512];
	GetModuleFileName(NULL,out_path,512);//得到程序运行所在位置
	char *p = strrchr(out_path,'\\');
	p[1]='\0';
	strcat(out_path,"Mission.dat");

	CFile file;
	if(file.Open(out_path,CFile::modeRead|CFile::typeBinary))
	{
		long listcount=0;
		BYTE length[4];
		long *plength=(long*)length;

		file.Read((void*)length,4);
		listcount=*plength;
		

		MISSION *pmission;

		for(int i=0;i<listcount;i++)
		{	
			pmission = new MISSION;//要释放掉
			file.Read((void*)pmission,sizeof(MISSION));
			m_MissionArray.Add((DWORD)pmission);
		}

		file.Close();
	}
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void CClockDlg::ClosetoClock(void)//关闭闹钟
{

	for(int i=0; i<m_MissionArray.GetSize(); i++)
	{
		KillTimer(10+i);
	}
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void CClockDlg::ChecktoClock(void)//设置闹钟
{

	for(int i=0; i<m_MissionArray.GetSize(); i++)
	{
		if(((MISSION*)m_MissionArray[i])->set_using)//如果有启用闹铃.则设置定时器,
		{
			SetTimer(10+i,1000,NULL);//规定定时器为10+i为列表中的项
		}
	}

}
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////    线程区    /////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void __cdecl beginThreadFunc( LPVOID )//铃声线程
{
    PlaySound(MAKEINTRESOURCE(IDR_WAVE1),AfxGetResourceHandle(),SND_RESOURCE);
}


void __cdecl beginstandardThreadFunc( LPVOID )//默认标准铃声线程
{
    PlaySound(MAKEINTRESOURCE(IDR_WAVE2),AfxGetResourceHandle(),SND_RESOURCE);
}





void __cdecl beginSoundThreadFunc( LPVOID )//整点报时自定义音乐线程(同一时间内有两个以上读取文件,就会出错)
{
	char files_path[512];
	GetModuleFileName(NULL,files_path,512);
	char *p = strrchr(files_path,'\\');
	p[1]='\0';
	strcat(files_path,"Settings.ini");
	char buffer[256];
	GetPrivateProfileString("Sound","m_strPath","",buffer,256,files_path);
    PlaySound(buffer,NULL,SND_FILENAME);

}

void __cdecl beginwavThread( LPVOID para )//播放wav文件音乐线程
{
	char *pcd=(char*)para;

	PlaySound(pcd,NULL,SND_FILENAME);
}


void __cdecl beginactionThread( LPVOID actionpara )//执行任务前提示线程
{
	int paction=(int)actionpara;
	switch(paction)
	{
	case 0:
	 {
		AfxMessageBox(" 提醒(此次任务):打开程序或文件! ");
		break;
	 }
	case 1:
	 {
		AfxMessageBox(" 提醒(此次任务):关闭计算机! ");
		break;
	 }
	case 2:
	 {
		AfxMessageBox(" 提醒(此次任务):计算机待机! ");
		break;
	 }
	case 3:
	 {
		AfxMessageBox(" 提醒(此次任务):重新启动计算机! ");
		break;
	 }
	case 4:
	 {
		AfxMessageBox(" 提醒(此次任务):注销计算机! ");
		break;
	 }
	}
}


void __cdecl begindofileThread( LPVOID filepara )//执行任务线程
{
	char *pfile=(char*)filepara;
	//if(AfxMessageBox(" 确定执行文件任务? ",MB_OKCANCEL)==IDOK)
		::ShellExecute(NULL, "open", pfile, NULL, NULL, SW_SHOWNORMAL);
}

void __cdecl beginshutdownThread( LPVOID )//执行关闭任务线程
{
	//if(AfxMessageBox(" 确定关闭计算机? ",MB_OKCANCEL)==IDOK)
		::ExitWindowsEx(EWX_SHUTDOWN,0);
}

void __cdecl beginpoweroffThread( LPVOID )//执行待机任务线程
{
	//if(AfxMessageBox(" 确定计算机待机? ",MB_OKCANCEL)==IDOK)
		::ExitWindowsEx(EWX_POWEROFF,0);
}

void __cdecl beginrebootThread( LPVOID )//执行重启任务线程
{
	//if(AfxMessageBox(" 确定重启计算机? ",MB_OKCANCEL)==IDOK)
		::ExitWindowsEx(EWX_REBOOT,0);
}

void __cdecl beginlogoffThread( LPVOID )//执行注销任务线程
{
	//if(AfxMessageBox(" 确定注销计算机? ",MB_OKCANCEL)==IDOK)
		::ExitWindowsEx(EWX_LOGOFF,0);
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////start OnTimer
void CClockDlg::OnTimer(UINT nIDEvent) //计时器
{
	// TODO: Add your message handler code here and/or call default
	SYSTEMTIME curtime;
	GetLocalTime(&curtime);//获得当前时间

	if(nIDEvent==3)//最小化启动第5步
	{
		if(move_rect.top!=0 && move_rect.bottom!=0 && move_rect.left!=0 && move_rect.right!=0)
		{
			MoveWindow(&move_rect);
			ShowWindow(SW_HIDE);
			ShowWindow(SW_SHOW);
		}
		else
		{
			CenterWindow();
		}
		
		KillTimer(nIDEvent);
	}
	
	if(nIDEvent==2)//最小化启动第3步
	{
		ShowWindow(SW_HIDE);
		KillTimer(nIDEvent);
	}
	
	if(nIDEvent==1)//刷新时间
	{
		int h=curtime.wHour;
		int f=curtime.wMinute;
		int s=curtime.wSecond;
		if (m_nSecond==s)
		{
			return;
		}
		else
		{
			m_nSecond=s;
		}
		
		m_ns1=m_nSecond/10;
		m_ns2=m_nSecond%10;
		
		m_nh1=h/10;
		m_nh2=h%10;
			
		m_nm1=f/10;
		m_nm2=f%10;
		//只重绘时钟区域
		Invalidate(FALSE);



	//	CString strTemp;
	//	对话框上显示时间
	//	strTemp.Format("%02i:%02i:%02i",curtime.wHour,curtime.wMinute,curtime.wSecond);
	//	GetDlgItem(IDC_NOW_TIME)->SetWindowText(strTemp);
	}
	

	if(nIDEvent==8)//启用整点报时
	{
		if(curtime.wMinute==0 && curtime.wSecond==0)//判断分是否为0,秒是否为0,如果是则继续判断为哪种方式报时
		{
			
			char files_path[512];
			GetModuleFileName(NULL,files_path,512);
			char *p = strrchr(files_path,'\\');
			p[1]='\0';
			strcat(files_path,"Settings.ini");

			int m_standardbell=GetPrivateProfileInt("Sound","m_standardbell",0,files_path);
		
			char buffer[256];
			GetPrivateProfileString("Sound","m_strPath","",buffer,256,files_path);

			switch(m_standardbell)
			{
				case 0://使用标准铃声
						_beginthread(beginstandardThreadFunc,0,NULL);
						break;
				case 1://使用自定义铃声
						_beginthread(beginSoundThreadFunc,0,NULL);	
						break;
				case 2://使用语言报时
						_beginthread(beginstandardThreadFunc,0,NULL);//暂且用一下标准铃声
						break;
			}
			
		}
	
	}
	
	if(nIDEvent>=10 && nIDEvent<100)//"设置窗口"定时器索引号定为10-100;
	{
		int i=nIDEvent-10;

		int year = ((MISSION*)m_MissionArray[i])->set_date_year;
		int month = ((MISSION*)m_MissionArray[i])->set_date_month;
		int day = ((MISSION*)m_MissionArray[i])->set_date_day;
		int hour = ((MISSION*)m_MissionArray[i])->set_time_hour;
		int minute = ((MISSION*)m_MissionArray[i])->set_time_minute;
		int week = ((MISSION*)m_MissionArray[i])->set_date_week;
		int cycle = (((MISSION*)m_MissionArray[i])->set_time_cycletime)*60;//将分钟换算成秒计算
		switch(((MISSION*)m_MissionArray[i])->set_mode)
		{
//case0///////////////////////////////////////////			
		case 0://只提醒一次
			{
				//判断设置的年月日是否与当前的年月日相等,如果相等,继续判断小时是否过时,
				if(year==curtime.wYear && month==curtime.wMonth && day==curtime.wDay && hour==curtime.wHour)
				{
					//如果有启用闹铃任务,执行前有提示,则执行提示信息线程
					if(((MISSION*)m_MissionArray[i])->choice_usingaction==1 ) 
					{
						if(((MISSION*)m_MissionArray[i])->choice_actionhint==1)//执行前有提示
						{
							int actiontime=((MISSION*)m_MissionArray[i])->choice_actiontime;
							int min_minute;
							min_minute=minute;
							min_minute=min_minute-1;//有提示,则分钟减1
							int second=60-actiontime;
							if(curtime.wMinute==min_minute && curtime.wSecond==second)//当时间与秒到时后,运行提示线程
							{							
								int actionmode=((MISSION*)m_MissionArray[i])->choice_actionmode;
								_beginthread(beginactionThread,0,(void*)actionmode);
							
							}
							
						}

						else
						{
						}
							
						
						if(minute==curtime.wMinute)//如果分=当前分
						{
						
							//如果设置有启用媒体音乐,则启用播放wav文件音乐线程
							if(((MISSION*)m_MissionArray[i])->choice_usingsound==1)
							{
								_beginthread(beginwavThread,0,(void*)(((MISSION*)m_MissionArray[i])->choice_wavpath));
							
							}
							else//否则启用默认标准铃声线程
							{
								_beginthread(beginThreadFunc,0,NULL);
							}
							switch(((MISSION*)m_MissionArray[i])->choice_actionmode)//执行任务
							{
							case 0://执行文件
								_beginthread(begindofileThread,0,(void*)(((MISSION*)m_MissionArray[i])->choice_actionpath));
								break;
							case 1://关闭计算机
								_beginthread(beginshutdownThread,0,NULL);
								break;
							case 2://待机
								_beginthread(beginpoweroffThread,0,NULL);
								break;
							case 3://重启
								_beginthread(beginrebootThread,0,NULL);
								break;
							case 4://注销
								_beginthread(beginlogoffThread,0,NULL);
								break;
							}

							CAwakeDlg* pDialog= new CAwakeDlg();//创建一个CAwakeDlg对话框
							if(pDialog != NULL)
							{
								//将数据传递给对话框。
								memcpy((void*)&(pDialog->m_mission),(void*)((MISSION*)m_MissionArray[i]),sizeof(MISSION));
								
								
								pDialog->number=i;//将第几项数据传给对话框,
								pDialog->pclockdlg=this;
								//创建对话框
								BOOL ret = pDialog->Create(IDD_AWAKE_DIALOG,this);
								//显示对话框
								pDialog->ShowWindow(SW_SHOW);
								//把窗口句柄赋给hWin,
								((MISSION*)m_MissionArray[i])->hWin= (long)pDialog;
								KillTimer(nIDEvent);
							}
						
						}//end minute if
						
					
					}//end 启用闹铃任务 if


					else//如果没有启用任务,
					{
						if(minute==curtime.wMinute)//如果分=当前分
						{
						
							//如果设置有启用媒体音乐,则启用播放wav文件音乐线程
							if(((MISSION*)m_MissionArray[i])->choice_usingsound==1)
							{
								_beginthread(beginwavThread,0,(void*)(((MISSION*)m_MissionArray[i])->choice_wavpath));
							
							}
							else//否则启用默认标准铃声线程
							{
								_beginthread(beginThreadFunc,0,NULL);
							}

							CAwakeDlg* pDialog= new CAwakeDlg();//创建一个CAwakeDlg对话框
							if(pDialog != NULL)
							{
								//将数据传递给对话框。
								memcpy((void*)&(pDialog->m_mission),(void*)((MISSION*)m_MissionArray[i]),sizeof(MISSION));
								
								
								pDialog->number=i;//将第几项数据传给对话框,
								pDialog->pclockdlg=this;

								//创建对话框
								BOOL ret = pDialog->Create(IDD_AWAKE_DIALOG,this);
								//显示对话框
								pDialog->ShowWindow(SW_SHOW);
								//把窗口句柄赋给hWin,
								((MISSION*)m_MissionArray[i])->hWin= (long)pDialog;
								KillTimer(nIDEvent);
							}
						
						}//end minute if

					}
				}//end year month day hour if
				break;
			}
		case 1://循环提醒
			{
				m_cyclecount++;
				//如果有启用闹铃任务,执行前有提示,则执行提示信息线程
		 		if(((MISSION*)m_MissionArray[i])->choice_usingaction==1 ) 
				{
					if(((MISSION*)m_MissionArray[i])->choice_actionhint==1)//执行前有提示
					{
						int actiontime=((MISSION*)m_MissionArray[i])->choice_actiontime;
						int l_cycle;
						l_cycle=cycle-actiontime;
						if(m_cyclecount==l_cycle)//当时间与秒到时后,运行提示线程
						{							
							int actionmode=((MISSION*)m_MissionArray[i])->choice_actionmode;
							_beginthread(beginactionThread,0,(void*)actionmode);
						}
					}
					else
					{
					}
					if(m_cyclecount==cycle)
					{
						//如果设置有启用媒体音乐,则启用播放wav文件音乐线程
						if(((MISSION*)m_MissionArray[i])->choice_usingsound==1)
						{
							_beginthread(beginwavThread,0,(void*)(((MISSION*)m_MissionArray[i])->choice_wavpath));
						
						}
						else//否则启用默认标准铃声线程

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?