• 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.
  • Posts

    • Model of the planet?  Last time I checked, Earth was a "Poisonous craphole dumpsterfire" model but I hear it's going to be recalled and scrapped before long...  
    • Prices have been unchanged for a looooong time, so really, it's time for a new price bracket, no matter how annoying it is. AAA titles just cost too much to develop these days. Personally, though, I tend to play mostly indie titles that are much lower priced and usually are far better games anyway.
    • Personally I don't usually play most games enough to justify paying £75 / $100 for a single game. I recently got a Switch 2 which came bundled with Mario Kart World, due to some discount codes I worked out I paid approx. £30 for Mario Kart World getting it bundled with the console. If I had paid £75 for Mario Kart World I'll be honest I would have been very disappointed with it. The big open world is essentially empty and lacking any real content, the changes to go GP mode mean you only really do one lap in most races... compared to Mario Kart 8 which has pretty much always been available for around £40 you have to ask why the game is almost double the cost? Look at all the content available in a Forza Horizon game compared to Mario Kart. For £30 I feel the game is ok, its not bad, however its not amazing either.
    • It was just a copy pasta from his other replies...
    • I think that the last game I bought on day-one was something like Ghost of Tsushima. I've come to realise that I don't have enough time (or sometimes inclination) to game as much as I used to, and I've got plenty of games to keep me occupied if I do decide to play. So I don't see the need to buy a game when it first releases - I wait until it goes on sale. If I'm really excited about a game and the price is under £50 then I would consider it, but anything over that price point gets ignored until the price comes down.
  • Recent Achievements

    • Apprentice
      Wireless wookie went up a rank
      Apprentice
    • Week One Done
      bukro earned a badge
      Week One Done
    • One Year In
      Wulle earned a badge
      One Year In
    • One Month Later
      Wulle earned a badge
      One Month Later
    • One Month Later
      Simmo3D earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      603
    2. 2
      ATLien_0
      281
    3. 3
      +FloatingFatMan
      179
    4. 4
      Michael Scrip
      151
    5. 5
      Steven P.
      111
  • Tell a friend

    Love Neowin? Tell a friend!