0

I am looking for a way to check the monitor status in C# (.net framework 4.5, win8.1).

(I know impossible to check, the user turn off the monitor manually, I do not want this. I want to check the system(the screen saver) turned it off, or not...)

I found some info about SystemEvents.PowerModeChanged, but it is just only about sleep.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Krisz
  • 701
  • 7
  • 23

1 Answers1

1

You can determine if the screen saver is running by calling the SystemParametersInfo function and passing it the SPI_GETSCREENSAVERRUNNING action.

You'll need a managed prototype for the function:

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(
    SPI uiAction, uint uiParam, ref T pvParam, SPIF fWinIni); // T = any type

const int SpiGetScreenSaverRunning = 0x0072;

And to call it:

bool screenSaverRunning;
if (!SystemParametersInfo(SpiGetScreenSaverRunning, 0, ref screenSaverRunning, 0))
{
    // some error occurred
}
// screenSaverRunning will be true if the screen saver is running
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351