c# How to get the events when the screen/display goes to power OFF or ON?

前端 未结 4 999
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 15:33

Hi I have been searching but I can\'t find the answer. How do I know when the screen is going off or on. Not the SystemEvents.PowerModeChanged . I dont know how to retrieve

4条回答
  •  孤城傲影
    2020-12-13 16:06

    The missing part was that I didn't register for the events.

    Found that there's a power management example from Microsoft:

    http://www.microsoft.com/en-us/download/details.aspx?id=4234

    hMonitorOn = RegisterPowerSettingNotification(this.Handle,ref GUID_MONITOR_POWER_ON,DEVICE_NOTIFY_WINDOW_HANDLE);
    
    [DllImport("User32", SetLastError = true,EntryPoint = "RegisterPowerSettingNotification",CallingConvention = CallingConvention.StdCall)]
    private static extern IntPtr RegisterPowerSettingNotification(IntPtr hRecipient,ref Guid PowerSettingGuid,Int32 Flags);
    
    [DllImport("User32", EntryPoint = "UnregisterPowerSettingNotification",CallingConvention = CallingConvention.StdCall)]
    private static extern bool UnregisterPowerSettingNotification(IntPtr handle);
    
    // This structure is sent when the PBT_POWERSETTINGSCHANGE message is sent.
    // It describes the power setting that has changed and contains data about the change
    [StructLayout(LayoutKind.Sequential, Pack = 4)]
    internal struct POWERBROADCAST_SETTING
    {
        public Guid PowerSetting;
        public Int32 DataLength;
    }
    

提交回复
热议问题