简体   繁体   中英

Can we turn on/Off monitor with respect to time intervals in windows 8?

I am using this code to turn off the monitor but I need to turn off/on the monitor with some condition eg turn it off at 6.00 pm and turn it on at 7.00 pm, Is it Possible?

    private int SC_MONITORPOWER = 0xF170;

    private uint WM_SYSCOMMAND = 0x0112;

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    enum MonitorState
    {
        ON = -1,
        OFF = 2,
        STANDBY = 1
    }

    private void OnMonitor()
    {
        IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
        SendMessage(hwnd, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)MonitorState.ON);
    }


    private void OffMonitor()
    {
        IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
        SendMessage(hwnd, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)MonitorState.OFF);
    }

I am not sure whether your code is working or not, but assuming that it's working then you can use some kind of timers to achieve this say you want to turn off monitor at 6:00 pm then

    System.Timers.Timer timer = new System.Timers.Timer();
    TimeSpan tsDifference = DateTime.Parse("06:00 pm").Subtract(DateTime.Now);
    timer.Interval= (double)((tsDifference.Hours * 60 * 60) + (tsDifference.Minutes * 60) + (tsDifference.Seconds)) * 1000 + (tsDifference.Milliseconds);    
    timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    timer.Enabled=true;

      void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
               //turn off your monitor
             }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM