国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現托盤程序并禁止多個應用實例運行的方法

2020-01-24 01:21:47
字體:
來源:轉載
供稿:網友

本文實例講述了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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌恰县| 思南县| 航空| 绥芬河市| 张家界市| 卫辉市| 平邑县| 屯门区| 开江县| 本溪市| 曲阜市| 嘉义县| 木里| 盐山县| 股票| 叶城县| 西峡县| 建湖县| 凉城县| 邢台县| 新田县| 本溪市| 乐昌市| 万宁市| 温泉县| 舟山市| 巫溪县| 方城县| 万年县| 尤溪县| 汉源县| 绥德县| 阿瓦提县| 华亭县| 柞水县| 西充县| SHOW| 汉寿县| 吴旗县| 建平县| 南平市|