C#实现托盘程序并禁止多个应用实例运行的方法
2015-11-27 10:45
851 查看
本文实例讲述了C#实现托盘程序并禁止多个应用实例运行的方法。分享给大家供大家参考,具体如下:
托盘程序的制作:
1.把NotifyIcon控件拉一个到窗体上,并设置NotifyIcon的Icon(很重要!否则运行后看不到效果)
2.窗体关闭时,将程序最小化到系统托盘上
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { //MessageBox.Show("程序将最小化到系统托盘区"); e.Cancel = true; // 取消关闭窗体 this.Hide(); this.ShowInTaskbar = false;//取消窗体在任务栏的显示 this.notifyIcon1.Visible = true;//显示托盘图标 }
3.放一个上下文菜单,添加几个基本项,"显示主窗体","退出" ,将这个菜单挂到NotifyIcon上
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.左键单击托盘图标时,显示主窗体,右击时当然是弹出上面设置的菜单
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { this.Show(); this.ShowInTaskbar = true; this.notifyIcon1.Visible = false; } }
防止这个程序同时运行多个
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#程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- [Android]在代码里运行另一个程序的方法
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- C#中this的用法集锦
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#生成饼形图及添加文字说明实例代码
- C#判等对象是否相等的方法汇总
- C#简单的向量用法实例教程
- C#实现基于链表的内存记事本实例