Programmatically prevent Windows screensaver from starting

后端 未结 13 1968
心在旅途
心在旅途 2020-12-03 00:58

Is there a recommended way to prevent the Windows screensaver from starting? The closest thing I\'ve found is this article, but what I would really like to do is just tell

相关标签:
13条回答
  • 2020-12-03 01:41

    AutoHotkey can set SystemParametersInfo(SPI_SETSCREENSAVEACTIVE) with a 1-liner DllCall in script to easily accomplish this with a .ahk script.

    AutoHotkey code to disable Screensaver:

    DllCall("SystemParametersInfo", Int, 17, Int, 0, UInt, NULL, Int, 2)
    

    AutoHotkey code to enable screensaver:

    DllCall("SystemParametersInfo", Int, 17, Int, 1, UInt, NULL, Int, 2)
    

    Reference Forum Threads:

    F13Key - Toggling Screen Saver with SystemParametersInfo
    SKAN - How to Disable Screen Saver Temporarily

    0 讨论(0)
  • 2020-12-03 01:44

    Subtle. The official way to tell Windows that the system is not idle is SetThreadExecutionState. This resets the idle timer, (or turns it off, if you pass ES_CONTINUOUS ). However, even though SetThreadExecutionState resets the idle timer, it does not stop the screensaver!

    0 讨论(0)
  • 2020-12-03 01:45

    This blog post details what you need to do in C++.

    The actual code snippet from the website:

    LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
      switch (uMsg)                  
      {
        case WM_SYSCOMMAND:
        {
          switch (wParam)
          {
            case SC_SCREENSAVE:  
              return 0;
            case SC_MONITORPOWER:
              return 0;      
          }
          break;      
        }
    
        case WM_CLOSE:                
        {
          PostQuitMessage(0);            
          return 0;        
        }
      }
      return DefWindowProc(hWnd,uMsg,wParam,lParam);
    

    }

    0 讨论(0)
  • 2020-12-03 01:46

    Can't believe no one has pointed out the easy and obvious solution:

    #include <windows.h>
    
    void main()
    {
       while(1){
          INPUT input;
          input.type = INPUT_MOUSE;
          input.mi.dx = 1;
          input.mi.dy = 1;
          input.mi.mouseData = 0;
          input.mi.dwFlags = MOUSEEVENTF_MOVE;
          input.mi.time = 0;
          input.mi.dwExtraInfo = 0;
          SendInput( 1, &input, sizeof(input) );
          sleep(60000);
       }
    }
    
    0 讨论(0)
  • 2020-12-03 01:48

    SystemParametersInfo

    Specifically, the SPI_SETSCREENSAVEACTIVE parameter.

    Does this not work? I was surprised that I did not see it here. Note that SetThreadExecutionState will not affect the screen saver at all, just the sleeping of the display.

    0 讨论(0)
  • 2020-12-03 01:51

    From JD Design Freeware - Flipss.exe (download 12kb) is a command line utility that will set SPI_SETSCREENSAVEACTIVE for you.

    "FlipSS.exe -h" to see the current state.
    "FlipSS.exe /on" to set the screensaver on.
    "FlipSS.exe /off" to set the screensaver off.
    
    0 讨论(0)
提交回复
热议问题