csharp SingleInstance Windows窗体 - 托盘栏中的通知图标
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp SingleInstance Windows窗体 - 托盘栏中的通知图标相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
namespace Adacta.USD.Launcher
{
public partial class SettingsForm : Form
{
private const int cShowBaloonForMiliSeconds = 2000;
private const string cAppName = "Unified Service Desk Launcher";
private bool allowVisible; // ContextMenu's Show command used
private bool allowClose; // ContextMenu's Exit command used
public SettingsForm()
{
InitializeComponent();
this.allowVisible = false;
this.allowClose = false;
this.WindowState = FormWindowState.Minimized;
notifyIcon.ContextMenuStrip = menuStrip;
this.testToolStripMenuItem.Click += testToolStripMenuItem_Click;
this.exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;
}
//Show and Hide notification icon from tray if app is visible or hidden (in our case it is always minimized)
protected override void OnResize(EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon.Visible = true;
ShowBaloon("Started");
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon.Visible = false;
}
//base.OnResize(e);
}
//Prevents visibility among open applications (alt-tab)
protected override void SetVisibleCore(bool value)
{
if (!allowVisible)
{
value = false;
if (!this.IsHandleCreated) CreateHandle();
}
base.SetVisibleCore(value);
}
//Prevent app closing if window closed - instead minimize it and show notification in tray bar
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (!allowClose)
{
notifyIcon.Visible = true;
this.Hide();
e.Cancel = true;
}
base.OnFormClosing(e);
}
//Exit application on "Exit" menu option
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
allowClose = true;
Application.Exit();
}
//ReSend last request
private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
USDRequest.SendRequest(null);
notifyIcon.BalloonTipText = "Unified Service Desk Request succeded";
}
catch (Exception exc)
{
notifyIcon.BalloonTipText = exc.Message;
}
notifyIcon.Visible = true;
ShowBaloon(this.notifyIcon.BalloonTipText);
}
public void SendRequest(string[] args)
{
try
{
USDRequest.SendRequestForArguments(args);
ShowBaloon(string.Format("SENT"));
}
catch (Exception exc)
{
ShowBaloon(string.Format("FAILED: {0}", exc.Message));
}
}
private string GetBaloonMessage(string message)
{
return string.Format("{0}: {1}", cAppName, message);
}
public void ShowBaloon(string message, int? showForMiliSecs = null)
{
notifyIcon.BalloonTipText = GetBaloonMessage(message);
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(showForMiliSecs ?? cShowBaloonForMiliSeconds);
}
}
}
using NLog;
using System;
using System.Linq;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
using System.Collections.ObjectModel;
namespace Adacta.USD.Launcher
{
static class Program
{
//private static string appGuid = "c0a76b5a-12ab-45c5-b9d9-d693faa6e7b9";
//public static SettingsForm settings;
//static Mutex mutex = new Mutex(true, appGuid);
private static Logger logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Commented part is an alternative aproach - if you need to get arguments it is not recommended
//if (mutex.WaitOne(TimeSpan.Zero, true))
//{
// logger.Trace("Adacta.USD.Launcher started");
// logger.Trace("1 Adacta.USD.Launcher params:: {0}", string.Join("", args));
// //Initialize Form and Run App
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// settings = new SettingsForm();
// USDRequest.SetSettingsForm(settings);
// USDRequest.SendRequestForArguments(args);
// Application.Run(settings);
// mutex.ReleaseMutex();
//}
//logger.Trace("2 Adacta.USD.Launcher params:: {0}", string.Join("", args));
//USDRequest.SendRequestForArguments(args);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SingleInstanceController controller = new SingleInstanceController();
controller.Run(args);
}
public class SingleInstanceController : WindowsFormsApplicationBase
{
public SingleInstanceController()
{
IsSingleInstance = true;
StartupNextInstance += this_StartupNextInstance;
}
private void SendRequest(string[] args)
{
SettingsForm form = MainForm as SettingsForm; //My derived form type
form.SendRequest(args);
}
void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
{
SendRequest(e.CommandLine?.ToArray());
}
private string[] _initialArguments;
protected override bool OnInitialize(ReadOnlyCollection<string> commandLineArgs)
{
_initialArguments = commandLineArgs.ToArray();
return base.OnInitialize(commandLineArgs);
}
protected override void OnCreateMainForm()
{
MainForm = new SettingsForm();
SendRequest(_initialArguments);
}
}
}
}
以上是关于csharp SingleInstance Windows窗体 - 托盘栏中的通知图标的主要内容,如果未能解决你的问题,请参考以下文章
Autofac RegisterInstance 与 SingleInstance
SingleInstance 的简单问题 LaunchMode