• 0

[C#] Enable/ disable screen saver


Question

Hello,

I'm trying to use SystemParametersInfo in order to disable/ enable the screen saver. I try this:

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

private const int HWND_BROADCAST = 65535;
  private const int WM_SYSCOMMAND = 274;
  private const int SC_SCREENSAVE = 61760;
  private const int SPI_GETSCREENSAVETIMEOUT = 14;
 private const int SPI_SETSCREENSAVETIMEOUT = 15;
 private const int SPIF_UPDATEINIFILE = 1;
 private const int SPIF_SENDWININICHANGE = 2;
 private const int SPI_SETSCREENSAVEACTIVE = 17;
private const int SPI_GETSCREENSAVEACTIVE = 16;

bool IsActive;

SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, IsActive, 0);
// disable the screen saver if it is enabled
if(IsActive)
{
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, false, 0, 0);
}

But it doesn't work :( I'm not sure that the SystemParametersInfo is OK, and I don't know how to use SPI_GETSCREENSAVEACTIVE and SPI_SETSCREENSAVEACTIVE.

Can anybody please tell me how to use it ?

Link to comment
https://www.neowin.net/forum/topic/298659-c-enable-disable-screen-saver/
Share on other sites

9 answers to this question

Recommended Posts

  • 0

I use this code but in VB.net, try to convert it to C# ;)

Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As Integer, ByVal fuWinIni As Integer) As Integer
    Public Function ToggleScreenSaverActive(ByRef Active As Boolean) As Boolean
        Dim lActiveFlag As Integer
        Dim retvaL As Integer

        lActiveFlag = IIf(Active, 1, 0)
        retvaL = SystemParametersInfo(17, lActiveFlag, 0, 0)
        ToggleScreenSaverActive = retvaL > 0
    End Function


    ' On
        ToggleScreenSaverActive(True)


    ' Off
        ToggleScreenSaverActive(False)

  • 0
  yyy said:
I know how to do that - it works fine. This is not the problem.

My problem is that I want to GET the screen saver state so I'll know whether the screen saver is active or not.

For that I need to use SPI_GETSCREENSAVEACTIVE

Do you know how to use that ?

585642321[/snapback]

ok change it to the line :

Private Const SPI_GETSCREENSAVEACTIVE As Long = &H10

and give a try

  • 0

No - I know that too. Nevermind - I've found the solution, I think.

I did it that way:

[DllImport("user32", CharSet=CharSet.Auto)]
 ?private static extern bool SystemParametersInfo(int uAction, int uParam, ref bool lpvParam, int fuWinIni

SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, ref IsActive, 0);

I had to add the ref before the bool to cause it get the value.

Thanks anyway :DD

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.