C++--------检测电脑是否休眠过

来源:互联网 发布:手机照片排版软件 编辑:程序博客网 时间:2024/05/16 09:31

前一阵子,产测上需要这么一种功能------检测电脑是否睡眠过,现在记录一下主要的实现代码。

// TODO: 在此添加额外的初始化代码CString strCommandLine = GetCommandLine();strCommandLine.TrimLeft().TrimRight();CString strTimeout;int idx = strCommandLine.Find(_T(" "));if (idx != -1){strTimeout = strCommandLine.Mid(idx+1);strTimeout.TrimLeft().TrimRight();//m_nTimeout = _ttoi(strTimeout);//======================================================//m_nTimeout = 50;}TRACE(_T("m_nTimeout = %d\r\n"),m_nTimeout);SetTimer(TIMER1,TIMER1_INTERVAL,NULL);

LRESULT CPowerMonitorDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam){// TODO: 在此添加专用代码和/或调用基类//TRACE(_T("%04x\r\n"),message);switch (message){case WM_POWERBROADCAST:{TRACE(_T("%04x\r\n"),wParam);//系统睡眠了if(wParam == PBT_APMSUSPEND){m_bAPMSUSPEND = TRUE;}//系统唤醒了else if (wParam == PBT_APMRESUMESUSPEND){m_bAPMRESUMESUSPEND = TRUE;}}break;default:break;}return CDialogEx::WindowProc(message, wParam, lParam);}

void CPowerMonitorDlg::OnTimer(UINT_PTR nIDEvent){// TODO: 在此添加消息处理程序代码和/或调用默认值switch (nIDEvent){case TIMER1:{m_dwCnts++;TRACE(_T("m_dwCnts=%d\r\n"),m_dwCnts);CString strSeconds;strSeconds.Format(_T("%ds"),m_nTimeout-m_dwCnts);//strSeconds.Format(_T("%ds"), 40);GetDlgItem(IDC_STATIC_TIMER)->SetWindowText(strSeconds);//3秒钟后执行//if (m_dwCnts==3)//{//system("shutdown -h");////SetSystemPowerState(false, false);////Sleep(1000*6);//}CString  strTemp;CFile  mFile;DWORD dwAttr = GetFileAttributes(_T("C://test"));//若文件夹不存在,创建文件夹if (dwAttr == 0xFFFFFFFF){CreateDirectory(_T("C://test"), NULL);}else{mFile.Open(_T("C://test//log.txt "), CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);}WORD unicode = 0xFEFF; //这句重要mFile.SeekToBegin();mFile.Write(&unicode, 2); //这句重要    CTime time = CTime::GetCurrentTime(); ///构造CTime对象CString m_strTime = time.Format("%Y-%m-%d %H:%M:%S");if (m_bAPMSUSPEND && m_bAPMRESUMESUSPEND){SetTimer(TIMER2,TIMER2_INTERVAL,NULL);m_bResult = TRUE;KillTimer(TIMER1);mFile.SeekToEnd();mFile.Write(_T("\r\n"), sizeof(_T("\r\n")));strTemp = _T("TestTime:") + m_strTime + _T(" result:pass ");mFile.Write(strTemp, wcslen(strTemp)*sizeof(wchar_t));GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("测试通过!"));}else if(m_dwCnts >= static_cast<DWORD>(m_nTimeout)){SetTimer(TIMER2,TIMER2_INTERVAL,NULL);m_bResult = FALSE;KillTimer(TIMER1);mFile.SeekToEnd();mFile.Write(_T("\r\n"), sizeof(_T("\r\n")));strTemp = _T("TestTime:") + m_strTime + _T(" result:fail ");mFile.Write(strTemp, wcslen(strTemp)*sizeof(wchar_t));GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("测试失败!"));}mFile.Flush();mFile.Close();}break;case TIMER2:{KillTimer(TIMER2);if (m_bResult){exit(0);}else{exit(1);}}default:break;}CDialogEx::OnTimer(nIDEvent);}


0 0
原创粉丝点击