以编程方式阻止 Windows 屏幕保护程序启动

发布于 2024-07-13 01:46:18 字数 190 浏览 9 评论 0原文

是否有推荐的方法来阻止 Windows 屏幕保护程序启动? 我发现的最接近的是这篇文章,但我真正喜欢的是要做的只是告诉 Windows 计算机没有空闲,而不是愚弄当前设置的屏幕保护程序值。

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 Windows that the computer isn't idle rather than fooling with the currently set screensaver values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

时光瘦了 2024-07-20 01:46:18

为了进行测试,我将屏幕保护程序设置为 1 分钟并需要密码。

我尝试捕获 SC_SCREENSAVE 并在 VB .Net 中返回 -1。 正如所评论的,它在没有屏幕保护程序密码时有效,但如果屏幕保护程序密码处于活动状态,则失败。 (我在Windows XP 中尝试过)。 我还将其放入计时器的滴答事件中,每 1000 毫秒一次:

Static dir As Integer = 4
Cursor.Position = Cursor.Position + New Size(dir, dir)
dir = -dir

它不起作用。 光标来回晃动,1 分钟后屏幕保护程序会短暂闪烁,然后关闭。 屏幕保护程序只打开一会儿,不足以要求输入密码。 但闪光灯还是很难看。

然后我尝试使用user32.dll的SetCursorPos和GetCursorPos。 您可以在 pinvoke 上查找它们。 与上面的结果相同。

然后我查看了这个问题其他地方提到的“JiggleMouse”的代码。 JiggleMouse 使用 SendInput。 SendInput 有效! 屏幕保护程序不闪烁。 我在计时器内调用了 SendInput,每 50 秒触发一次(略小于屏幕保护程序的最小超时时间 60 秒)。 将鼠标移动 0,0 增量就足够了,没有真正的移动。 这确实有效。 放入 Tick 事件的代码:

Dim i(0) As INPUT
i(0).dwType = INPUT.InputType.INPUT_MOUSE
i(0).mkhi = New MOUSEKEYBDHARDWAREINPUT
i(0).mkhi.mi = New MOUSEINPUT
i(0).mkhi.mi.dx = 0
i(0).mkhi.mi.dy = 0
i(0).mkhi.mi.mouseData = 0
i(0).mkhi.mi.dwFlags = MOUSEINPUT.MouseEventFlags.MOUSEEVENTF_MOVE
i(0).mkhi.mi.time = 0
i(0).mkhi.mi.dwExtraInfo = IntPtr.Zero
SendInput(1, i(0), Marshal.SizeOf(i(0)))

来自 pinvoke.com:

Public Declare Function SendInput Lib "user32" (ByVal nInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer

Public Structure INPUT
    Enum InputType As Integer
        INPUT_MOUSE = 0
        INPUT_KEYBOARD = 1
        INPUT_HARDWARE = 2
    End Enum

    Dim dwType As InputType
    Dim mkhi As MOUSEKEYBDHARDWAREINPUT
End Structure

Public Structure MOUSEINPUT
    Enum MouseEventFlags As Integer
        MOUSEEVENTF_MOVE = &H1
        MOUSEEVENTF_LEFTDOWN = &H2
        MOUSEEVENTF_LEFTUP = &H4
        MOUSEEVENTF_RIGHTDOWN = &H8
        MOUSEEVENTF_RIGHTUP = &H10
        MOUSEEVENTF_MIDDLEDOWN = &H20
        MOUSEEVENTF_MIDDLEUP = &H40
        MOUSEEVENTF_XDOWN = &H80
        MOUSEEVENTF_XUP = &H100
        MOUSEEVENTF_WHEEL = &H800
        MOUSEEVENTF_VIRTUALDESK = &H4000
        MOUSEEVENTF_ABSOLUTE = &H8000
    End Enum

    Dim dx As Integer
    Dim dy As Integer
    Dim mouseData As Integer
    Dim dwFlags As MouseEventFlags
    Dim time As Integer
    Dim dwExtraInfo As IntPtr
End Structure

Public Structure KEYBDINPUT
    Public wVk As Short
    Public wScan As Short
    Public dwFlags As Integer
    Public time As Integer
    Public dwExtraInfo As IntPtr
End Structure

Public Structure HARDWAREINPUT
    Public uMsg As Integer
    Public wParamL As Short
    Public wParamH As Short
End Structure

Const KEYEVENTF_EXTENDEDKEY As UInt32 = &H1
Const KEYEVENTF_KEYUP As UInt32 = &H2
Const KEYEVENTF_UNICODE As UInt32 = &H4
Const KEYEVENTF_SCANCODE As UInt32 = &H8
Const XBUTTON1 As UInt32 = &H1
Const XBUTTON2 As UInt32 = &H2

<StructLayout(LayoutKind.Explicit)> Public Structure MOUSEKEYBDHARDWAREINPUT
    <FieldOffset(0)> Public mi As MOUSEINPUT
    <FieldOffset(0)> Public ki As KEYBDINPUT
    <FieldOffset(0)> Public hi As HARDWAREINPUT
End Structure

For testing, I set the screensaver to 1 minute and required a password.

I tried capturing SC_SCREENSAVE and returning -1 in VB .Net. As commented, it works when there is no screensaver password but fails if the screensaver password is active. (I tried it in Windows XP). I also put this into a Timer's tick event, every 1000 milliseconds:

Static dir As Integer = 4
Cursor.Position = Cursor.Position + New Size(dir, dir)
dir = -dir

It doesn't work. The cursor jiggles back and forth and after 1 minute the screensaver flashes on for a short instance and then turns off. The screensaver turns on for only a moment, not long enough to require a password. But still, the flash is ugly.

Then I tried using user32.dll's SetCursorPos and GetCursorPos. You can look them up at pinvoke. Same result as above.

Then I peeked at the code of "JiggleMouse" mentioned elsewhere in this question. JiggleMouse uses SendInput. SendInput works! No flash of the screensaver. I put a call to SendInput inside of a Timer that triggers every 50 seconds (just less than the minimum screensaver timeout of 60 seconds). It's sufficient to move the mouse by a delta of 0,0, no real movement. That does work. The code to put in the Tick event:

Dim i(0) As INPUT
i(0).dwType = INPUT.InputType.INPUT_MOUSE
i(0).mkhi = New MOUSEKEYBDHARDWAREINPUT
i(0).mkhi.mi = New MOUSEINPUT
i(0).mkhi.mi.dx = 0
i(0).mkhi.mi.dy = 0
i(0).mkhi.mi.mouseData = 0
i(0).mkhi.mi.dwFlags = MOUSEINPUT.MouseEventFlags.MOUSEEVENTF_MOVE
i(0).mkhi.mi.time = 0
i(0).mkhi.mi.dwExtraInfo = IntPtr.Zero
SendInput(1, i(0), Marshal.SizeOf(i(0)))

This comes from pinvoke.com:

Public Declare Function SendInput Lib "user32" (ByVal nInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer

Public Structure INPUT
    Enum InputType As Integer
        INPUT_MOUSE = 0
        INPUT_KEYBOARD = 1
        INPUT_HARDWARE = 2
    End Enum

    Dim dwType As InputType
    Dim mkhi As MOUSEKEYBDHARDWAREINPUT
End Structure

Public Structure MOUSEINPUT
    Enum MouseEventFlags As Integer
        MOUSEEVENTF_MOVE = &H1
        MOUSEEVENTF_LEFTDOWN = &H2
        MOUSEEVENTF_LEFTUP = &H4
        MOUSEEVENTF_RIGHTDOWN = &H8
        MOUSEEVENTF_RIGHTUP = &H10
        MOUSEEVENTF_MIDDLEDOWN = &H20
        MOUSEEVENTF_MIDDLEUP = &H40
        MOUSEEVENTF_XDOWN = &H80
        MOUSEEVENTF_XUP = &H100
        MOUSEEVENTF_WHEEL = &H800
        MOUSEEVENTF_VIRTUALDESK = &H4000
        MOUSEEVENTF_ABSOLUTE = &H8000
    End Enum

    Dim dx As Integer
    Dim dy As Integer
    Dim mouseData As Integer
    Dim dwFlags As MouseEventFlags
    Dim time As Integer
    Dim dwExtraInfo As IntPtr
End Structure

Public Structure KEYBDINPUT
    Public wVk As Short
    Public wScan As Short
    Public dwFlags As Integer
    Public time As Integer
    Public dwExtraInfo As IntPtr
End Structure

Public Structure HARDWAREINPUT
    Public uMsg As Integer
    Public wParamL As Short
    Public wParamH As Short
End Structure

Const KEYEVENTF_EXTENDEDKEY As UInt32 = &H1
Const KEYEVENTF_KEYUP As UInt32 = &H2
Const KEYEVENTF_UNICODE As UInt32 = &H4
Const KEYEVENTF_SCANCODE As UInt32 = &H8
Const XBUTTON1 As UInt32 = &H1
Const XBUTTON2 As UInt32 = &H2

<StructLayout(LayoutKind.Explicit)> Public Structure MOUSEKEYBDHARDWAREINPUT
    <FieldOffset(0)> Public mi As MOUSEINPUT
    <FieldOffset(0)> Public ki As KEYBDINPUT
    <FieldOffset(0)> Public hi As HARDWAREINPUT
End Structure
骄傲 2024-07-20 01:46:18

在 Windows 7+ 中,将电源管理 API 的 PowerSetRequest()PowerRequestDisplayRequired 结合使用

https://msdn.microsoft.com/en-us/library/windows/desktop/dd405534(v=vs.85)。 aspx

在以前版本的 Windows 中,拦截 WM_SYSCOMMAND - SC_SCREENSAVE 消息,如 Eddie Parker 的回答中所述。

In Windows 7+, use the Power Management API's PowerSetRequest() with PowerRequestDisplayRequired

https://msdn.microsoft.com/en-us/library/windows/desktop/dd405534(v=vs.85).aspx

In previous versions of windows, intercept the WM_SYSCOMMAND - SC_SCREENSAVE message as detailed in Eddie Parker's answer.

情绪失控 2024-07-20 01:46:18

微妙的。 告诉Windows系统不空闲的官方方法是SetThreadExecutionState。 这会重置空闲计时器(或者如果您传递 ES_CONTINUOUS 则将其关闭)。 但是,即使 SetThreadExecutionState 重置空闲计时器,它也不会停止屏幕保护程序!

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!

逆流 2024-07-20 01:46:18

我使用 Mouse Jiggler 重置空闲状态。 这绕过了一个组策略,该策略往往会在不合时宜的时间启动我的屏幕保护程序(并锁定机器):当我正在阅读长文档、研究复杂的代码块或在谈话期间说/听/不经常打字时会议。

由于让鼠标每秒对角跳 1px 可能有点烦人,我打算使用 AutoHotKey 编写一个脚本执行基本相同的操作,但仅在配置的键盘/鼠标空闲超时之后,并且可能使用 Shift 键(或 Scroll Lock)而不是鼠标移动。

I use Mouse Jiggler to reset the idle state. This gets around a Group Policy that tends to start my screensaver (and lock the machine) at inopportune times: when I'm reading a long document, studying a complex chunk of code, or talking/listening/not-constantly-typing during a meeting.

As it can be slightly annoying to have the mouse jump 1px diagonally every second, I intend to use AutoHotKey to write a script that does basically the same thing, but only after a configured keyboard/mouse idle timeout, and maybe use the Shift key (or Scroll Lock) instead of a mouse move.

欢烬 2024-07-20 01:46:18

来自 MSDN

如果存在以下任一情况,Windows 不会启动屏幕保护程序:

  • 活动应用程序不是基于 Windows 的应用程序。
  • 存在 CBT 窗口。
  • 活动应用程序接收 WM_SYSCOMMAND 消息,并将 wParam 参数设置为 SC_SCREENSAVE 值,但它不会将该消息传递给 DefWindowProc 函数。

但有一个警告:

Windows Vista 及更高版本:如果通过策略启用了密码保护,则无论应用程序对 SC_SCREENSAVE 通知执行什么操作,屏幕保护程序都会启动。

即使您将 SetThreadExecutionState 与 ES_CONTINUOUS 一起使用,这似乎也适用。

因此,如果没有警告,您的选择将是:

  1. SetThreadExecutionState 和 ES_CONTINUOUS (如其他答案中所述)。
  2. 建立一个基于计算机的培训窗口(需要挂钩)。
  3. 不要让带有 SC_SCREENSAVE 的 WM_SYSCOMMAND 传递到 DefWindowProc。 (假设您只关心您的应用程序是活动应用程序。)
  4. 安装模拟鼠标抖动的加密狗。

最后一个选项很好,因为它甚至可以使用密码保护策略。

From MSDN:

Windows does not start the screen saver if any of the following conditions exist:

  • The active application is not a Windows-based application.
  • A CBT window is present.
  • The active application receives the WM_SYSCOMMAND message with the wParam parameter set to the SC_SCREENSAVE value, but it does not pass the message to the DefWindowProc function.

There's a caveat though:

Windows Vista and later: If password protection is enabled by policy, the screen saver is started regardless of what an application does with the SC_SCREENSAVE notification.

That seems to apply even if you use the SetThreadExecutionState with ES_CONTINUOUS.

So, if it weren't for the caveat, your choices would be:

  1. SetThreadExecutionState with ES_CONTINUOUS (as described in other answers).
  2. Put up a computer-based training window (which requires hooks).
  3. Don't let the WM_SYSCOMMAND with SC_SCREENSAVE be passed onto DefWindowProc. (Assuming you care only when your application is the active application.)
  4. Install a dongle that simulates mouse jiggle.

The last option is nice in that it works even with the password protection policy.

开始看清了 2024-07-20 01:46:18

SystemParametersInfo

具体来说,SPI_SETSCREENSAVEACTIVE< /代码> 参数。

这不行吗? 我很惊讶我在这里没有看到它。 请注意,SetThreadExecutionState 根本不会影响屏幕保护程序,只会影响显示器的休眠。

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.

明明#如月 2024-07-20 01:46:18

这篇博文详细介绍了您需要在 C++ 中做什么。

来自网站的实际代码片段:

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);

}

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);

}

兮颜 2024-07-20 01:46:18

正如 Adrian McCarthyMSDN 表示:

如果通过策略启用了密码保护,则无论应用程序对 SC_SCREENSAVE 通知执行什么操作,屏幕保护程序都会启动。

因此,使用 UINT SC_SCREENSAVE 从 WM_SYSCOMMAND 捕获事件,并通过返回 0 或创建假鼠标移动(“mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, 0)”)来丢弃它,如果用户启用了受密码保护的屏幕保护程序选项。

使用 SetThreadExecutionState winAPI 告诉即使用户没有与计算机交互,操作系统也会识别该线程正在使用中。 这些将防止出现屏幕保护程序并阻止机器自动暂停。

有一系列标志用于指定当前线程的新状态:

  • ES_AWAYMODE_REQUIRED (0x00000040):启用离开模式。
  • ES_DISPLAY_REQUIRED (0x00000002) : 强制显示
    重置显示器空闲计时器。
  • ES_SYSTEM_REQUIRED (0x00000001) : 强制系统处于
    通过重置系统空闲计时器进入工作状态。
  • ES_CONTINUOUS (0x80000000) : 通知系统当前状态
    设置应保持有效,直到下一次调用使用
    ES_CONTINUOUS 和其他状态标志之一被清除。

由于它是一个winAPI,您可以直接在win32或mfc应用程序中调用它

//To stop/start screen saver and monitor power off event
void SetKeepScreenOn(BOOL isKeepScreenOn)
{
   if (isKeepScreenOn == TRUE)
   {
       SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED /*| ES_AWAYMODE_REQUIRED*/);        
   }
   else
   {
       SetThreadExecutionState(ES_CONTINUOUS);      
   }
}

如果有人想在 C# 中使用它,必须PInvoke

[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);

用户定义类型:

[FlagsAttribute]
public enum EXECUTION_STATE :uint
{
   ES_AWAYMODE_REQUIRED = 0x00000040,
   ES_CONTINUOUS = 0x80000000,
   ES_DISPLAY_REQUIRED = 0x00000002,
   ES_SYSTEM_REQUIRED = 0x00000001
}

这里下面是调用过程:

void SetKeepScreenOn(bool isKeepScreenOn)
{
    if (isKeepScreenOn == true)
    {
         //You can combine several flags and specify multiple behaviors with a single call
         SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED /*| EXECUTION_STATE.ES_AWAYMODE_REQUIRED*/);        
    }
    else
    {
         //To reset or allow those event again you have to call this API with only ES_CONTINUOUS
         SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);      
    }
 }

根据MSDN 此 API 使用起来也很安全。

系统维护已调用 SetThreadExecutionState 的应用程序的计数。 系统跟踪每个调用 SetThreadExecutionState 的线程并相应地调整计数器。 如果该计数器达到零并且没有任何用户输入,系统将进入睡眠状态。

如果应用程序在重置标志之前崩溃,系统将进行调整并自动重置。

As Adrian McCarthy mentioned from MSDN that :

If password protection is enabled by policy, the screen saver is started regardless of what an application does with the SC_SCREENSAVE notification.

So catch the event from WM_SYSCOMMAND using UINT SC_SCREENSAVE and discarded it by returning 0 or by creating a fake mouse move ("mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, 0)") will not work properly if the user enabled password-protected screen saver option.

Use SetThreadExecutionState winAPI to tell the operating system that the thread is in use, even if the user is not interacting with the computer. These will prevent to appear screen saver and stop the machine from being suspended automatically.

There are series of flags to specify a new state for the current thread:

  • ES_AWAYMODE_REQUIRED (0x00000040) : Enables away mode.
  • ES_DISPLAY_REQUIRED (0x00000002) : Forces the display to be on by
    resetting the display idle timer.
  • ES_SYSTEM_REQUIRED (0x00000001) : Forces the system to be in the
    working state by resetting the system idle timer.
  • ES_CONTINUOUS (0x80000000) : Informs the system that the state being
    set should remain in effect until the next call that uses
    ES_CONTINUOUS and one of the other state flags are cleared.

As it's a winAPI, you can call this directly in win32 or mfc application

//To stop/start screen saver and monitor power off event
void SetKeepScreenOn(BOOL isKeepScreenOn)
{
   if (isKeepScreenOn == TRUE)
   {
       SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED /*| ES_AWAYMODE_REQUIRED*/);        
   }
   else
   {
       SetThreadExecutionState(ES_CONTINUOUS);      
   }
}

If someone wants to use this in C#, must have to PInvoke this :

[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);

User-Defined Types:

[FlagsAttribute]
public enum EXECUTION_STATE :uint
{
   ES_AWAYMODE_REQUIRED = 0x00000040,
   ES_CONTINUOUS = 0x80000000,
   ES_DISPLAY_REQUIRED = 0x00000002,
   ES_SYSTEM_REQUIRED = 0x00000001
}

Here below is the calling procedure:

void SetKeepScreenOn(bool isKeepScreenOn)
{
    if (isKeepScreenOn == true)
    {
         //You can combine several flags and specify multiple behaviors with a single call
         SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED /*| EXECUTION_STATE.ES_AWAYMODE_REQUIRED*/);        
    }
    else
    {
         //To reset or allow those event again you have to call this API with only ES_CONTINUOUS
         SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);      
    }
 }

According to MSDN this API is safe also to use.

The system maintains a count of applications that have called SetThreadExecutionState. The system tracks each thread that calls SetThreadExecutionState and adjusts the counter accordingly. If this counter reaches zero and there has not been any user input, the system enters sleep.

If the Application crashed before resetting flag, the System will adjust and will reset automatically.

埋葬我深情 2024-07-20 01:46:18

不敢相信没有人指出简单而明显的解决方案:

#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);
   }
}

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);
   }
}
装迷糊 2024-07-20 01:46:18

您可以使用 SystemParametersInfo
获取SCREENSAVETIMEOUT,然后立即将超时设置回相同的值。 只要您想阻止屏幕保护程序继续运行,就定期在计时器上执行此操作。

这具有重置当前倒计时器的效果,而无需实际更改系统设置。

您可能还想调用 SetThreadExecutionState 来影响其他答案提到的电源。

You can use SystemParametersInfo
to get the SCREENSAVETIMEOUT and then immediately set the timeout back to the same value. Do this periodically on a timer for as long as you want to prevent the screensaver from going on.

This has the effect of resetting the current countdown timer without actually changing the system setting.

You probably also want to call SetThreadExecutionState to affect the power as other answers mention.

九公里浅绿 2024-07-20 01:46:18

重置超时计数器即可

只需使用SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 1, nil, SPIF_SENDWININICHANGE);

Just reset the timeout counter with

SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 1, nil, SPIF_SENDWININICHANGE);

乖乖兔^ω^ 2024-07-20 01:46:18

来自 JD 设计免费软件 - Flipss.exe(下载 12kb) 是一个命令行实用程序,将为您设置 SPI_SETSCREENSAVEACTIVE。

"FlipSS.exe -h" to see the current state.
"FlipSS.exe /on" to set the screensaver on.
"FlipSS.exe /off" to set the screensaver off.

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.
又怨 2024-07-20 01:46:18

我意识到这是一个旧线程,但我第一次面临这个问题(工作机器完全锁定,就更改超短睡眠时间、屏幕保护程序等而言)-我什至无法更改我的桌面背景)。 我环顾四周的解决方案,有些看起来过于复杂,有些......不是那么复杂。

我的一些同事正在使用咖啡因。 但这肯定是某种间谍软件等,因为如果没有开放的互联网连接,它就拒绝运行。

所以我发现了这个(并稍微修改了它),这正是 Caffeine 所做的(除了 Caffeine 每 59 秒执行一次),而没有所有......充其量是膨胀软件。

在 PowerShell 中,执行以下 2 行命令行:

 $WShell = New-Object -Com "Wscript.Shell"
 while(1) {$WShell.SendKeys("{F15}"); sleep 200}

或者,如果您愿意,也可以将其设为单行:(

while(1) {(New-Object -Com "Wscript.Shell").SendKeys("{F15}"); sleep 200}

后者似乎会泄漏内存,但似乎根本不会泄漏)

一旦运行其中任何一个,你的屏幕不会锁定,直到你执行 ctrl-c 或关闭 Powershell 窗口(仅在后一个版本中,似乎 ctrl-c 可能不会发生,直到睡眠间隔过去)。

请注意,至少在我见过的任何键盘上都没有 F15 键(但它是合法的 Windows 按键),因此没有副作用。 现在,如果您的 IT 部门。 异常偏执,他们可能会标记 F15 按键(我的就是超级偏执,但他们几个月来都没有注意到任何事情)。 如果是这样,请改用滚动锁定之类的东西。

这两个在我的win10机器上100%工作。 简单就好!

I realize this is an old thread, but I'm faced with this issue for the first time (work machine is totally locked down, as far as changing super short sleep time, screensaver, etc. - I can't even change my desktop background). I've looked around at solutions, some seemingly way overcomplicated and some...not so much.

Some of my colleagues are using Caffeine. But that is surely some kind of spyware, etc., as it refuses to run if there is not an open internet connection.

So I found this (and modified it slightly), which is exactly what Caffeine does (except Caffeine does it every 59 seconds), without all the...at best, bloatware.

In PowerShell, execute the following 2 command lines:

 $WShell = New-Object -Com "Wscript.Shell"
 while(1) {$WShell.SendKeys("{F15}"); sleep 200}

Or you can make it a one-liner if you like:

while(1) {(New-Object -Com "Wscript.Shell").SendKeys("{F15}"); sleep 200}

(the latter of which seems like it would leak memory, but it does not seem to at all)

Once you run either of those, your screen will NOT lock, until you do ctrl-c, or close the Powershell window (in the latter version only, it seems, the ctrl-c may not happen until the sleep interval elapses).

Note that there is no F15 key, at least on any keyboard I've ever seen (but it's a legit windows keystroke), so there are no side effects. Now, if you your IT dept. is exceptionally paranoid, they may flag an F15 keystroke (mine is super paranoid, but they haven't noticed anything for months). If so, use something like scroll-lock instead.

Both of these 100% work on my win10 machine. Simple is good!

瀞厅☆埖开 2024-07-20 01:46:18

AutoHotkey 可以使用脚本中的 1 行 DllCall 设置 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE),以便使用 .ahk 脚本轻松完成此操作。

用于禁用屏幕保护程序的 AutoHotkey 代码:

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

用于启用屏幕保护程序的 AutoHotkey 代码:

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

参考论坛主题:

F13Key - 使用 SystemParametersInfo 切换屏幕保护程序
SKAN - 如何暂时禁用屏幕保护程序

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文