如何通过代码在Windows 7中打开屏幕保护程序(使用cmd)?
以下内容是否符合您的要求?
start logon.scr /s
只要将.scr
添加到PATH路径中,上述命令就应该可以工作。logon.scr
,请确保您使用的是实际安装在Windows 7中的.scr
。/s
调用.scr
的想法:当Windows运行您的屏幕保护程序时, 它会使用以下三个命令之一来启动它:
lock.cmd
:@start /wait logon.scr /s & rundll32 user32.dll,LockWorkStation
或者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"
这两个答案都不完美,都会在屏幕保护程序被禁用并且工作站锁定之前显示一闪而过的桌面。
可能无法重现启动屏幕保护程序并在恢复后保护密码的系统行为。即使从C# Windows表单启动系统屏幕保护程序的答案只会启动屏幕保护程序,它也不会在恢复时保护密码。
结合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();
}
}
要构建它,安装 .NET Framework,将上述代码复制并粘贴到 lock.cs
中,然后运行:
%SystemRoot%\Microsoft.NET\Framework\v3.5\csc.exe lock.cs
lock.exe
文件放在环境变量中,之后输入命令 lock
即可启动配置好的屏幕保护程序并锁定工作站。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();
}
}
I have Windows 7. I placed the line:
@start /wait %windir%\ExtraPath\ScreenSaverName.scr /s & rundll32 user32.dll,LockWorkStation
\ExtraPath
是您win目录下的附加路径(通常为\system32
),其中屏幕保护程序位于该路径下,而ScreenSaverName.scr则是所需屏幕保护程序的名称。
cmd
或vbs
的解决方案对其他问同样问题的人来说“足够好”。 - Grant Wagner