- To get started, open an existing C# Windows form (or create a new one).
- Open the Visual Studio Toolbox.
- Drag a NotifyIcon control onto the form. The control will named notifyIcon1 by default and placed below the form because it has no visual representation on the form itself.
- Set the NotifyIcon control's Text property to the name you want to appear when the user pauses the mouse over the application's icon. For example, this value could be "KillerApp 1.0".
- Set the control's Icon property to the icon that you want to appear in the System Tray.
- Add an event handler for the form's Resize event that will hide the application when it's minimized. That way, it won't appear on the task bar.
private void Form1_Resize(object sender, System.EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
Hide();
}
Add an event handler for the NotifyIcon.DoubleClick event and code it as follows so that the application will be restored when the icon is double-clicked.
private void notifyIcon1_DoubleClick(object sender,
System.EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
Adding a Context Menu- From the Visual Studio Toolbox, drag a ContextMenu control onto the form.
- Right-click the ContextMenu control and select the Edit Menu.option.
- Type in the options that you want to appear in your context menu. For example, you can add options such as Restore and Close Application. As with any menu, double-click the menu item to create and code each item's handler. As an example, you could copy the code from the form's DoubleClick handler into the context menu's Restore handler and for the Close Application menu item; simply call the form's Close method.
- Finally, set the NotifyIcon control's ContextMenu property to the new context menu you just created by selecting the menu from the drop-down list. Figure 2 shows a simple Tray context menu.
Additional notes from the comments in the article:
-->
And if you do not want show the icon when the application run but show it when it be hidden, you can set the
notifyicon.visible = false.
and when the form Minimized
set notifyicon.visble = true
and when in the double click event handler, set the visible to false again.
-->
to capture the X button, use the _FormClosing event.
private void _FormClosing(object sender, FormClosingEventArgs args)
Make sure you check the reason matches the user hitting the X:
if (args.CloseReason == CloseReason.UserClosing)
-->
The default setting for this check box is cleared, allowing multiple instances of the application to be run. You can do this by following the steps below:
- Right-Click on MyProject inside SolutionExplorer --> Open
- Select Application tab
- Set the Make single instance application checkbox Checked/Unchecked as per the requirement.