一个Windows下检测系统空闲时间的类
//head file
#ifndef IDLE_CHECK
#define IDLE_CHECK
/*
*说明:这个类作为检测系统的无输入操作是否达到预定时间,类似屏保时间检测
*函数:setIdleInterval 设置要检测等待的时间
参数为等待的秒数
* setIdleMsg 设置要发送的消息
参数为消息
* Run 启动检测线程
参数为窗口函数的句柄
* Stop 停止检测线程
* getStatus获得线程状态
* Run 启动检测线程
参数为void WINAPI callback()模型的回调函数
*/
#pragma once
#define THIS_INTERVAL_T 20
#define THIS_MSG WM_USER+500
typedef void (WINAPI *PFCALLBACK)() ;
class CIdleCheck
{
public:
CIdleCheck(void);
~CIdleCheck(void);
void setIdleInterval(int interval/*seconds*/);
void setIdleMsg(long msgtype/*as WM_USER+2*/);
bool Run(HWND hwnd); //start the check thread
void Stop(); //stop the check thread
bool getStatus(); //this status
bool Run(PFCALLBACK func);
private:
bool status;
HANDLE tHandle;
};
#endif
////////////////////////////////////////////////////////////////////////
//cpp file
#include "StdAfx.h"
#include "IdleCheck.h"
#include <afxwin.h>
int iInterval=0;
long lMsgType=0;
bool bStop=false;
bool bHasSend=false;
static DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
HWND hwnd=(HWND)lpParameter;
while (!bStop)
{
LASTINPUTINFO info;
info.cbSize=sizeof(info);
if (GetLastInputInfo(&info))
{
if ((GetTickCount()-info.dwTime) > iInterval*1000)
{
if (!bHasSend)
{
bHasSend = true;
SendMessage(hwnd,lMsgType,NULL,NULL);
}
}
else
bHasSend =false;
}
Sleep(1000);
}
return 0;
}
static DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{
PFCALLBACK func=(PFCALLBACK)lpParameter;
while (!bStop)
{
LASTINPUTINFO info;
info.cbSize=sizeof(info);
if (GetLastInputInfo(&info))
{
if ((GetTickCount()-info.dwTime) > iInterval*1000)
{
if (!bHasSend)
{
bHasSend = true;
//SendMessage(hwnd,lMsgType,NULL,NULL);
func();
}
}
else
bHasSend =false;
}
Sleep(1000);
}
return 0;
}
CIdleCheck::CIdleCheck(void)
{
status=false;
iInterval=THIS_INTERVAL_T;
lMsgType = THIS_MSG;
}
CIdleCheck::~CIdleCheck(void)
{
if (status)
{
Stop();
CloseHandle(tHandle);
}
}
void CIdleCheck::setIdleInterval( int interval )
{
if (!status)
iInterval = interval;
}
void CIdleCheck::setIdleMsg( long msgtype )
{
if (!status)
lMsgType = msgtype;
}
bool CIdleCheck::Run(HWND hwnd)
{
DWORD dwThreadID=0;
tHandle = CreateThread(NULL,0,ThreadProc,hwnd,0,&dwThreadID);
if (tHandle==NULL)
{
status =false;
}
else
{
status =true;
bStop=false;
}
return status;
}
bool CIdleCheck::Run( PFCALLBACK func )
{
DWORD dwThreadID=0;
tHandle = CreateThread(NULL,0,ThreadProc2,func,0,&dwThreadID);
if (tHandle==NULL)
{
status =false;
}
else
{
status =true;
bStop=false;
}
return status;
}
void CIdleCheck::Stop()
{
bStop =true;
}
bool CIdleCheck::getStatus()
{
return status;
}
#ifndef IDLE_CHECK
#define IDLE_CHECK
/*
*李浩
*mailto:lihaoyxj@gmail.com
*说明:这个类作为检测系统的无输入操作是否达到预定时间,类似屏保时间检测
*函数:setIdleInterval 设置要检测等待的时间
参数为等待的秒数
* setIdleMsg 设置要发送的消息
参数为消息
* Run 启动检测线程
参数为窗口函数的句柄
* Stop 停止检测线程
* getStatus获得线程状态
* Run 启动检测线程
参数为void WINAPI callback()模型的回调函数
*/
#pragma once
#define THIS_INTERVAL_T 20
#define THIS_MSG WM_USER+500
typedef void (WINAPI *PFCALLBACK)() ;
class CIdleCheck
{
public:
CIdleCheck(void);
~CIdleCheck(void);
void setIdleInterval(int interval/*seconds*/);
void setIdleMsg(long msgtype/*as WM_USER+2*/);
bool Run(HWND hwnd); //start the check thread
void Stop(); //stop the check thread
bool getStatus(); //this status
bool Run(PFCALLBACK func);
private:
bool status;
HANDLE tHandle;
};
#endif
////////////////////////////////////////////////////////////////////////
//cpp file
#include "StdAfx.h"
#include "IdleCheck.h"
#include <afxwin.h>
int iInterval=0;
long lMsgType=0;
bool bStop=false;
bool bHasSend=false;
static DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
HWND hwnd=(HWND)lpParameter;
while (!bStop)
{
LASTINPUTINFO info;
info.cbSize=sizeof(info);
if (GetLastInputInfo(&info))
{
if ((GetTickCount()-info.dwTime) > iInterval*1000)
{
if (!bHasSend)
{
bHasSend = true;
SendMessage(hwnd,lMsgType,NULL,NULL);
}
}
else
bHasSend =false;
}
Sleep(1000);
}
return 0;
}
static DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{
PFCALLBACK func=(PFCALLBACK)lpParameter;
while (!bStop)
{
LASTINPUTINFO info;
info.cbSize=sizeof(info);
if (GetLastInputInfo(&info))
{
if ((GetTickCount()-info.dwTime) > iInterval*1000)
{
if (!bHasSend)
{
bHasSend = true;
//SendMessage(hwnd,lMsgType,NULL,NULL);
func();
}
}
else
bHasSend =false;
}
Sleep(1000);
}
return 0;
}
CIdleCheck::CIdleCheck(void)
{
status=false;
iInterval=THIS_INTERVAL_T;
lMsgType = THIS_MSG;
}
CIdleCheck::~CIdleCheck(void)
{
if (status)
{
Stop();
CloseHandle(tHandle);
}
}
void CIdleCheck::setIdleInterval( int interval )
{
if (!status)
iInterval = interval;
}
void CIdleCheck::setIdleMsg( long msgtype )
{
if (!status)
lMsgType = msgtype;
}
bool CIdleCheck::Run(HWND hwnd)
{
DWORD dwThreadID=0;
tHandle = CreateThread(NULL,0,ThreadProc,hwnd,0,&dwThreadID);
if (tHandle==NULL)
{
status =false;
}
else
{
status =true;
bStop=false;
}
return status;
}
bool CIdleCheck::Run( PFCALLBACK func )
{
DWORD dwThreadID=0;
tHandle = CreateThread(NULL,0,ThreadProc2,func,0,&dwThreadID);
if (tHandle==NULL)
{
status =false;
}
else
{
status =true;
bStop=false;
}
return status;
}
void CIdleCheck::Stop()
{
bStop =true;
}
bool CIdleCheck::getStatus()
{
return status;
}