title: Events fired when the display power is switched On/Off
body: <p>I search for an event or if doesn't exist, a method to know if the screen off (Power Options – Control Panel - Turn off the display setting).</p>
  
  <p>None of these solutions work for me.<br>
  So either I was wrong somewhere, or it's just not suitable.  </p>
  
  <p><a href="https://stackoverflow.com/questions/2208595/c-sharp-how-to-get-the-events-when-the-screen-display-goes-to-power-off-or-on">How to get the events when the screen/display goes to power OFF or ON?</a></p>
  
  <p>I expect some track or solution.<br>
  The problem is that I don't know what I'm doing, if you could help me a little more it would be cool.  </p>
  
  <p>I made this, but it doesn't work:  </p>
  
  <pre><code>internal static class NativeMethods
  {
      public static Guid GUID_MONITOR_POWER_ON = new Guid(0x02731015, 0x4510, 0x4526, 0x99, 0xE6, 0xE5, 0xA1, 0x7E, 0xBD, 0x1A, 0xEA);
      public const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000;
      public const int WM_POWERBROADCAST = 0x0218;
      public const int PBT_POWERSETTINGCHANGE = 0x8013;
  
      [StructLayout(LayoutKind.Sequential, Pack = 4)]
      public struct POWERBROADCAST_SETTING
      {
          public Guid PowerSetting;
          public uint DataLength;
          public byte Data;
      }
  
      [DllImport(@"User32", SetLastError = true, EntryPoint = "RegisterPowerSettingNotification", CallingConvention = CallingConvention.StdCall)]
      public static extern IntPtr RegisterPowerSettingNotification(IntPtr hRecipient, ref Guid PowerSettingGuid, Int32 Flags);
  
      [DllImport(@"User32", SetLastError = true, EntryPoint = "UnregisterPowerSettingNotification", CallingConvention = CallingConvention.StdCall)]
      public static extern bool UnregisterPowerSettingNotification(IntPtr handle);
  }
  
  private void WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  {
      Debug.WriteLine("EVENT", "DEBUG");
  }
  
  public form1()
  {
      NativeMethods.RegisterPowerSettingNotification(this.Handle, ref NativeMethods.GUID_MONITOR_POWER_ON, NativeMethods.DEVICE_NOTIFY_WINDOW_HANDLE);
  }
  </code></pre>