天天看點

C#實作托盤程式并禁止多個應用執行個體運作的方法C#程式在托盤背景運作

托盤程式的制作:

1.把NotifyIcon控件拉一個到窗體上,并設定NotifyIcon的Icon(很重要!否則運作後看不到效果)

2.窗體關閉時,将程式最小化到系統托盤上

?

1 2 3 4 5 6 7 8

private

void

Form1_FormClosing(

object

sender, FormClosingEventArgs e)

{

//MessageBox.Show("程式将最小化到系統托盤區");

e.Cancel =

true

;

// 取消關閉窗體

this

.Hide();

this

.ShowInTaskbar =

false

;

//取消窗體在工作列的顯示

this

.notifyIcon1.Visible =

true

;

//顯示托盤圖示

}

3.放一個上下文菜單,添加幾個基本項,"顯示主窗體","退出" ,将這個菜單挂到NotifyIcon上

?

1 2 3 4 5 6 7 8 9 10 11

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.左鍵單擊托盤圖示時,顯示主窗體,右擊時當然是彈出上面設定的菜單

?

1 2 3 4 5 6 7 8 9

private

void

notifyIcon1_MouseClick(

object

sender, MouseEventArgs e)

{

if

(e.Button == MouseButtons.Left)

{

this

.Show();

this

.ShowInTaskbar =

true

;

this

.notifyIcon1.Visible =

false

;

}

}

防止這個程式同時運作多個

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

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#程式設計有所幫助。

C#程式在托盤背景運作

源代碼如下:

請在工具欄把notifyIcon控件拖入窗體再使用:

C#代碼  

C#實作托盤程式并禁止多個應用執行個體運作的方法C#程式在托盤背景運作
  1.   //隐藏窗體  
  2.         private bool windowCreate = true;  
  3.         private void toolStripMenuItem1_Click(object sender, EventArgs e) //這是菜單選項的一個item點選事件  
  4.         {  
  5.             if (windowCreate)  
  6.             {  
  7.                 base.Visible = false;  
  8.                 windowCreate = false;  
  9.             }  
  10.             this.Hide();  
  11.             base.OnActivated(e);   
  12.         }  
  13.         //顯示回窗體(notifyIcon控件輕按兩下事件,注:請選擇一個ico圖示,這樣隐藏後在右下角顯示有相關圖示)  
  14.         private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)  
  15.         {  
  16.             if (this.Visible == true)  
  17.             {  
  18.                 this.Hide();  
  19.                 this.ShowInTaskbar = false;  
  20.             }  
  21.             else  
  22.             {  
  23.                 this.Visible = true;  
  24.                 this.ShowInTaskbar = true;  
  25.                 this.WindowState = FormWindowState.Normal;  
  26.                 //this.Show();  
  27.                 this.BringToFront();  
  28.                 windowCreate = true;  
  29.             }  
  30.         }  

若想直接打開程式後,自動背景運作的話,以下代碼實作:

load函數中

this.Hide();

this.ShowInTaskbar = false;