X分钟后的空白屏幕

时间:2013-02-18 18:33:12

标签: c# .net

我认为我没有休息,我的眼睛因为在电脑上而有点紧张。我尽可能多地尝试20分钟的规则但是一旦我进入该区域,我往往会忘记这样做。

我想掀起一个快速的应用程序(如果存在请指出我),在20分钟后(可能在倒计时前10秒)将关闭屏幕或空白或其他东西迫使我休息一下

不确定C#是否可以访问这样的api并且不确定它是应该是控制台应用程序还是wpf应用程序或者是什么。它需要在启动时启动,并且可能存在于任务栏中。

有什么建议吗?

修改

如果它太宽泛,那就是我追求的目标

  1. X分钟后C#可以将屏幕空白吗?然后在X秒后恢复正常。
  2. 要做这样的定时任务,最好使用带有的东西 gui或者我可以使用控制台应用程序(尝试将其设置为 尽可能快)

4 个答案:

答案 0 :(得分:6)

要打开/关闭,您可以使用以下课程:

  public static class MonitorHelper
    {
        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);

        public static void TurnOn()
        {
            SendMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);
        }

        public static void TurnOff()
        {
            SendMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
        }

        const int SC_MONITORPOWER = 0xF170;
        const int WM_SYSCOMMAND = 0x0112;
        const int MONITOR_ON = -1;
        const int MONITOR_OFF = 2;     
    }

MonitorHelper类的用法:

class Program
{
    static void Main(string[] args)
    {
        MonitorHelper.TurnOff();
        Thread.Sleep(TimeSpan.FromSeconds(2));
        MonitorHelper.TurnOn();
    }
}

如果您想通过开启/关闭监视器安排任务,可以使用Quartz.NET

Quartz.NET示例:

工作班:

class MonitorJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        MonitorHelper.TurnOff();
        Thread.Sleep(TimeSpan.FromSeconds(2));
        MonitorHelper.TurnOn();
    }
}

配置代码:

ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler sched = schedFact.GetScheduler();

IJobDetail job = JobBuilder.Create<MonitorJob>()
 .WithIdentity("monitorJob", "group")
 .Build();

ITrigger trigger = TriggerBuilder.Create()
  .WithIdentity("monitorTrigger", "group")            
  .StartNow()
  .WithSimpleSchedule(x => x.WithIntervalInMinutes(1).RepeatForever())
  .Build();

sched.ScheduleJob(job, trigger);

sched.Start();

使用PostMessage的MonitorHelper类:

class MonitorHelperEx
{
    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    public static void TurnOn()
    {
        PostMessageSafe(new IntPtr(-1), WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);
    }

    public static void TurnOff()
    {
        PostMessageSafe(new IntPtr(-1), WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
    }

    static void PostMessageSafe(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
    {
        bool returnValue = PostMessage(hWnd, msg, wParam, lParam);
        if (!returnValue)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
    }

    static readonly IntPtr SC_MONITORPOWER = new IntPtr(0xF170);
    static readonly uint WM_SYSCOMMAND = 0x0112;
    static readonly IntPtr MONITOR_ON = new IntPtr(-1);
    static readonly IntPtr MONITOR_OFF = new IntPtr(2);     
}

答案 1 :(得分:3)

您可以使用WPF Application和Quartz.NET。

在这种情况下,您可以向窗口添加自定义内容,例如时钟,屏幕将被阻止时显示。

MainWindow.xaml:

<Window x:Class="WPFBlankScreen.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"   
        WindowState="Minimized"
        Background="Orange" 
        KeyDown="Window_KeyDown"
        >
    <Grid>        
    </Grid>
</Window>

代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Init();
    }

    private void Init()
    {
        ISchedulerFactory schedFact = new StdSchedulerFactory();
        IScheduler sched = schedFact.GetScheduler();

        IDictionary<string, object> map = new Dictionary<string, object>();
        map.Add("window", this);

        IJobDetail job = JobBuilder.Create<ShowJob>()
         .WithIdentity("showJob", "group")             
         .UsingJobData(new JobDataMap(map))
         .Build();

        ITrigger trigger = TriggerBuilder.Create()
          .WithIdentity("showTrigger", "group")
          .StartNow()
          .WithSimpleSchedule(x => x.WithIntervalInMinutes(1).RepeatForever())
          .Build();

        sched.ScheduleJob(job, trigger);
        sched.Start();
    }

    private void Window_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.F11)
        {               
            this.Hide();
        }
    }
}

ShowJob类:

class ShowJob: IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Window win = context.JobDetail.JobDataMap.Get("window") as Window;
        if (win != null)
        {
            Application.Current.Dispatcher.Invoke((Action)(() =>
            {
                win.Width = System.Windows.SystemParameters.FullPrimaryScreenWidth;
                win.Height = System.Windows.SystemParameters.FullPrimaryScreenHeight;
                win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                win.WindowStyle = WindowStyle.None;
                win.Topmost = true;
                win.WindowState = WindowState.Maximized;
                win.Show();
            }));

            IDictionary<string, object> map = new Dictionary<string, object>();
            map.Add("window", win);

            IJobDetail job = JobBuilder.Create<HideJob>()
             .WithIdentity("hideJob", "group")
             .UsingJobData(new JobDataMap(map))
             .Build();

            ITrigger trigger = TriggerBuilder.Create()
              .WithIdentity("hideTrigger", "group")
              .StartAt(DateBuilder.FutureDate(5, IntervalUnit.Second))
              .Build();

            context.Scheduler.ScheduleJob(job, trigger);
        }
    }
}

HideJob类:

class HideJob: IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Window win = context.JobDetail.JobDataMap.Get("window") as Window;          
        if (win != null && Application.Current != null)
        {               
            Application.Current.Dispatcher.Invoke((Action)(() =>
            {
                win.Hide();
            }));
        }            
    }
}

答案 2 :(得分:1)

我的回答可能不太花哨,但它非常简单,而且有效:

在新的Winforms项目中,执行以下操作:
- 设置FormBorderStyle =无
- 设置BackColor =红色(或任何你想要的)
- 设置TopMost = True
- 设置WindowState =最大化
- 在表格上放一个计时器
- 将计时器间隔设置为2000
- 设置计时器启用=真
- 双击计时器并写入Close();在事件处理程序中。

完成!

编辑;这只会使一个屏幕亮红色两秒钟,但你应该能够注意到这一点....

答案 3 :(得分:0)

这是关闭屏幕的方式

using System.Runtime.InteropServices; //to DllImport

 public int WM_SYSCOMMAND = 0x0112;
 public int SC_MONITORPOWER = 0xF170; //Using the system pre-defined MSDN constants    
 that    can be used by the SendMessage() function .
 [DllImport("user32.dll")]
 private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);
//To call a DLL function from C#, you must provide this declaration .


  private void button1_Click(object sender, System.EventArgs e)
  {

   SendMessage( this.Handle.ToInt32() , WM_SYSCOMMAND , SC_MONITORPOWER ,2 );//DLL    
   function
   }