閱讀144 返回首頁    go 阿裏雲 go 技術社區[雲棲]


利用c#製作托盤程序,並禁止多個應用實例運行

轉載自:https://www.cnblogs.com/yjmyzz/articles/1021058.html

托盤程序的製作:

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());
}

}

}

}

最後更新:2017-04-02 17:51:24

  上一篇:go Android QQ登錄驗證的小例子
  下一篇:go Android中 檢查網絡連接狀態的變化,無網絡時跳轉到設置界麵