首页 > 解决方案 > 以可编程方式唤醒睡眠中的 Windows 10 屏幕

问题描述

所以我尝试了几种不同的方法来唤醒我的显示器。我想从外部服务唤醒我的显示器(我在“更改 PC 睡眠时间”->“屏幕”->“插入后关闭”中设置为 15 分钟),作为唤醒 3 个屏幕需要 45-60 秒。我想在走廊中检测到运动时调用此 API,但该部分已设置并完成。

所以很自然(我猜),我设置了一个调用 Windows API 的 .NET 5.0 Web API。目前它只是将光标移动 1 个像素,但不幸的是,这似乎并没有唤醒屏幕(可能是因为 Windows 知道是软件做到了)。我还尝试将光标移动许多像素,但这也没有做任何事情。

我是这样做的:

[ApiController]
[Route("[controller]")]
public class MouseController : ControllerBase
{
    [DllImport("User32.dll", SetLastError = false)] 
    public static extern IntPtr GetDesktopWindow();
        
    [DllImport("User32.Dll")]
    public static extern long SetCursorPos(int x, int y);

    [DllImport("User32.Dll")]
    public static extern bool GetCursorPos(out POINT lpPoint);

    [DllImport("User32.Dll")]
    public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;

        public POINT(int X, int Y)
        {
            x = X;
            y = Y;
        }
    }

    [HttpGet("[action]")]
    public async Task WakeMonitors()
    {
        POINT p;
        if (GetCursorPos(out p))
        {
            IntPtr desktopWinHandle = GetDesktopWindow();
            ClientToScreen(desktopWinHandle, ref p);
            SetCursorPos(p.x, p.y - 1);
        }
    }
}

它出奇地有效,但正如我所说,它不会将我的显示器从睡眠中唤醒。

那我该怎么做呢?我不关心我必须做的黑客攻击,但如果我可以调用一些 Windows API 并告诉它“从睡眠中唤醒监视器”,那就太好了。

标签: c#.netwinapi

解决方案


推荐阅读