1

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

我想创建一个快速应用程序(如果存在请指点我),它会在 20 分钟后(可能是倒计时前 10 秒)关闭屏幕或使其空白或迫使我休息一下。

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

有什么建议么?

编辑

如果它太宽泛,这就是我所追求的

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

4 回答 4

7

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

  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);     
}
于 2013-02-18T19:00:22.267 回答
3

您可以使用 WPF 应用程序和 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);
        }
    }
}

隐藏作业类:

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();
            }));
        }            
    }
}
于 2013-02-20T21:02:25.517 回答
1

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

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

完成的!

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

于 2013-02-26T12:18:18.610 回答
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
   }
于 2013-02-26T10:29:25.687 回答