SystemTray Icon

A

Andrew Mueller

Hello,

I have a C# service that I would like an icon to show in the system tray
upon startup. I put a NotifyIcon on the form and changed the properties as
follows:

Name: ni
ContextMenu: (none)
Icon: {points to location of icon file}
Size: 32,32
Modifiers: Public
Text: EPA Service
Visible: True



Shouldn't it just show up, now?? Or is there something that I have to do to
get it to show?

Thanks in advance,

Andrew
 
B

Bonj

Have this code in the service's main .cs file:
(Replace "icontrans.ico" with your icon, or load from resource (harder)).


HTH


using System;

using System.ServiceProcess;

using System.Windows.Forms;

using System.Drawing;

public class MyService : System.ServiceProcess.ServiceBase

{

private System.Windows.Forms.NotifyIcon TheIcon;

private System.Windows.Forms.ContextMenu TheMenu;

private System.Windows.Forms.MenuItem TheMenuItem;

private System.ComponentModel.IContainer components;


public MyService()

{

this.ServiceName = "MyService";

this.CanStop = true;

this.CanPauseAndContinue = true;

this.AutoLog = true;

}


public static void Main()

{

System.ServiceProcess.ServiceBase.Run(new MyService());

}


protected override void OnStart(string[] args)

{

this.components = new System.ComponentModel.Container();

this.TheMenu = new System.Windows.Forms.ContextMenu();

this.TheMenuItem = new System.Windows.Forms.MenuItem();


this.TheMenu.MenuItems.AddRange(

new System.Windows.Forms.MenuItem[] {this.TheMenuItem});


this.TheMenuItem.Index = 0;

this.TheMenuItem.Text = "E&xit";

this.TheMenuItem.Click += new System.EventHandler(this.TheMenuItem_Click);


this.TheIcon = new System.Windows.Forms.NotifyIcon(this.components);

TheIcon.Icon = new Icon("icontrans.ico");

TheIcon.ContextMenu = this.TheMenu;

TheIcon.Text = "MyService Service";

TheIcon.Visible = true;

}


protected override void Dispose(bool disposing)

{

if(disposing)

if(components != null)

components.Dispose();

base.Dispose(disposing);

}


private void TheMenuItem_Click(object sender, EventArgs e)

{

ServiceController scs = new ServiceController("MyService");

scs.Stop();

}

}



and a standard service installer, this is no different to one without an
icon.
 
A

Andrew Mueller

OK...

The answer is this:

In the services properties, select 'Allow service to interact with the
desktop.'

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top