【发布时间】:2010-11-28 15:25:59
【问题描述】:
如何通过代码(在 cmd 中)打开屏幕保护程序(Windows 7)?
【问题讨论】:
标签: windows command screensaver
如何通过代码(在 cmd 中)打开屏幕保护程序(Windows 7)?
【问题讨论】:
标签: windows command screensaver
我有 Windows 7。我放置了一行:
@start /wait %windir%\ExtraPath\ScreenSaverName.scr /s & rundll32 user32.dll,LockWorkStation
在批处理 (.bat) 文件中,将其放在适当的目录中,并使用所需的快捷键创建指向该目录的快捷方式。
在这一行中,\ExtraPath 是您的 win 目录下的附加路径(通常是 \system32),屏幕保护程序所在的位置,ScreenSaverName.scr 是所需屏幕保护程序本身的名称。
效果很好。
现在我可以按快捷键来运行屏幕保护程序并锁定电脑了。
【讨论】:
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();
}
}
这行得通 - 唯一的缺点是你不能与电脑交互 7 秒,但我猜它的 7 可以让人们在制作屏幕保护程序“永久”之前有时间。
【讨论】:
以下是否符合您的要求?
start logon.scr /s
只要.scr 在 PATH 上,上述命令就可以工作。
编辑:我不知道 Windows 7 是否带有 logon.scr,请确保您使用实际安装在 Windows 7 中的 .scr 对其进行测试。
请注意,我的想法是从Screensaver Sample Command Line Options 调用.scr 和/s:
当 Windows 运行您的屏幕保护程序时,它 使用三个命令之一启动它 线路选项:
- /s - 以全屏模式启动屏幕保护程序。
- /c – 显示配置设置对话框。
- /p #### – 使用指定的显示屏幕保护程序的预览 窗口句柄。
编辑 2:
我做了一些额外的搜索,发现你可以创建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"
这些答案都不是完美的,在屏幕保护程序被禁用后和工作站被锁定之前都会显示桌面闪烁。
可能无法重现恢复时启动屏幕保护程序和密码保护的系统行为。即使the answer to Launch System Screensaver from C# Windows Form 只是启动屏幕保护程序,它在恢复时没有密码保护。
【讨论】:
cmd 或vbs 解决方案对于其他提出相同问题的人来说“足够好”。
将the cmd and vbs script ideas 与the answer to Launch System Screensaver from C# Windows Form 的代码放在一起,我得出以下结论:
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();
}
}
要构建它,install the .NET Framework,将上面的代码复制并粘贴到lock.cs,然后运行:
%SystemRoot%\Microsoft.NET\Framework\v3.5\csc.exe lock.cs
将创建的lock.exe 放在您的路径中,然后输入lock 应该启用配置的屏幕保护程序并锁定您的工作站。
【讨论】:
【讨论】: