Domanda

I'm trying to detect when the computer enters power-save mode. Problem is, this program has to run on both Windows XP and 7. RegisterPowerSettingNotification only works for Vista and newer, so that's not an option. I also tried using SystemParametersInfo with the SPI_GETSCREENSAVERRUNNING but that doesn't work for the power-save mode, which is what the computer is actually set for. Any other suggestions?

È stato utile?

Soluzione

To answer my own question, grabbing the screensaver timeout and the last user input, and comparing the two appears to be the best way:

int screenTimeout;
SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, NULL, &screenTimeout, SPIF_UPDATEINIFILE);
LASTINPUTINFO lastInput;
lastInput.cbSize = sizeof(LASTINPUTINFO);
GetLastInputInfo(&lastInput);
DWORD ticks = GetTickCount();
int lastInputTime = (ticks-lastInput.dwTime)/1000;

GetLastInputInfo returns the number of ticks since the last user input. According to MSDN, the ticks occur between 10 to 16 ms, so this isn't a precise way of measuring time, but it's good enough for my purposes.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top