Minimize to sys tray

  • Thread starter Thread starter Ricardo
  • Start date Start date
I think u can use a NotifyIcon from System.Windows.Form ,,and add that to ur
application
 
You need to do this (you can change the defined names for these, but for
tutorial sake, use what I have stated below until you get the hang of it):

Add a context menu to your form called "IconMenu"
Create two options in this menu. Once called "MenuOpen" the other
called "MenuExit"

in your form constructor add this:
this.SizeChanged += new EventHandler(Form1_SizeChanged);
this.MenuOpen.Click += new System.EventHandler(this.MenuOpen_Click);
this.MenuExit.Click += new System.EventHandler(this.MenuExit_Click);




now add this code to the form class:
public NotifyIcon TrayIcon;
private bool Shown;

private void Form1_SizeChanged(object sender, System.EventArgs e)
{
if (Shown & this.WindowState == FormWindowState.Minimized)
{
Shown = false;
this.ShowInTaskbar = false;
TrayIcon.Visible = true;
}
}

private void MenuOpen_Click(object sender, System.EventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
Shown = true;
TrayIcon.Visible = false;
}

private void MenuExit_Click(object sender, System.EventArgs e)
{
TrayIcon.Visible = false;
this.Close();
System.Environment.Exit(0);
}


and finally add this to your Form1_load event:
TrayIcon = new NotifyIcon();
Shown = true;
TrayIcon.Visible = false;
TrayIcon.Icon = this.Icon;
TrayIcon.Text = "Aplication Running...";
TrayIcon.ContextMenu = this.IconMenu;
 
Back
Top