亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

c#制作托盤程序并禁止多個應用實例運行

標簽:
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)

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());
            }
        }
    }
}
點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消