Monday, July 14, 2008

Minimize App to System Tray

Recently I made a Tata Indicom web dialer. The problem I faced was that Tata Indicom (my ISP) logs me out if I dont use internet for a few hours. When back I need re login and then my internet will be up. I wished to automate it so I used the 'Axshdocvw' way to set the user name and password whenever I reached the login page. That done, the app was supposed to run as a service. I did not want to code that much so just ran the app. The problem was that it stayed in my task bar and looked odd. So I wanted it to minimize to system tray and here's how its done:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MinimizeToTray
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

// make the icon invisible to start with
notifyIcon1.Visible = false;
}

private void Form1_Resize(object sender, EventArgs e)
{
notifyIcon1.BalloonTipTitle = "Minimize to Tray";
notifyIcon1.BalloonTipText = "You have successfully minimized your App to tray.";

if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}

}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;

}

private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
// Create Menu here
}
}
}
}

No comments: