2

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

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

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

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

有任何想法吗?

4

2 回答 2

1

对我来说,使用 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

于 2017-02-22T13:55:53.913 回答
0

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

Cursor.Position = new Point(x, y);
于 2010-04-05T09:47:21.047 回答