我们可以利用定时器进行一些操作:如定时关机,关闭显示器等等;下面介绍一些简单的用法:
1.显示器api
关闭显示器:SendMessage(hwnd,WM_SYSCOMMAND,SC_MONITORPOWER,2);
低电状态:SendMessage(hwnd,WM_SYSCOMMAND,SC_MONITORPOWER,1);
打开显示器 SendMessage(hwnd,WM_SYSCOMMAND,SC_MONITORPOWER,-1);
例子:
void CALLBACK MyTimerProc(HWND hwnd,UINT message,UINT iTimerID,DWORD dwTime)
{
SYSTEMTIME stLocal;
GetLocalTime(&stLocal);
TCHAR strTime[256];
wsprintf(strTime,"%i年%i月%i日,%i:%i:%i",stLocal.wYear,stLocal.wMonth,stLocal.wDay,stLocal.wHour,stLocal.wMinute,stLocal.wSecond);
SetDlgItemText(hwnd,IDC_EDITTIMER,strTime);
if(1==stLocal.wHour && 2==stLocal.wMinute)
SendMessage(hwnd,WM_SYSCOMMAND,SC_MONITORPOWER,2);
}
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
SetTimer(hwnd,0,1000,MyTimerProc);
return TRUE;
}
2.计算机api
ExitWindowsEx(EWX_LOGOFF, 0); // 注销
ExitWindowsEx(EWX_REBOOT, 0); // 重启
ExitWindowsEx(EWX_SHUTDOWN, 0); // 关机
ExitWindowsEx(EWX_REBOOT, 0); // 重启
ExitWindowsEx(EWX_SHUTDOWN, 0); // 关机
例子:
void CALLBACK MyTimerProc(HWND hwnd,UINT message,UINT iTimerID,DWORD dwTime) { SYSTEMTIME stLocal; GetLocalTime(&stLocal); TCHAR strTime[256]; wsprintf(strTime,"%i年%i月%i日,%i:%i:%i",stLocal.wYear,stLocal.wMonth,stLocal.wDay,stLocal.wHour,stLocal.wMinute,stLocal.wSecond); SetDlgItemText(hwnd,IDC_EDITTIMER,strTime); HANDLE hToken; TOKEN_PRIVILEGES tkp; DWORD dwVersion; dwVersion = GetVersion(); // 得到WINDOWS NT或Win32的版本号 if(dwVersion < 0x80000000) // 用于判断WIN系列,从而设置相应的权限 { OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken); LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0); } else { ExitWindowsEx(EWX_FORCE | EWX_SHUTDOWN, 0); } } BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) { SetTimer(hwnd,0,1000,MyTimerProc); return TRUE; }