HOW TO: Determine If Screen Saver Is Start to Run by Using C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like my application to execute a function if and when the screen saver starts. I have been searching for a way for my program to know that the screen saver has started but the only thing I could find was how to do it in C++ & Visual Basic 6.

The question is How can I determine if screen saver is start to run by using C#

Thanks in advance,
Yan
 
This is one approach, but it only works if your application is the
foreground application when the screen saver starts.

const int WM_SYSCOMMAND = 0x0112;
const int SC_SCREENSAVE = 0xF140;

protected override void WndProc( ref Message m )
{
if( m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_SCREENSAVE )
{
// your code here
}
base.WndProc( ref m );
}
 
Yan said:
I would like my application to execute a function if and when the screen
saver starts. I have been searching for a way for my program to know that
the screen saver has started but the only thing I could find was how to do
it in C++ & Visual Basic 6.
The question is How can I determine if screen saver is start to run by
using C#.

Try this:

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SystemParametersInfo(int uAction,
int uParam, ref int lpvParam, int fuWinIni);

const int SPI_GETSCREENSAVERRUNNING = 114;
int screenSaverRunning = -1;

// is the screen saver running?

int ok = SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref
screenSaverRunning, 0);

if (ok == 0)
{
Console.WriteLine("Call to SystemParametersInfo failed.");
}

if (screenSaverRunning != 0)
{
// screen saver is running

}
 
Back
Top