如何用c#实现一个简单的间隔一段时间让眼睛休息的程序

每日对着电脑屏幕,今天想用c#实现隔一段时间自动让系统提示我休息的功能,代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace eye_rest
{
    public partial class Form1 : Form
    {
        private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);
        private const int WM_SYSCOMMAND = 0x0112;
        private const int SC_SCREENSAVE = 0xf140;

        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        public Form1()
        {
            InitializeComponent();
            bool isStarted = false;
            bool isWindowVisible = true;
            NotifyIcon notifyIcon = new NotifyIcon();
            //notifyIcon.Icon = new Icon("icon.ico");
            notifyIcon.Icon = SystemIcons.Information; // 使用系统默认信息图标

            notifyIcon.Text = "eye_rest";
            notifyIcon.ContextMenuStrip = new ContextMenuStrip();
            notifyIcon.ContextMenuStrip.Items.Add("退出", null, (s, e) => Application.Exit()); // 添加“退出”菜单项


            while (true)
            {
                if (!isStarted)
                {
                    Console.WriteLine("程序已经启动,按任意键开始执行...");
                    //Console.ReadKey();
                    MessageBox.Show("start");
                    isStarted = true;
                    notifyIcon.ShowBalloonTip(1000, "提示", "程序已经开始运行!", ToolTipIcon.Info);
                    
                    
                }
                Console.WriteLine("休息一下眼睛...");
                // 打开屏保
                SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_SCREENSAVE, 0);

                if (isWindowVisible)
                {
                    // 将窗口隐藏,显示 NotifyIcon
                    IntPtr hWnd = GetForegroundWindow();
                    ShowWindow(hWnd, SW_HIDE);
                    notifyIcon.Visible = true;
                    isWindowVisible = false;
                }

                System.Threading.Thread.Sleep(TimeSpan.FromMinutes(1));  //修改想中间间隔的时间,分钟为单位,20分钟就输入20,测试方便1分钟
            }
        }

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        private const int SW_HIDE = 0;
    }
}

如果你是使用 Visual Studio 创建的项目,可以在“新建项目”对话框中选择“Windows Forms App (.NET Framework)”或“Windows Forms App (.NET)”模板来创建一个 Windows 应用程序项目。

然后将以下代码拷贝进去,运行后,会弹出一个提示框,回车后,就开始自动计时,间隔一段时间后就会自动弹出屏保,提示眼睛休息一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wuhanwhite

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值