简体   繁体   English

我们可以根据Windows 8中的时间间隔打开/关闭监视器吗?

[英]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? 我正在使用此代码关闭显示器,但是我需要在某些条件下关闭/打开显示器,例如,在6.00 pm时将其关闭,在7.0 pm时将其打开,是否可以?

    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 我不确定您的代码是否正常工作,但是假设它正常工作,那么您可以使用某种计时器来实现此功能,例如您想在下午6:00关闭监视器

    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
             }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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