想每隔一段时间执行一件事的的时候,你能够使用它windows
SetTimer 构造函数:函数
UINT_PTR SetTimer( HWND hWnd, // 窗口句柄 UINT_PTR nIDEvent, // 定时器ID,多个定时器时,能够经过该ID判断是哪一个定时器 UINT nElapse, // 时间间隔,单位为毫秒 TIMERPROC lpTimerFunc // 回调函数);
返回值:1. 函数成功,第一个参数参数为0,则返回新创建的时钟编号,能够把这个时钟编号传递给KillTimer来销毁时钟. 2. 函数成功,hWnd参数为非0,则返回一个非零的整数,能够把这个非零的整数传递给KillTimer来销毁时钟. 3. 函数失败,返回值是零.若想得到更多的错误信息,调用GetLastError函数.spa
在MFC程序中SetTimer被封装在CWnd类中,调用就不用指定窗口句柄了。因而SetTimer函数的原型变为:code
UINT SetTimer(UINT nIDEvent,UINT nElapse,void(CALLBACK EXPORT *lpfnTimer)(HWND,UINT ,YINT ,DWORD))
高亮一个线路或者设备,为了使它看起来明显,能够用俩种颜色变换闪烁。对象
.h里面 //使当前显示对象闪烁 afx_msg void OnTimer(UINT_PTR nIDEvent); cpp里面 1.绑定事件响应 ON_WM_TIMER() 2.对话框初始化函数 BOOL CXXXDlg::OnInitDialog() { __super::OnInitDialog(); m_uTimer = SetTimer(1,1000,NULL); return TRUE; } 3.事件响应 void CXXXDlg::OnTimer(UINT_PTR nIDEvent) { CQKQuery mQky(m_pDataBase); mQky.LockDoc(); std::map<AcDbObjectId, int>::iterator iter = m_mapOrignColor.begin(); for (; iter != m_mapOrignColor.end(); ++iter) { AcDbObjectId ObjectId = iter->first; if (ObjectId.isNull() || !ObjectId.isValid()) continue; int nCurOrignColor = -1; mQky.GetObjectColorI(ObjectId, nCurOrignColor); //设置为反色 mQky.SetObjectColorI(ObjectId, getReverseColorIndex(nCurOrignColor)); } mQky.UnLockDoc(); } //----------------------------------------------------------------------------------- // 函 数 名:GetObjectColorI // 函数介绍:设置实体颜色 // 参数介绍:const AcDbObjectId& mId // int& nColorIndex // 返 回 值:bool true:成功,false:失败 //----------------------------------------------------------------------------------- bool CQKQuery::GetObjectColorI(const AcDbObjectId& mId, int& nColorIndex) { AcDbEntity *pEnt = NULL; HHVerifyErr2(acdbOpenObject(pEnt, mId, AcDb::kForWrite), return false;); nColorIndex = pEnt->colorIndex(); HHVerifyErr(pEnt->close()); return true; } //----------------------------------------------------------------------------------- // 函 数 名:SetObjectColorI // 函数介绍:设置某ID的颜色 // 参数介绍:const AcDbObjectId& mId object id // int nColorIndex 颜色序号 // 返 回 值:bool //----------------------------------------------------------------------------------- bool CQKQuery::SetObjectColorI(const AcDbObjectId& mId, int nColorIndex) { AcDbEntity *pEnt = NULL; HHVerifyErr2(acdbOpenObject(pEnt, mId, AcDb::kForWrite), return false;); HHVerify(SetObjectColorIP(pEnt, nColorIndex)); HHVerifyErr(pEnt->close()); return true; } //! 获取反色索引值 int CXXXGlg::getReverseColorIndex(int nOrgIndex) { //当原色为白时,直接返回红 if (256 == nOrgIndex) return 1; CAcUiColorComboBox acColor; // 获取原始索引值的RGB值 if (nOrgIndex == 0 || nOrgIndex == 255) nOrgIndex = 1; COLORREF dwOld = acColor.GetColorFromIndex(nOrgIndex); COLORREF dwReverse = dwOld ^ RGB(255,255,255); // RGB转颜色索引值 int nReverseIndex = acColor.GetColorIndex(dwReverse); COLORREF dwOld1 = acColor.GetColorFromIndex(nReverseIndex); if (nReverseIndex == 0) nReverseIndex = nOrgIndex/2; return nReverseIndex; }
SetTimer 使用的是时间中断响应计时,windows的时间中断每1/18秒触发一次,因此Timer最低精度约在55ms,低于这个时间则精度不够。 从启动系统开始,经历的时间。单位为毫秒。最大值约为49.7天,若系统连续运行超过49.7天,dwTime的值会回到0从新开始计算。索引
有时可能达不到精准度,这时能够使用timeSetEvent。事件