C#实现托盘程序并禁止多个应用实例运行的方法
本文实例讲述了C#实现托盘程序并禁止多个应用实例运行的方法。分享给大家供大家参考,具体如下:
托盘程序的制作:
1.把NotifyIcon控件拉一个到窗体上,并设置NotifyIcon的Icon(很重要!否则运行后看不到效果)
2.窗体关闭时,将程序最小化到系统托盘上
privatevoidForm1_FormClosing(objectsender,FormClosingEventArgse)
{
//MessageBox.Show("程序将最小化到系统托盘区");
e.Cancel=true;//取消关闭窗体
this.Hide();
this.ShowInTaskbar=false;//取消窗体在任务栏的显示
this.notifyIcon1.Visible=true;//显示托盘图标
}
3.放一个上下文菜单,添加几个基本项,"显示主窗体","退出",将这个菜单挂到NotifyIcon上
privatevoidmenuShow_Click(objectsender,EventArgse)
{
this.Show();
this.ShowInTaskbar=true;
this.notifyIcon1.Visible=false;
}
privatevoidmenuExit_Click(objectsender,EventArgse)
{
this.Dispose(true);
Application.ExitThread();
}
4.左键单击托盘图标时,显示主窗体,右击时当然是弹出上面设置的菜单
privatevoidnotifyIcon1_MouseClick(objectsender,MouseEventArgse)
{
if(e.Button==MouseButtons.Left)
{
this.Show();
this.ShowInTaskbar=true;
this.notifyIcon1.Visible=false;
}
}
防止这个程序同时运行多个
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Windows.Forms;
usingSystem.Threading;
namespaceLuceneTest
{
staticclassProgram
{
///<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoidMain()
{
boolbCreatedNew;
Mutexm=newMutex(false,"Product_Index_Cntvs",outbCreatedNew);
if(bCreatedNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(newForm1());
}
}
}
}
希望本文所述对大家C#程序设计有所帮助。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下