c# 桌面程序 状态栏图标

使用组件NotifyIcon

图标的图片.ico

notifyIcon = new NotifyIcon();

// 设置图标
notifyIcon.Icon = new System.Drawing.Icon(@"E:\素材\汉堡面包.ico"); // 替换为你的图标路径

鼠标悬停在图标上显示提示信息

notifyIcon.Text = "提示";

效果

 弹出消息

 // 设置ballon提示文本
 notifyIcon.BalloonTipText = "这是一个文字提示!";

 // 设置ballon提示标题
 notifyIcon.BalloonTipTitle = "提示";

 // 设置ballon提示在5秒后显示

 // 让图标显示在系统托盘
 notifyIcon.Visible = true;
 //notifyIcon.ShowBalloonTip(60, "警告", "有设备需要维修", ToolTipIcon.Warning);
 notifyIcon.ShowBalloonTip(500);

效果

 完整代码

public Form1()
{
    notifyIcon = new NotifyIcon();

    // 设置图标
    notifyIcon.Icon = new System.Drawing.Icon(@"E:\素材\汉堡面包.ico"); // 替换为你的图标路径
    // 设置ballon提示文本
    notifyIcon.BalloonTipText = "这是一个文字提示!";

    // 设置ballon提示标题
    notifyIcon.BalloonTipTitle = "提示";

    // 设置ballon提示在5秒后显示

    // 让图标显示在系统托盘
    notifyIcon.Visible = true;
    notifyIcon.ShowBalloonTip(60, "警告", "有设备需要维修", ToolTipIcon.Warning);
    notifyIcon.ShowBalloonTip(500);

    notifyIcon.Text = "提示";

    // 防止程序退出
    //this.Visible = false;
    InitializeComponent();
}

 右键菜单

在Form中增加一个Timer

private NotifyIcon notifyIcon;
private ContextMenuStrip contextMenu;
private Icon normalIcon=new Icon(@"E:\素材\蘑菇.ico");
private Icon blackIcon=new Icon(@"E:\素材\蘑菇灰.ico");
private bool status = true;
private bool blink = false;

/// <summary>
/// 图标右键菜单
/// </summary>
public void NotifyRightClickMenu()
{
    notifyIcon = new NotifyIcon();

    // 设置图标
    notifyIcon.Icon = new System.Drawing.Icon(@"E:\素材\蘑菇48.ico"); // 替换为你的图标路径
    notifyIcon.Text = "提示";
    notifyIcon.Visible = true;

    contextMenu = new ContextMenuStrip();
    var aa = new EventHandler(MenuItem_Show);
    contextMenu.Items.Add("显示", null, new EventHandler(MenuItem_Show));
    contextMenu.Items.Add("隐藏", null, new EventHandler(MenuItem_Hide));
    contextMenu.Items.Add("闪烁", null, new EventHandler(MenuItem_Blink));
    contextMenu.Items.Add("退出", null, new EventHandler(MenuItem_Exit));
    notifyIcon.MouseUp += NotifyIcon_MouseUp;
    //this.timer = new System.Timers.Timer();

}

/// <summary>
/// 弹出右键菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void NotifyIcon_MouseUp(object? sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        contextMenu.Show(MousePosition);
    }
}
/// <summary>
/// 显示图标
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_Show(object? sender, EventArgs e)
{
    notifyIcon.Visible = true;
}
/// <summary>
/// 隐藏图标
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_Hide(object? sender, EventArgs e)
{
    notifyIcon.Visible = false;
}
/// <summary>
/// 闪烁
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_Blink(object? sender, EventArgs e)
{
    if (!blink)
    {
        blink = true;
        timer1.Enabled = true;
        timer1.Start();
    }
    else
    {
        blink = false;
        timer1.Stop();
    }
}
/// <summary>
/// 退出程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_Exit(object? sender, EventArgs e)
{
    Application.Exit();
}

 

posted @ 2024-05-13 09:43  流年sugar  阅读(107)  评论(0编辑  收藏  举报