从挂起模式唤醒后如何打开显示器?

时间:2010-04-05 09:40:09

标签: c# monitor suspend wakeup

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

我使用过CreateWaitableTimer个函数,一切都很顺利。在给定时间,PC唤醒但显示器仍处于省电模式(关闭)!

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

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

有什么想法吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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