Krisz
Krisz

Reputation: 743

Monitor status query in c#

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.

Upvotes: 0

Views: 416

Answers (1)

Jim Mischel
Jim Mischel

Reputation: 134125

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

Upvotes: 1

Related Questions