欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)托盤程序并禁止多個(gè)應(yīng)用實(shí)例運(yùn)行的方法

 更新時(shí)間:2015年11月27日 10:45:33   作者:Jimmy.Yang  
這篇文章主要介紹了C#實(shí)現(xiàn)托盤程序并禁止多個(gè)應(yīng)用實(shí)例運(yùn)行的方法,涉及C#中NotifyIcon控件的使用及設(shè)置標(biāo)志位控制程序只運(yùn)行一個(gè)的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#實(shí)現(xiàn)托盤程序并禁止多個(gè)應(yīng)用實(shí)例運(yùn)行的方法。分享給大家供大家參考,具體如下:

托盤程序的制作:

1.把NotifyIcon控件拉一個(gè)到窗體上,并設(shè)置NotifyIcon的Icon(很重要!否則運(yùn)行后看不到效果)

2.窗體關(guān)閉時(shí),將程序最小化到系統(tǒng)托盤上

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  //MessageBox.Show("程序?qū)⒆钚』较到y(tǒng)托盤區(qū)");
  e.Cancel = true; // 取消關(guān)閉窗體
  this.Hide();
  this.ShowInTaskbar = false;//取消窗體在任務(wù)欄的顯示
  this.notifyIcon1.Visible = true;//顯示托盤圖標(biāo)
}

3.放一個(gè)上下文菜單,添加幾個(gè)基本項(xiàng),"顯示主窗體","退出" ,將這個(gè)菜單掛到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.左鍵單擊托盤圖標(biāo)時(shí),顯示主窗體,右擊時(shí)當(dāng)然是彈出上面設(shè)置的菜單

private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
  if (e.Button == MouseButtons.Left)
  {
    this.Show();
    this.ShowInTaskbar = true;
    this.notifyIcon1.Visible = false;
  }
}

防止這個(gè)程序同時(shí)運(yùn)行多個(gè)

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace LuceneTest
{
  static class Program
  {
    /// <summary>
    /// 應(yīng)用程序的主入口點(diǎn)。
    /// </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());
      }
    }
  }
}

希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論