• 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

    • hi thanks for input. those two modems are supported  thankfully. i really am leaning towards the b12 though since there's a few complaints of the cm3000 and its chipset (drops and whatnot), although those might just be vocal minority complaints.
    • A PDF or text file is all you need for Copilot to generate a PowerPoint for you in seconds by Usama Jawad Assistive AI is all the rage these days. In the past few hours alone, we have witnessed AI summaries on YouTube, context-aware text generation capabilities in Google Sheets, Gemini tools on the cloud, the availability of additional AI models with smarter defaults in Visual Studio, and chat summarization on WhatsApp. Now, Microsoft has detailed an upcoming Copilot capability in PowerPoint. Microsoft has outlined a streamlined approach to creating PowerPoint presentations within seconds using a PDF or text file, and a natural language prompt as input. Previously, you could just ground Word files for this purpose, so support for additional formats is certainly welcome. In order to generate a slide deck using Copilot, you can simply open a PowerPoint presentation in the platform of your choice and click the Copilot button. In the resulting Copilot pane, select the "Create a presentation from a file" option, which allows you to type in the name of a PDF or text file stored on the cloud or browse for files in OneDrive or SharePoint manually using the "Attach cloud files" button. You can then refine your prompt by adding details about items that you'd like Copilot to focus on and click the "Send" button. This will generate the slide deck which you can rearrange or add more topics to, before you create the final version of the presentation. Of course, this is a particularly useful feature in many scenarios including those which require you to create a slide deck based on some detailed documentation that you have already completed. That said, it's obviously worth vetting any content that Copilot creates, since data inaccuracy is still a concern when it comes to AI models. This feature is available to Insiders across PowerPoint for web, Windows, and Mac for the following versions: Windows: Version 2409 (Build 18025.20000) or later Mac: Version 16.88 (Build 24082514) or later That said, you do need an Enterprise Copilot license in order to utilize the AI assistant in this way.
    • >For instance, if you are watching a tutorial, you could ask for a summary or even for a quiz on the main points. At that point, the content creator should get their cut, Google. Instead: >If people are getting everything they need from a summary without ever clicking through, there's not much reason to watch the actual video. That means fewer views, less watch time, and less money. Do content creators get to write their own summaries or turn off Google's summary content thievery? Somehow I doubt it...
    • I know with my isp they will only support certain models. You might want to check with them,
  • Recent Achievements

    • One Month Later
      jfam earned a badge
      One Month Later
    • First Post
      TheRingmaster earned a badge
      First Post
    • Conversation Starter
      Kavin25 earned a badge
      Conversation Starter
    • One Month Later
      Leonard grant earned a badge
      One Month Later
    • Week One Done
      pcdoctorsnet earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      552
    2. 2
      ATLien_0
      205
    3. 3
      +FloatingFatMan
      180
    4. 4
      Michael Scrip
      147
    5. 5
      Som
      119
  • Tell a friend

    Love Neowin? Tell a friend!