Hiding simple program in system tray?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How difficult is it to hide my program in the system tray? It's not something
that's required, but it'd look to add it since the program will be writing an
ascii file every minute. And it wouldn't too elegant to have it in the
taskbar.

Thanks.
 
Hi,

Use the code below for just that, note that I included both declarations and
code, the code can be inserted in the constructor ( part of it can be
inserted by the designer )


[DllImport("user32.dll")]
public static extern int SetWindowLong( IntPtr window, int index, int
value);
[DllImport("user32.dll")]
public static extern int GetWindowLong( IntPtr window, int index);

const int GWL_EXSTYLE = -20;
const int WS_EX_TOOLWINDOW = 0x00000080;
const int WS_EX_APPWINDOW = 0x00040000;

normalIcon = new Icon(this.GetType(),"Normal.ico");

this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
notifyIcon1.Icon = normalIcon;

notifyIcon1.ContextMenu = this.contextMenu1;
notifyIcon1.Visible = true;

this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
this.Visible = false;
this.ShowInTaskbar = false;
iconTimer.Start();

int windowStyle = GetWindowLong(Handle, GWL_EXSTYLE);
SetWindowLong(Handle, GWL_EXSTYLE, windowStyle | WS_EX_TOOLWINDOW);

cheers,
 
its relatively simple:
the trick is to set the form.Opacity = 0

1. drag and drop a System.Windows.Forms.NotifyIcon to the form

2. code sample
public class SysTrayForm : System.Windows.Forms.Form
{
public SysTrayForm() {
InitializeComponent();
this.Hide();
InitializeTray();
}

private System.Windows.Forms.NotifyIcon notifyIcon;
private MenuItem[] muItems = new MenuItem[2];

void InitializeTray() {
notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.Icon = this.Icon;
notifyIcon.Text = "Right Click for the menu";
notifyIcon.Visible = true;

muItems[0] = new MenuItem("Show", new EventHandler(new
System.EventHandler(this.Show)));
muItems[1] = new MenuItem("Exit", new EventHandler(new
System.EventHandler(this.ExitForm)));

ContextMenu notifyiconMnu = new ContextMenu(muItems);
notifyIcon.ContextMenu = notifyiconMnu;
}

void Show(object sender, System.EventArgs e) {
//show some form here, but NOT this one
}

void ExitForm(object sender, System.EventArgs e) {
notifyIcon.Visible = false;
this.Close();
Application.Exit();
}
}
 
Back
Top