【问题标题】:How to turn on monitor after wake-up from suspend mode? [closed]从挂起模式唤醒后如何打开显示器? [关闭]
【发布时间】:2010-04-05 09:40:09
【问题描述】:

我需要将电脑从睡眠中唤醒以使用 C# 执行一些操作。

我使用了CreateWaitableTimer 函数,一切正常。在给定时间,PC 会唤醒,但显示器仍处于省电模式(关闭)!

所以我想知道,唤醒后如何打开显示器?

PS 我试过“如何打开/关闭/待机显示器的完整指南”——使用 SendMessage (Codeproject) 和 SetThreadExecutionState(ES_DISPLAY_REQUIRED)——它对我不起作用。

有什么想法吗?

【问题讨论】:

  • 这个问题的意义为零。显示器是给人看的,不是手电筒。

标签: c# monitor suspend wakeup


【解决方案1】:

尝试让鼠标移动。当我通过点击键盘唤醒我的 Windows 7 系统时,屏幕一直保持黑色,直到我移动鼠标。

Cursor.Position = new Point(x, y);

【讨论】:

  • 不,它也不起作用。但是在我通过按键盘键打开监视器后 - 我注意到光标设置为 (x,y) 位置,所以代码被执行了
  • 您可以通过使用 user32.dll 函数 mouse_event 以编程方式使鼠标移动唤醒监视器。请参阅此答案stackoverflow.com/a/14698416/999120 以了解如何在代码中执行此操作;并查看第 4 条评论以了解如何在 .bat 文件、powershell 或任务计划程序中执行此操作。
【解决方案2】:

对我来说,使用 pinvoke 调用 SendMessage 效果很好。
csharp 的代码示例:

using System;
using System.Runtime.InteropServices;

namespace MyDummyNamespace
{
   class MyProgram
   {
      private static int Main(string[] args)
      {
         // your program code here
         // ...

         NativeMethods.MonitorOff();
         System.Threading.Thread.Sleep(5000);
         NativeMethods.MonitorOn();

         return 0;
      }

      private static class NativeMethods
      {
         internal static void MonitorOn()
         {
            SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (IntPtr)MONITOR_ON);
         }

         internal static void MonitorOff()
         {
            SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (IntPtr)MONITOR_OFF);
         }

         [DllImport("user32.dll", CharSet = CharSet.Auto)]
         private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

         private static int MONITOR_ON = -1;
         private static int MONITOR_OFF = 2;
         private static int MONITOR_STANBY = 1;

         private static IntPtr HWND_BROADCAST = new IntPtr(0xffff);
         private static UInt32 WM_SYSCOMMAND = 0x0112;
         private static IntPtr SC_MONITORPOWER = new IntPtr(0xF170);
      }
   }
}

上述解决方案的灵感来自这个答案:https://stackoverflow.com/a/332733/1468842

【讨论】:

  • 仅供参考,这种关闭屏幕的方法是一种黑客行为 - 您正在尝试将您无法控制的消息广播到桌面上的所有窗口? Microsoft 明确不支持此行为。您永远不应发送明确记录为仅由系统发送的消息的消息。这可能不适用于未来版本的 Windows,您绝对不应该向所有 Windows 广播。如果可能,请避免使用这种技术。
猜你喜欢
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-30
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多