从挂起模式唤醒后如何打开显示器? [英] How to turn on monitor after wake-up from suspend mode?

查看:132
本文介绍了从挂起模式唤醒后如何打开显示器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to wake up a PC from sleep to perform some actions using C#.

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

I've used CreateWaitableTimer functions, everything goes fine. At given time the PC wakes up but the monitor stays in power save mode (turned off)!

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

So I want to know, how to turn the monitor ON after wake up?

PS我已经尝试过关于如何打开/关闭/待机显示器的完整指南"-使用SendMessage(代码项目)和SetThreadExecutionState(ES_DISPLAY_REQUIRED)-对我不起作用.

PS I've tried "Complete Guide on How To Turn A Monitor On/Off/Standby" - with SendMessage (Codeproject) and SetThreadExecutionState(ES_DISPLAY_REQUIRED) - it doesn't work for me.

有什么想法吗?

推荐答案

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

for me it works fine to use pinvoke to call SendMessage.
code example for 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

the above solution was inspired by this answer: https://stackoverflow.com/a/332733/1468842

这篇关于从挂起模式唤醒后如何打开显示器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆