In this article we will see how we can minimize the application to system tray when we click on Minimize Button
Introduction
Some time back working on Windows Application we were assigned a task to minimize the application to System Tray so that it run in background.In order to do it we will see how we can achieve the following.
Objective
- Understand the Notify Icon for Minimizing the Application.
- Understanding Form Re-size event.
- Understanding Notify Icon Mouse Double Click
Using the code
- Create a New Windows Application in VS2010.
- Drag and Drop NotifyIcon from toolbox.
- Now on FormLoad event we will set the BalloonTipText Property to the Text that we want to show.
Notify Icon Properties Used :-
- BalloonTipText Property is used to set the text of the NotifyIcon.
- BalloonTipTitle Property is normally used to name the Application which is minimized.
Form1_Resize Event :-
- This event will help us to recognize whether we have clicked the minimized button and when we click the button we are setting the ShowInTaskbar property of Windows form to False
- We then go ahead make the Notify Icon Visible and set the time to show the Balloontooltip using the BalloonToolTip Method to show the BalloonTooltip and we assign time in milliseconds.
- Over here we are showing to BalloonToolTip for 3 Seconds i.e 3000 milliseconds.
Notify Icon Mouse Double Click
- In this event we follow the reverse process of Form1_Resize Event we first show the Application in task bar by setting the ShowInTaskbar property of Windows form to true
- We then go ahead and invisible the Notify Icon by setting the Visible property to false.
- Then we restate the Windows form to Normal.
Let us Start Implemention// Code Behind.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MinimisedApplicationtoTray
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Setting the text and title of Notify Icon
notifyIcon1.BalloonTipText = "Application Minimised to System Tray";
notifyIcon1.BalloonTipTitle = "Dot Net Funda";
}
private void Form1_Resize(object sender, EventArgs e)
{
// Checking the Windows State if we have clicked Minimised button then we execute the following logic
if (WindowState == FormWindowState.Minimized)
{
ShowInTaskbar = false;
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(3000);
}
}
// Re-setting the window to normal State.
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ShowInTaskbar = true;
notifyIcon1.Visible = false;
WindowState = FormWindowState.Normal;
}
}
}
OutPut Screen 1 : Running Application
Output 2 :- Minimizing the Application to System Tray
Conclusion
In order to have an Icon for the Notify Icon we can only use .ICO file type extension.