栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c#制作托盘程序并禁止多个应用实例运行

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
    {
        /// 
        /// 应用程序的主入口点。
        /// 
        [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());
            }
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/231874.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号