Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在windows 8中,我们可以根据时间间隔打开/关闭监视器吗?_C#_Windows 8 - Fatal编程技术网

C# 在windows 8中,我们可以根据时间间隔打开/关闭监视器吗?

C# 在windows 8中,我们可以根据时间间隔打开/关闭监视器吗?,c#,windows-8,C#,Windows 8,我正在使用此代码关闭显示器,但我需要在某些情况下关闭/打开显示器,例如,在下午6:00关闭显示器,在晚上7:00打开显示器,是否可能 private int SC_MONITORPOWER = 0xF170; private uint WM_SYSCOMMAND = 0x0112; [System.Runtime.InteropServices.DllImport("user32.dll")] static extern IntPtr SendMessage(

我正在使用此代码关闭显示器,但我需要在某些情况下关闭/打开显示器,例如,在下午6:00关闭显示器,在晚上7:00打开显示器,是否可能

    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);
    }

我不确定您的代码是否正常工作,但假设它正常工作,那么您可以使用某种计时器来实现这一点 假设你想在下午6点关掉显示器

    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
             }