如何通过代码(在cmd中)打开(Win​​dows 7)屏幕保护程序?

oO.*_*oO. 9 windows command screensaver

如何通过代码(在cmd中)打开(Win​​dows 7)屏幕保护程序?

Gra*_*ner 10

以下是否符合您的要求?

start logon.scr /s
Run Code Online (Sandbox Code Playgroud)

只要.scr在PATH上,上面的命令应该有效.

编辑:我不知道Windows 7是否附带logon.scr,请确保您使用.scr实际安装在Windows 7中的Windows 进行测试.

请注意,我想到.scr/s屏幕保护程序示例命令行选项中调用with :

当Windows运行您的屏幕保护程序时,它会使用以下三个命令行选项之一启动它:

  • / s - 以全屏模式启动屏幕保护程序.
  • / c - 显示配置设置对话框.
  • / p #### - 使用指定的窗口句柄显示屏幕保护程序的预览.

编辑2:

我做了一些额外的搜索,发现你可以创建lock.cmd:

@start /wait logon.scr /s & rundll32 user32.dll,LockWorkStation
Run Code Online (Sandbox Code Playgroud)

或者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"
Run Code Online (Sandbox Code Playgroud)

这些答案都不是完美的,都会在屏幕保护程序被禁用后以及工作站被锁定之前显示桌面的闪烁.

可能无法重现启动屏幕保护程序和恢复密码保护的系统行为.即使从C#Windows窗体启动系统屏幕保护程序的答案只启动屏幕保护程序,它在恢复时没有密码保护.


Gra*_*ner 6

放在一起cmdvbs剧本的想法与代码的答案,从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();
    }
}
Run Code Online (Sandbox Code Playgroud)

要构建它,请安装.NET Framework,将上面的代码复制并粘贴到其中lock.cs,然后运行:

%SystemRoot%\Microsoft.NET\Framework\v3.5\csc.exe lock.cs
Run Code Online (Sandbox Code Playgroud)

将创建lock.exe的路径放在您的路径中,之后,键入lock应该使用已配置的屏幕保护程序并锁定您的工作站.


oO.*_*oO. 3

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这个有效 - 唯一的缺点是你不能与电脑交互大约 7 秒,但我猜它 7 秒是为了让人们在使屏幕保护程序“永久”之前有时间。