利用c#製作托盤程式,並禁止多個應用執行個體運行

來源:互聯網
上載者:User

托盤程式的製作:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.