c# 托盘程序

落日映苍穹つ 2024-02-17 14:07 130阅读 0赞

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;

namespace test
{
public partial class Form1 : Form
{
public Form1() //过早函数
{
InitializeComponent();

/// 初始化推盘程序参数
this.notifyIconServer.ContextMenuStrip = this.txtMenu; //托盘关联的菜单
this.notifyIconServer.Text = “测试托盘程序”;
this.notifyIconServer.Icon = new Icon(Application.StartupPath + @”/tray.ico”);
this.notifyIconServer.Visible = false; //隐藏托盘

this.ShowInTaskbar = false; //设置不再任务栏中显示图标
}
///


/// 是否关闭窗口
///

private bool IsClose = false;
///
/// 托盘图像
///

private NotifyIcon notifyIconServer = new NotifyIcon();

///


/// 获取或设置是否关闭主窗体
///

private bool getIsColse
{
set { this.IsClose = value; }
get { return this.IsClose; }
}
///
/// 快捷菜单打开事件
///

private void menuOpen_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized; //指定窗体最大化
}
///
/// 快捷菜单关闭事件
///

private void mentClose_Click(object sender, EventArgs e)
{
this.getIsColse = true;
this.Close();
}

///


/// 窗体大小改变时激发的事件
///

private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized)
{
this.notifyIconServer.Visible = false; //隐藏托盘
}
if (this.WindowState == FormWindowState.Minimized)
{
this.notifyIconServer.Visible = true; //显示托盘
}
}

///


/// 窗体关闭前激发的事件
///

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.getIsColse)
{
this.notifyIconServer.Visible = false;
}
else
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.notifyIconServer.Visible = true;
}

}

}
}

发表评论

表情:
评论列表 (有 0 条评论,130人围观)

还没有评论,来说两句吧...

相关阅读

    相关 swing 托盘

    参考文章:[swing 托盘][swing] [JFrame实现系统托盘功能缩放在任务栏,窗体图标自定义][JFrame] [swing 最小化成托盘][swing 1]

    相关 C++刷新托盘程序

    项目中需要重启一个托盘程序。但是重启之后,任务栏的托盘程序图标增加了一个,原来的图标并没有消失。 原来任务栏的托盘程序图标,当鼠标在上面移动过之后才会刷新。所以一个解决办法