说明
写个定时关机程序自用,界面简单实用,最终界面如下
搭建界面
新建一个MFC对话框应用程序,拖几个控件过来,界面如下:
然后给下拉列表框,复选按钮绑定变量
,以方便进行操作。
CComboBox m_com_hour; CComboBox m_com_minute; CComboBox m_com_second;
功能实现
先实现简单关机功能
,不通过文件读写进行关键。在选择过时间后,确定关机后按时关机。
添加更改事件
给下拉列表框添加更改事件,直接右键控件
->选择添加事件处理程序
,选择CBN_SELCHANGE
然后编辑代码,添加如下:
void CAutoShutDownDlg::OnCbnSelchangeCombo1()//时 { // TODO: 在此添加控件通知处理程序代码 int sel = 0; sel = m_com_hour.GetCurSel();//这个是获取当前鼠标选择的时间的索引,从零开始的 hour = sel;//hour是添加的成员变量,还有minute和second,这就是关机时间 }
还有其他的分钟
和秒
类似这个,这里不多说了,代码如下
void CAutoShutDownDlg::OnCbnSelchangeCombo2()//分 { // TODO: 在此添加控件通知处理程序代码 int sel = 0; sel = m_com_minute.GetCurSel(); minute = sel; } void CAutoShutDownDlg::OnCbnSelchangeCombo3()//秒 { // TODO: 在此添加控件通知处理程序代码 int sel = 0; sel = m_com_second.GetCurSel(); second = sel; }
现在可以获取程序中选择的时间了,然后就是获取这些信息,在单击过确定按钮后弹出确认窗口,确认关机时间。
在资源窗口直接双击确认按钮
进入按钮按下消息响应程序
void CAutoShutDownDlg::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 //复选框是否选中,这个处理是否添加开机启动项,后面再说这个 UINT ischecked = IsDlgButtonChecked(IDC_CHECK1); //获取选择的时间 hour = m_com_hour.GetCurSel(); minute = m_com_minute.GetCurSel(); second = m_com_second.GetCurSel(); //数据进行格式化处理 CString str = ""; str.Format("%2d时%2d分%2d秒", hour, minute, second); // GetDlgItem(IDC_STATIC_NOW)->SetWindowText(str); int resoult = MessageBox("确定要在"+str+"关机吗?","是否关机?",MB_OKCANCEL); if (resoult == IDOK) { //是否添加到启动项,这里用来检查是否写入到开机启动项,直接写入注册表,这个后面再说 AutoStart(ischecked); //更新程序窗口中显示的关机时间 GetDlgItem(IDC_STATIC_SHUT)->SetWindowText(str); FILE *file; int err; err = fopen_s(&file, "c:\\ShutDownTime.txt", "w+"); if (!err) { err = fprintf_s(file, "%02d%02d%02d",hour, minute, second); if (err == EOF) { MessageBox("请重试!"); } } else { MessageBox("文件打开错误"); } if (fclose(file) != 0) { MessageBox("文件关闭错误"); } } }
现在来看看OnInitDialog()
函数里做了什么,这里主要用来读取上次保存的关机时间
//读取软件设置的默认时间 FILE *file; errno_t err; err = fopen_s(&file, "c:\\ShutDownTime.txt", "a+"); if (!err) { fscanf_s(file, "%02d%02d%02d",&hour, &minute, &second); } else { MessageBox("文件打开错误"); } if (fclose(file) != 0) { MessageBox("文件关闭错误"); } str = ""; str.Format("%2d时%2d分%2d秒", hour, minute, second); GetDlgItem(IDC_STATIC_SHUT)->SetWindowText(str); SetTimer(0,100,NULL); //定时刷新当前时间 SetTimer(1, 1000, NULL); //定时检查是否到达关机时间
这里主要用了两个定时器来检查关机时间。 还有一个默认显示的关机时间,当第一次打开程序时,并没有关机时间,这里默认现在当前的时间,关机时间会在确认关机后更新。
GetTime();//获取当前时间,自定义的成员函数 CString str = ""; str.Format("%2d时%2d分%2d秒", hour, minute, second); GetDlgItem(IDC_STATIC_NOW)->SetWindowText(str); int temp = 0; int i = 0; //设置默认显示的小时 str = ""; for (i = 0; i <= 23; i++) { str.Format("%2d时", i); m_com_hour.AddString(str); } m_com_hour.SetCurSel(hour); //设置默认显示的分钟 str = ""; for (i = 0; i <= 59; i++) { str.Format("%2d分", i); m_com_minute.AddString(str); } m_com_minute.SetCurSel(minute); //设置默认显示的妙 str = ""; for (i = 0; i <= 59; i++) { str.Format("%2d秒", i); m_com_second.AddString(str); } m_com_second.SetCurSel(second);
其中GetTime()
为自定义的成员函数,用于获取当前时间
void CAutoShutDownDlg::GetTime() { CTime time1 = CTime::GetCurrentTime();//获取系统当前时间 hour = time1.GetHour(); minute = time1.GetMinute(); second = time1.GetSecond(); }
现在能实现关机了,不过程序没法最小化到任务栏,实在不方便,明天继续研究这个。