码迷,mamicode.com
首页 > Windows程序 > 详细

C#最小化到托盘+双击托盘恢复+禁止运行多个该程序

时间:2018-04-06 00:14:18      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:bar   ica   ini   rms   this   text   enable   zed   event   

托盘程序的制作:

1.添加notifyIcon控件,并添加Icon,否则托盘没有图标(托盘右键菜单也可直接在属性里添加);
2.响应Form的Resize或SizeChanged消息:

// Hide to system tray  
private void Form1_Resize(object sender, EventArgs e)  
{  
    if (this.WindowState == FormWindowState.Minimized)  
    {  
        this.Hide();  
        this.ShowInTaskbar = false;  
        this.notifyIcon.Visible = true;  
    }  
}  

3.双击托盘图标恢复需要响应notifyIcon的DoubleClick消息:

// Show from system tray  
private void notifyIcon_DoubleClick(object sender, EventArgs e)  
{  
    if (this.WindowState == FormWindowState.Minimized)  
    {  
        this.Show();  
        this.WindowState = FormWindowState.Normal;  
        this.ShowInTaskbar = true;
        notifyIcon.Visible = false;  
    }  
}  

 防止这个程序同时运行多个 编辑 Program.cs 文件  (参考1:C#如何防止程序多次运行的技巧 参考2:[C# 开发技巧]如何防止程序多次运行)

using System;
using System.Windows.Forms;
using System.Threading;

namespace WhoOnline
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Mutex m = new Mutex(false, "Product_Index_Cntvs", out bool bCreatedNew);
            if (bCreatedNew)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("应用程序已经在运行中...");
                System.Threading.Thread.Sleep(1000);
                //  终止此进程并为基础操作系统提供指定的退出代码。
                System.Environment.Exit(1);
            }
        }
    }
}

 

C#最小化到托盘+双击托盘恢复+禁止运行多个该程序

标签:bar   ica   ini   rms   this   text   enable   zed   event   

原文地址:https://www.cnblogs.com/guyk/p/8724887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!