任务栏窗口中的 C# 通知7.



我想用 c# winforms 编写一个简单的程序,该程序在恒定时间(例如在 13:00 和 21:00(向用户(在任务栏窗口 7 或 10 中(显示通知。

我该怎么做?

基本上,

如果DateTime.Now(当前小时(等于特定小时,则显示通知图标。

您应该在计时器/任务中运行此代码。

if (DateTime.Now.Hour == 13 && DateTime.Now.Minute == 00 || DateTime.Now.Hour == 21 && DateTime.Now.Minute == 00)
{
    var notification = new System.Windows.Forms.NotifyIcon()
    {
        Visible = true,
        Icon = System.Drawing.SystemIcons.Information,
        BalloonTipText = "This is my notify icon",
    };
    notification.ShowBalloonTip(1000);
}

这应该可以做到...

Timer timer = new Timer();
    public Form1()
    {
        InitializeComponent();
        timer.Tick += Timer_Tick;
        timer.Interval = 1000;
        timer.Start();
    }
    int lastNotify = 0;
    private void Timer_Tick(object sender, EventArgs e)
    {
        if ((DateTime.Now.Hour == 16 && lastNotify != 16) || (DateTime.Now.Hour == 21 && lastNotify != 21))
        {
            this.notifyIcon1.BalloonTipText = "Whatever";
            this.notifyIcon1.BalloonTipTitle = "Title";
            this.notifyIcon1.Visible = true;
            this.notifyIcon1.ShowBalloonTip(3);
            lastNotify = DateTime.Now.Hour;
        }
    }

最新更新