Created
August 27, 2020 10:52
-
-
Save tkouba/bb4c65971db92f19ffe2a6b2b511a913 to your computer and use it in GitHub Desktop.
System.Windows.Forms.ApplicationContext enhancement with tray icon and menu.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.Text; | |
using System.Windows.Forms; | |
namespace MyVpnManager.Base | |
{ | |
/// <summary> | |
/// Specifies the contextual information about an application thread with <see cref="Icon"/> and <see cref="ContextMenuStrip"/> | |
/// </summary> | |
public abstract class TrayIconApplicationContext : ApplicationContext | |
{ | |
private readonly NotifyIcon notifyIcon; | |
/// <summary> | |
/// Object is disposed | |
/// </summary> | |
public bool IsDisposed { get; private set; } | |
/// <summary> | |
/// Gets the shortcut menu associated with the notify icon. | |
/// </summary> | |
public ContextMenuStrip ContextMenuStrip { get; private set; } | |
/// <summary> | |
/// Gets or sets the <see cref="ToolTip"/> text displayed when the mouse pointer rests on a notification area icon. | |
/// </summary> | |
public string Text | |
{ | |
get { return notifyIcon?.Text; } | |
set { if (notifyIcon != null && !IsDisposed) notifyIcon.Text = value; } | |
} | |
public bool Visible | |
{ | |
get { return notifyIcon?.Visible ?? false; } | |
set { if (notifyIcon != null && !IsDisposed) notifyIcon.Visible = value; } | |
} | |
public Icon Icon | |
{ | |
get { return notifyIcon?.Icon; } | |
set { if (notifyIcon != null && !IsDisposed) notifyIcon.Icon = value; } | |
} | |
public TrayIconApplicationContext() | |
{ | |
ContextMenuStrip = new ContextMenuStrip(); | |
Application.ApplicationExit += Application_ApplicationExit; | |
notifyIcon = new NotifyIcon | |
{ | |
ContextMenuStrip = ContextMenuStrip, | |
Text = Application.ProductName, | |
Visible = true, | |
Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath) | |
}; | |
notifyIcon.MouseDoubleClick += NotifyIcon_MouseDoubleClick; | |
notifyIcon.MouseClick += NotifyIcon_MouseClick; | |
} | |
// | |
// Summary: | |
// Displays a balloon tip with the specified title, text, and icon in the taskbar | |
// for the specified time period. | |
// | |
// Parameters: | |
// timeout: | |
// The time period, in milliseconds, the balloon tip should display.This parameter | |
// is deprecated as of Windows Vista. Notification display times are now based on | |
// system accessibility settings. | |
// | |
// tipTitle: | |
// The title to display on the balloon tip. | |
// | |
// tipText: | |
// The text to display on the balloon tip. | |
// | |
// tipIcon: | |
// One of the System.Windows.Forms.ToolTipIcon values. | |
// | |
// Exceptions: | |
// T:System.ArgumentOutOfRangeException: | |
// timeout is less than 0. | |
// | |
// T:System.ArgumentException: | |
// tipText is null or an empty string. | |
// | |
// T:System.ComponentModel.InvalidEnumArgumentException: | |
// tipIcon is not a member of System.Windows.Forms.ToolTipIcon. | |
public void ShowBalloonTip(int timeout, string tipTitle, string tipText, ToolTipIcon tipIcon) | |
{ | |
notifyIcon.ShowBalloonTip(timeout, tipTitle, tipText, tipIcon); | |
} | |
/// <summary> | |
/// Displays a balloon tip in the taskbar for the specified time period. | |
/// </summary> | |
/// <param name="timeout"> | |
/// The time period, in milliseconds, the balloon tip should display.This parameter | |
/// is deprecated as of Windows Vista. Notification display times are now based on | |
/// system accessibility settings. | |
/// </param> | |
/// <exception cref="ArgumentOutOfRangeException">timeout is less than 0.</exception> | |
public void ShowBalloonTip(int timeout) | |
{ | |
notifyIcon.ShowBalloonTip(timeout); | |
} | |
/// <summary> | |
/// Releases the unmanaged resources used by the <see cref="TrayIconApplicationContext"/> | |
/// and optionally releases the managed resources. | |
/// </summary> | |
/// <param name="disposing"> | |
/// true to release both managed and unmanaged resources; false to release only unmanaged resources. | |
/// </param> | |
protected override void Dispose(bool disposing) | |
{ | |
if (!IsDisposed) | |
{ | |
IsDisposed = true; | |
if (disposing) | |
{ | |
notifyIcon.Dispose(); | |
ContextMenuStrip.Dispose(); | |
} | |
} | |
base.Dispose(disposing); | |
} | |
private void Application_ApplicationExit(object sender, EventArgs e) | |
{ | |
OnApplicationExit(e); | |
} | |
private void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) | |
{ | |
OnTrayIconDoubleClick(e); | |
} | |
private void NotifyIcon_MouseClick(object sender, MouseEventArgs e) | |
{ | |
OnTrayIconClick(e); | |
} | |
protected virtual void OnApplicationExit(EventArgs e) | |
{ } | |
protected virtual void OnTrayIconClick(MouseEventArgs e) | |
{ } | |
protected virtual void OnTrayIconDoubleClick(MouseEventArgs e) | |
{ } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment