469,312 Members | 2,496 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

notify icon and context menu for Windows Service Application

Hi Friends,

I have developed a Windows service. Now i need icon for this service in
systray and context menu fo this icon. Can i do this?

With regards,
Lalit
Jul 21 '05 #1
7 9725
On Fri, 1 Apr 2005 03:51:03 -0800, Lalit wrote:
I have developed a Windows service. Now i need icon for this service in
systray and context menu fo this icon. Can i do this?


Yes. Create a normal Window Form application without a Form but with a
notify icon and a context menu associated with it. Have it communicate with
your windows service via .NET Remoting, raw sockets or some other way,
whichever is the most suitable for you. At install time, place this
application in the startup folder so that it starts automatically when a
user logs in and displays the notify icon.
Jul 21 '05 #2
Try something like this. Just make sure to add a resource file that
contains your icon file in it:

using System;
using System.Windows.Forms;

namespace TrayIcon
{
class TrayIcon
{

private System.Windows.Forms.ContextMenu trayMenu;
private System.Windows.Forms.MenuItem mnuAbout;
private System.Windows.Forms.MenuItem mnuExit;
private System.Windows.Forms.NotifyIcon trayIcon;

public TrayIcon()
{
InitializeComponent();

Application.Run();
}

private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(TrayIcon)) ;
this.mnuAbout = new System.Windows.Forms.MenuItem();
this.mnuExit = new System.Windows.Forms.MenuItem();
this.trayMenu = new System.Windows.Forms.ContextMenu();
this.trayIcon = new System.Windows.Forms.NotifyIcon();

//
// mnuAbout
//
this.mnuAbout.Index = 8;
this.mnuAbout.Text = "About";
this.mnuAbout.Click += new System.EventHandler(this.MenuClicks);
//
// mnuExit
//
this.mnuExit.Index = 10;
this.mnuExit.Text = "Exit";
this.mnuExit.Click += new System.EventHandler(this.MenuClicks);

//
// trayMenu
//
this.trayMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]
{
this.mnuAbout,
this.mnuExit});
//
// trayIcon
//
this.trayIcon.ContextMenu = this.trayMenu;
this.trayIcon.Icon =
(System.Drawing.Icon)(resources.GetObject("trayIco n.Icon"));
this.trayIcon.Text = "TrayIcon Test";
this.trayIcon.Visible = true;
}

private void MenuClicks(object sender, EventArgs e)
{
if (sender == mnuExit) Application.Exit();
else if (sender == mnuAbout) MessageBox.Show("About TrayIcon test
program");

}

public static void Main(string[] args)
{
new TrayIcon();
}
}
}

Jul 21 '05 #3
Hi,

I have tried this.
I am able to show the Systray icon by setting the allow service to interact
with Desktop property checked.

But the Context menu is not shown for that and i also tried the double click
event of notify icon. If i open the Exe of service directly then context menu
is shown.

Regards,
Lalit

"cmelnick" wrote:
Try something like this. Just make sure to add a resource file that
contains your icon file in it:

using System;
using System.Windows.Forms;

namespace TrayIcon
{
class TrayIcon
{

private System.Windows.Forms.ContextMenu trayMenu;
private System.Windows.Forms.MenuItem mnuAbout;
private System.Windows.Forms.MenuItem mnuExit;
private System.Windows.Forms.NotifyIcon trayIcon;

public TrayIcon()
{
InitializeComponent();

Application.Run();
}

private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(TrayIcon)) ;
this.mnuAbout = new System.Windows.Forms.MenuItem();
this.mnuExit = new System.Windows.Forms.MenuItem();
this.trayMenu = new System.Windows.Forms.ContextMenu();
this.trayIcon = new System.Windows.Forms.NotifyIcon();

//
// mnuAbout
//
this.mnuAbout.Index = 8;
this.mnuAbout.Text = "About";
this.mnuAbout.Click += new System.EventHandler(this.MenuClicks);
//
// mnuExit
//
this.mnuExit.Index = 10;
this.mnuExit.Text = "Exit";
this.mnuExit.Click += new System.EventHandler(this.MenuClicks);

//
// trayMenu
//
this.trayMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]
{
this.mnuAbout,
this.mnuExit});
//
// trayIcon
//
this.trayIcon.ContextMenu = this.trayMenu;
this.trayIcon.Icon =
(System.Drawing.Icon)(resources.GetObject("trayIco n.Icon"));
this.trayIcon.Text = "TrayIcon Test";
this.trayIcon.Visible = true;
}

private void MenuClicks(object sender, EventArgs e)
{
if (sender == mnuExit) Application.Exit();
else if (sender == mnuAbout) MessageBox.Show("About TrayIcon test
program");

}

public static void Main(string[] args)
{
new TrayIcon();
}
}
}

Jul 21 '05 #4
On Thu, 7 Apr 2005 02:49:02 -0700, Lalit wrote:
I have tried this.
I am able to show the Systray icon by setting the allow service to interact
with Desktop property checked.

But the Context menu is not shown for that and i also tried the double click
event of notify icon. If i open the Exe of service directly then context menu
is shown.


I would really advise you to follow what i have told you in my other post.
Showing your tray icon directly from your Windows Service with Interact
with desktop is only going to give you problems. You may be able to get it
working fine when you start the service but as soon as you'll log off or if
2 users are logged in simlutaneously, you'll run into troubles.

Keep in mind that a windows service runs under its own user account (often
Local System) and runs independently of whether there is a (or several)
user logged in or not. If you display any UI element from your Windows
Service, you will have to detect users login and logout (and that's not
that trivial) in order to show/hide your UI elements properly. And under
Windows XP with fast user switching on, you'll probably have headaches
trying to display you tray icon on the desktop of the user that is
currently using the computer since there can be any number of interactive
users logged in simultaneously although only one at a time can effectively
use the computer. And be prepared for Windows 2003 where 2 interactive
users can be logged in and use the computer simultaneously (one at the
console, the other one via Remote Desktop).

Using the approach described in my previous post - that is, having a normal
window form application in the startup folder displaying the tray icon and
communicating with your service via for instance .NET Remoting - allows you
to solve all these problems at once and is fairly easy to implement. This
is how all the Windows Services that need a UI (e.g. antivirus sofwtare)
are implemented.
Jul 21 '05 #5
Hi,

I have no idea of remoting. Another thing i need only one application. If
i use another windows application then i will have two applications to manage.

Regards,
Lalit

"Mehdi" wrote:
On Thu, 7 Apr 2005 02:49:02 -0700, Lalit wrote:
I have tried this.
I am able to show the Systray icon by setting the allow service to interact
with Desktop property checked.

But the Context menu is not shown for that and i also tried the double click
event of notify icon. If i open the Exe of service directly then context menu
is shown.


I would really advise you to follow what i have told you in my other post.
Showing your tray icon directly from your Windows Service with Interact
with desktop is only going to give you problems. You may be able to get it
working fine when you start the service but as soon as you'll log off or if
2 users are logged in simlutaneously, you'll run into troubles.

Keep in mind that a windows service runs under its own user account (often
Local System) and runs independently of whether there is a (or several)
user logged in or not. If you display any UI element from your Windows
Service, you will have to detect users login and logout (and that's not
that trivial) in order to show/hide your UI elements properly. And under
Windows XP with fast user switching on, you'll probably have headaches
trying to display you tray icon on the desktop of the user that is
currently using the computer since there can be any number of interactive
users logged in simultaneously although only one at a time can effectively
use the computer. And be prepared for Windows 2003 where 2 interactive
users can be logged in and use the computer simultaneously (one at the
console, the other one via Remote Desktop).

Using the approach described in my previous post - that is, having a normal
window form application in the startup folder displaying the tray icon and
communicating with your service via for instance .NET Remoting - allows you
to solve all these problems at once and is fairly easy to implement. This
is how all the Windows Services that need a UI (e.g. antivirus sofwtare)
are implemented.

Jul 21 '05 #6
On Thu, 7 Apr 2005 04:25:03 -0700, Lalit wrote:
Hi,

I have no idea of remoting.
It's never too late to learn :-)

So what do you need to tray icon for? Do you just need to display the state
of your Windows Server (started, paused, stopped) and allow the user to
start and stop it? In this case, you don't even need .NET Remoting or any
other cross process communication framework. You can simply use the
ServiceController class which is very easy to use.

If you need the user to pass commands to the service, then you need some
way to communicate with the service. You could use raw sockets or named
pipes if you are more familiar with them. If you have never done this sort
of things before then .NET Remoting is you best bet i guess since it's very
easy to learn as long as you are not trying to make overly complicated
things. Have a look on the web, codeproject.com in particular, for some
beginner's tutorials.
Another thing i need only one application. If
i use another windows application then i will have two applications to manage.


That's right, you'll have 2 applications. However, what do you mean by
"managing". Are you thinking of maintenance? In this case, i think that
having 2 applications would be far easier to maintain than having just a
Windows Service with ugly hacks to show a tray icon with context menu in a
reliable way. Having 2 application being the standard way to go to solve
this kind of problem, somebody having to maintain your code will have no
problem understanding what you are doing. If you go the hacking route, then
i hope that you are keeping a very detailled documentation of your code or
it's gonna be a pure horror to maintain.

All that said, if your application is a small app just for yourself, then
you could get away with just a Windows Service showing its tray icon since
you'll know its limitations. I can't help you here since i've never been
able to solve this problem myself (i've never look really hard for a
solution though) but there are surely ways to do whatever you want to do.
If you are developing a "serious" application, then you already know by now
what i think you should do.
Jul 21 '05 #7
Hi,
you are right but i have the problem that the design can not be changed and
i have to provide the solution as i have described. I need to send a command
to the service to process something.

With regards,
Lalit

"Mehdi" wrote:
On Thu, 7 Apr 2005 04:25:03 -0700, Lalit wrote:
Hi,

I have no idea of remoting.


It's never too late to learn :-)

So what do you need to tray icon for? Do you just need to display the state
of your Windows Server (started, paused, stopped) and allow the user to
start and stop it? In this case, you don't even need .NET Remoting or any
other cross process communication framework. You can simply use the
ServiceController class which is very easy to use.

If you need the user to pass commands to the service, then you need some
way to communicate with the service. You could use raw sockets or named
pipes if you are more familiar with them. If you have never done this sort
of things before then .NET Remoting is you best bet i guess since it's very
easy to learn as long as you are not trying to make overly complicated
things. Have a look on the web, codeproject.com in particular, for some
beginner's tutorials.
Another thing i need only one application. If
i use another windows application then i will have two applications to manage.


That's right, you'll have 2 applications. However, what do you mean by
"managing". Are you thinking of maintenance? In this case, i think that
having 2 applications would be far easier to maintain than having just a
Windows Service with ugly hacks to show a tray icon with context menu in a
reliable way. Having 2 application being the standard way to go to solve
this kind of problem, somebody having to maintain your code will have no
problem understanding what you are doing. If you go the hacking route, then
i hope that you are keeping a very detailled documentation of your code or
it's gonna be a pure horror to maintain.

All that said, if your application is a small app just for yourself, then
you could get away with just a Windows Service showing its tray icon since
you'll know its limitations. I can't help you here since i've never been
able to solve this problem myself (i've never look really hard for a
solution though) but there are surely ways to do whatever you want to do.
If you are developing a "serious" application, then you already know by now
what i think you should do.

Jul 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Juan Manuel Alegrķa B. | last post: by
reply views Thread by Alireza Haghshenass | last post: by
5 posts views Thread by vinoth | last post: by
2 posts views Thread by Stephajn Craig | last post: by
8 posts views Thread by =?Utf-8?B?TWlrZVo=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.