如何通过代码(在 cmd 中)打开屏幕保护程序(Windows 7)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1430108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to turn screensaver on (windows 7) by a code (in cmd)?
提问by oO.
How to turn screensaver on (windows 7) by a code (in cmd)?
如何通过代码(在 cmd 中)打开屏幕保护程序(Windows 7)?
采纳答案by oO.
using System;
using System.Runtime.InteropServices;
public static class LockDesktop
{
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
private const int SC_SCREENSAVE = 0xF140;
private const int WM_SYSCOMMAND = 0x0112;
public static void SetScreenSaverRunning()
{
SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
}
public static void Main()
{
LockDesktop.SetScreenSaverRunning();
}
}
This works - only downside is that u cant interact with pc for something like 7 sec, but i guess its 7's to give ppl time before making screensaver 'permanent'.
这有效 - 唯一的缺点是你不能与 pc 交互 7 秒,但我想它的 7 在使屏幕保护程序“永久”之前给 ppl 时间。
回答by Grant Wagner
Does the following meet your requirements?
以下是否符合您的要求?
start logon.scr /s
As long as the .scr
is on the PATH the above command should work.
只要.scr
在 PATH 上,上面的命令就应该可以工作。
EDIT: I don't know if Windows 7 comes with logon.scr
, make sure you're testing it with a .scr
that is actually installed in Windows 7.
编辑:我不知道 Windows 7 是否带有logon.scr
,请确保您正在使用.scr
实际安装在 Windows 7 中的一个来测试它。
Note that I got the idea of just invoking the .scr
with /s
from Screensaver Sample Command Line Options:
请注意,我的想法是从Screensaver Sample Command Line Options 中调用.scr
with :/s
When Windows runs your screensaver, it launches it with one of three command line options:
- /s – Start the screensaver in full-screen mode.
- /c – Show the configuration settings dialog box.
- /p #### – Display a preview of the screensaver using the specified window handle.
当 Windows 运行您的屏幕保护程序时,它会使用以下三个命令行选项之一启动它:
- /s – 以全屏模式启动屏幕保护程序。
- /c – 显示配置设置对话框。
- /p #### – 使用指定的窗口句柄显示屏幕保护程序的预览。
EDIT 2:
编辑2:
I did some additional searching and found that you could create lock.cmd
:
我做了一些额外的搜索,发现你可以创建lock.cmd
:
@start /wait logon.scr /s & rundll32 user32.dll,LockWorkStation
Or lock.vbs
:
或者lock.vbs
:
Set objShell = CreateObject("Wscript.Shell")
' The "True" argument will make the script wait for the screensaver to exit
returnVal = objShell.Run("logon.scr", 1, True)
' Then call the lock functionality
objShell.Run "rundll32.exe user32.dll,LockWorkStation"
Neither of these answers is perfect, both reveal a flicker of the desktop after the screen saver is disabled and just prior to the workstation being locked.
这些答案都不是完美的,在屏幕保护程序被禁用后和工作站被锁定之前都显示桌面闪烁。
It may not be possible to reproduce the system behaviour of starting the screen saver and password protecting on resume. Even the answer to Launch System Screensaver from C# Windows Formonly starts the screen saver, it does not password protect on resume.
可能无法重现启动屏幕保护程序和恢复密码保护的系统行为。即使从 C# Windows 窗体启动系统屏幕保护程序的答案也只启动屏幕保护程序,它在恢复时没有密码保护。
回答by Grant Wagner
Putting together the cmd
and vbs
script ideaswith the code from the answer to Launch System Screensaver from C# Windows FormI came up with the following:
放在一起的cmd
和vbs
剧本的想法与代码从C#Windows窗体的答案发射系统屏幕保护程序,我想出了以下情况:
using System;
using System.Runtime.InteropServices;
public static class LockDesktop
{
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "LockWorkStation")]
private static extern IntPtr LockWorkStation();
private const int SC_SCREENSAVE = 0xF140;
private const int WM_SYSCOMMAND = 0x0112;
public static void SetScreenSaverRunning()
{
SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
LockWorkStation();
}
public static void Main()
{
LockDesktop.SetScreenSaverRunning();
}
}
To build it, install the .NET Framework, copy and paste the above code into lock.cs
, then run:
要构建它,请安装 .NET Framework,将上面的代码复制并粘贴到 中lock.cs
,然后运行:
%SystemRoot%\Microsoft.NET\Framework\v3.5\csc.exe lock.cs
Put the created lock.exe
in your path, after that, typing lock
should engage the configured screen saver and lock your workstation.
将创建的文件lock.exe
放在您的路径中,之后,键入lock
应启用配置的屏幕保护程序并锁定您的工作站。
回答by Albe.
I have Windows 7. I placed the line:
我有 Windows 7。我放置了以下行:
@start /wait %windir%\ExtraPath\ScreenSaverName.scr /s & rundll32 user32.dll,LockWorkStation
@start /wait %windir%\ExtraPath\ScreenSaverName.scr /s & rundll32 user32.dll,LockWorkStation
in a batch (.bat) file, place it in a appropriate dir, and created a shortcut pointing to this, with the desired shortcut key. In this line, \ExtraPath is the additional path under your win dir (usually this is \system32) where the screen savers are located, and ScreenSaverName.scr is the name of the desired screen saver itself.
在批处理 (.bat) 文件中,将其放置在适当的目录中,并使用所需的快捷键创建指向此的快捷方式。在这一行中,\ExtraPath 是屏幕保护程序所在的 win 目录(通常是 \system32)下的附加路径,而 ScreenSaverName.scr 是所需屏幕保护程序本身的名称。
It works perfectly.
它完美地工作。
Now I can press the shortcut keys to run the screen saver and lock the pc.
现在我可以按快捷键运行屏幕保护程序并锁定电脑。
Thanks really a lot.
真的非常感谢。