X分钟后出现空白屏幕

本文关键字:空白 屏幕 分钟 | 更新日期: 2023-09-27 18:00:11

我觉得我没有足够的休息时间,在电脑上我的眼睛有点紧张。我尽可能多地执行20分钟规则,但一旦进入区域,我往往会忘记执行。

我想制作一个快速应用程序(如果有,请指给我看),在20分钟后(可能是倒计时前10秒),它会关闭屏幕或清空屏幕,或者强迫我休息一下。

不确定C#是否有权访问类似的api,也不确定它应该是控制台应用程序还是wpf应用程序。它需要在启动时启动,并且可能存在于任务栏中。

有什么建议吗?

编辑

如果它太宽泛,这里就是我在之后的样子

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

X分钟后出现空白屏幕

要打开/关闭,可以使用以下类:

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

您可以使用WPF应用程序和Quartz.NET.

在这种情况下,您可以将自定义内容添加到窗口中,例如时钟,当屏幕被阻止时,它就会显示出来。

主窗口.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();
            }));
        }            
    }
}

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

在新的Winforms项目中,执行以下操作:
-设置FormBorderStyle=无
-设置背景颜色=红色(或您想要的任何颜色)
-Set TopMost=True
-设置WindowState=最大化
-在窗体上放置计时器
-将计时器间隔设置为2000
-设置计时器已启用=true
-双击计时器并写入Close();在事件处理程序中。

完成!

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

这就是关闭屏幕的方法

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
   }