Evo ti mali primer za koriscenje tray ikone i koji u sebi sadrzi gore dati kod za pokretanje programa pri podizanju windowsa.
Code:
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using Microsoft.Win32;
namespace WindowsApplication1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ContextMenuStrip cm = new ContextMenuStrip();
cm.Items.Add("About...", null, new EventHandler(OnAbout));
cm.Items.Add("-");
cm.Items.Add("Configuration...", null, new EventHandler(OnConfiguration));
cm.Items.Add("-");
menuStartWithWindows = cm.Items.Add("Start With Windows", null, new EventHandler(OnStartWithWindows)) as ToolStripMenuItem;
cm.Items.Add("-");
cm.Items.Add("Exit", null, new EventHandler(OnExit));
cm.Opening += new System.ComponentModel.CancelEventHandler(OnMenuOpening);
NotifyIcon notifyIcon = new NotifyIcon();
notifyIcon.ContextMenuStrip = cm;
notifyIcon.Icon = Properties.Resources.app_icon;
notifyIcon.MouseDoubleClick += new MouseEventHandler(OnAbout);
notifyIcon.Visible = true;
notifyIcon.Text = "Test";
Application.Run();
notifyIcon.Visible = false;
notifyIcon.Dispose();
}
private static ToolStripMenuItem menuStartWithWindows = null;
private static void OnAbout(object sender, EventArgs e)
{
MessageBox.Show("Test", "About", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private static void OnConfiguration(object sender, EventArgs e)
{
Form form = new Form();
form.ShowDialog();
form.Dispose();
}
private static void OnMenuOpening(object sender, System.ComponentModel.CancelEventArgs e)
{
menuStartWithWindows.Checked = Program.StartWithWindows;
}
private static void OnStartWithWindows(object sender, EventArgs e)
{
Program.StartWithWindows = !Program.StartWithWindows;
}
private static void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
public static bool StartWithWindows
{
get
{
bool startWithWindows = false;
try {
RegistryKey key = Registry.LocalMachine.CreateSubKey(key_startup);
if(key != null) {
string fileName = Convert.ToString(key.GetValue(value_startup, ""));
if(fileName == Application.ExecutablePath) {
startWithWindows = true;
} else {
key.DeleteValue(value_startup);
}
}
} catch { }
return startWithWindows;
}
set
{
try {
RegistryKey key = Registry.LocalMachine.CreateSubKey(key_startup);
if(key != null) {
if(value) {
key.SetValue(value_startup, Application.ExecutablePath);
} else {
key.DeleteValue(value_startup);
}
}
} catch { }
}
}
private static string key_startup = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
private static string value_startup = System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath);
}
}