首页 > 编程语言 >C# 实现程序最小化到托盘

C# 实现程序最小化到托盘

时间:2023-01-05 14:01:37浏览次数:39  
标签:C# void System menuItem 托盘 最小化 new Click notifyIcon1


1.设置WinForm窗体属性showinTask=false

2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标。

3.添加窗体最小化事件(首先需要添加事件引用):

this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged); 

//上面一行是主窗体InitializeComponent()方法中需要添加的引用

private void Form1_SizeChanged(object sender, EventArgs e)
{
if(this.WindowState == FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible=true;
}
}


4.添加点击图标事件(首先需要添加事件引用):

private void notifyIcon1_Click(object sender, EventArgs e) 
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}



5.可以给notifyIcon添加右键菜单:

主窗体中拖入一个ContextMenu控件NicontextMenu,点中控件,在上下文菜单中添加菜单,notifyIcon1的ContextMenu行为中选中NicontextMenu 作为上下文菜单。

this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); 
this.NicontextMenu = new System.Windows.Forms.ContextMenu();
this.menuItem_Hide = new System.Windows.Forms.MenuItem();
this.menuItem_Show = new System.Windows.Forms.MenuItem();
this.menuItem_Aubot = new System.Windows.Forms.MenuItem();
this.menuItem_Exit = new System.Windows.Forms.MenuItem();


this.notifyIcon1.ContextMenu = this.NicontextMenu;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject( "NotifyIcon.Icon ")));
this.notifyIcon1.Text = " ";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);


this.NicontextMenu.MenuItems.AddRange(

new System.Windows.Forms.MenuItem[]
{
this.menuItem_Hide,
this.menuItem_Show,
this.menuItem_Aubot,
this.menuItem_Exit
}
);

//
// menuItem_Hide
//
this.menuItem_Hide.Index = 0;
this.menuItem_Hide.Text = "隐藏 ";
this.menuItem_Hide.Click += new System.EventHandler(this.menuItem_Hide_Click);
//
// menuItem_Show
//
this.menuItem_Show.Index = 1;
this.menuItem_Show.Text = "显示 ";
this.menuItem_Show.Click += new System.EventHandler(this.menuItem_Show_Click);
//
// menuItem_Aubot
//
this.menuItem_Aubot.Index = 2;
this.menuItem_Aubot.Text = "关于 ";
this.menuItem_Aubot.Click += new System.EventHandler(this.menuItem_Aubot_Click);
//
// menuItem_Exit
//
this.menuItem_Exit.Index = 3;
this.menuItem_Exit.Text = "退出 ";
this.menuItem_Exit.Click += new System.EventHandler(this.menuItem_Exit_Click);

protected override void OnClosing(CancelEventArgs e)
{
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}
protected override void OnClosing(CancelEventArgs e)
{
//this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}

private void CloseCtiServer()
{
timer.Enabled = false;
DJ160API.DisableCard();
this.NotifyIcon.Visible = false;
this.Close();
this.Dispose();
Application.Exit();
}

private void HideCtiServer()
{
this.Hide();
}

private void ShowCtiServer()
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();

}
private void CtiManiForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.CloseCtiServer();
}

private void menuItem_Show_Click(object sender, System.EventArgs e)
{
this.ShowCtiServer();
}

private void menuItem_Aubot_Click(object sender, System.EventArgs e)
{

}

private void menuItem_Exit_Click(object sender, System.EventArgs e)
{
this.CloseCtiServer();
}

private void menuItem_Hide_Click(object sender, System.EventArgs e)
{
this.HideCtiServer();
}

private void CtiManiForm_SizeChanged(object sender, System.EventArgs e)
{
if(this.WindowState == FormWindowState.Minimized)
{
this.HideCtiServer();
}
}

private void notifyIcon1_DoubleClick(object sender,System.EventArgs e)
{
this.ShowCtiServer();
}

标签:C#,void,System,menuItem,托盘,最小化,new,Click,notifyIcon1
From: https://blog.51cto.com/kenkao/5991041

相关文章

  • C#中实现byte[]与任意对象互换(服务端通讯专用)
    C++中,我们可以非常方便的将网络通讯接收来的char*缓冲区转成任意类型的结构体,并从中提取必要信息,只需要一个结构体类型指针的强制转换即可。但是在C#中,所有涉及到内存及指针......
  • 记 对接拼多多官方代报 辽宁电子口岸联达通客户端 ic卡加签版
    对接前准备详见pdd文档https://open.pinduoduo.com/application/document/browse?idStr=EE386BF9BCC6EEC1总结需要我们做的有1 实现我们持有ic卡机器上的加签接口功能......
  • Amber22 安装过程miniconda报错处理
    最近在安装amber22时,使用其官方说明安装,但在安装miniconda这一步出错,致使amber22安装不成功!报错如下:CondaFileIOError:'/home/lbm/amber22/amber22_src/build/CMakeFi......
  • 【web】Emscripten一些注意事项
    中文网址https://cntofu.com/book/150/zh/ch1-quick-guide/ch1-01-install.md编译选项-sSAFE_HEAP=1可以检测到内存未对齐的错误,但是会影响性能,应该只在测试时使用-s......
  • linux服务器cup100%问题排查
    一、出现问题在发现公司门禁服务无法开门的第一时间,去线上服务器上查看了一下进程的运行情况,具体运行如下:   第一次在查看的时候发现并没有我需要的服务entrancegua......
  • RHEL/CentOS yum 源问题
    yum源问题1RHEL本地iso设置为源https://www.programminghunter.com/article/39032455888/创建挂载点[root@PC1home]#mkdir-p/media/cdrom进入/etc/yum.re......
  • CSS - 设置内边距padding,复合写法,内边距会影响盒子大小的问题
    1.设置内边距/*设置上内边距*/padding-top:20px;/*设置下内边距*/padding-bottom:20px;/*设置左内边距*/padding-left:20px;/*设置右内边距*/padding-right......
  • NC16611 [NOIP2009]最优贸易
    题目链接题目题目描述C国有n个大城市和m条道路,每条道路连接这n个城市中的某两个城市。任意两个城市之间最多只有一条道路直接相连。这m条道路中有一部分为单向通行的道......
  • profession computing -- ethic
      Equality,DiversityandComputing:ImplicitBiasandStereotypeThreatJusticeandDiscrimination      SurveillanceandPrivacy:  ......
  • c++ 查找目录下的子目录及文件
    c++读取指定目录下的所有目录名称+文件名称-远征i-博客园(cnblogs.com) 文件句柄的类型long如果不行试试longlong 另外:使用了批处理,这篇很好CMD批处理循环......