C#实现托盘程序并禁止多个应用实例运行的方法

秒速五厘米 2022-05-29 09:55 252阅读 0赞

托盘程序的制作:

1.把NotifyIcon控件拉一个到窗体上,并设置NotifyIcon的Icon(很重要!否则运行后看不到效果)

2.窗体关闭时,将程序最小化到系统托盘上

?










1


2


3


4


5


6


7


8



private
void
Form1_FormClosing(
object
sender, FormClosingEventArgs e)


{


  
//MessageBox.Show(“程序将最小化到系统托盘区”);


  
e.Cancel =
true
;
// 取消关闭窗体


  
this
.Hide();


  
this
.ShowInTaskbar =
false
;
//取消窗体在任务栏的显示


  
this
.notifyIcon1.Visible =
true
;
//显示托盘图标


}

3.放一个上下文菜单,添加几个基本项,”显示主窗体”,”退出” ,将这个菜单挂到NotifyIcon上

?










1


2


3


4


5


6


7


8


9


10


11



private
void
menuShow_Click(
object
sender, EventArgs e)


{


  
this
.Show();


  
this
.ShowInTaskbar =
true
;


  
this
.notifyIcon1.Visible =
false
;


}


private
void
menuExit_Click(
object
sender, EventArgs e)


{


  
this
.Dispose(
true
);


  
Application.ExitThread();


}

4.左键单击托盘图标时,显示主窗体,右击时当然是弹出上面设置的菜单

?










1


2


3


4


5


6


7


8


9



private
void
notifyIcon1_MouseClick(
object
sender, MouseEventArgs e)


{


  
if
(e.Button == MouseButtons.Left)


  
{


    
this
.Show();


    
this
.ShowInTaskbar =
true
;


    
this
.notifyIcon1.Visible =
false
;


  
}


}

防止这个程序同时运行多个

?










1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25



using
System;


using
System.Collections.Generic;


using
System.Windows.Forms;


using
System.Threading;


namespace
LuceneTest


{


  
static
class
Program


  
{


    
/// <summary>


    
/// 应用程序的主入口点。


    
/// </summary>


    
[STAThread]


    
static
void
Main()


    
{


      
bool
bCreatedNew;


      
Mutex m =
new
Mutex(
false
,
“Product_Index_Cntvs”
,
out
bCreatedNew);


      
if
(bCreatedNew)


      
{


        
Application.EnableVisualStyles();


        
Application.SetCompatibleTextRenderingDefault(
false
);


        
Application.Run(
new
Form1());


      
}


    
}


  
}


}

希望本文所述对大家C#程序设计有所帮助。

C#程序在托盘后台运行

源代码如下:

请在工具栏把notifyIcon控件拖入窗体再使用:

C#代码

收藏代码

  1. //隐藏窗体
  2. private bool windowCreate = true;
  3. private void toolStripMenuItem1_Click(object sender, EventArgs e) //这是菜单选项的一个item点击事件
  4. {
  5. if (windowCreate)
  6. {
  7. base.Visible = false;
  8. windowCreate = false;
  9. }
  10. this.Hide();
  11. base.OnActivated(e);
  12. }
  13. //显示回窗体(notifyIcon控件双击事件,注:请选择一个ico图标,这样隐藏后在右下角显示有相关图标)
  14. private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  15. {
  16. if (this.Visible == true)
  17. {
  18. this.Hide();
  19. this.ShowInTaskbar = false;
  20. }
  21. else
  22. {
  23. this.Visible = true;
  24. this.ShowInTaskbar = true;
  25. this.WindowState = FormWindowState.Normal;
  26. //this.Show();
  27. this.BringToFront();
  28. windowCreate = true;
  29. }
  30. }

若想直接打开程序后,自动后台运行的话,以下代码实现:

load函数中

this.Hide();
this.ShowInTaskbar = false;

发表评论

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

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

相关阅读

    相关 第一C#应用程序

     3.1 "Hello World" 代码   讨论编辑器有点离题 ,让我们把话题转回到一个非常出名的小应用程序。这个最短的[C\][C]版本应用程序见清单3.1.把它存起来

    相关 C++刷新托盘程序

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