473,320 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 10227
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DH | last post by:
I hope this is a right place to post my question I'm working on a Windows service application with VB.net. The purpose is to let the service application as a monitor to periodically check a...
2
by: Juan Manuel Alegría B. | last post by:
Hi group, I've sent this question in other group.... How can I see my Windows service application on the task manager. Thanks so much. Guadalajara, Mexico.
2
by: Mustaq | last post by:
Hi I am unable to get Timer1_Tick event in Windows Service Application inspite of setting following things Timer1.enabled = True Timer1.interval = 1000 Let me know how to resolve Thanks &
0
by: Alireza Haghshenass | last post by:
Dear All, I am facing a problem which I could not solve. I am writing an application which uses a notify icon and a context menu bound to it to show modal dialog forms. When these forms is shown...
5
by: vinoth | last post by:
Hi, I have created WindowsService Project.In that Project OnStart Method i have written the following Code. In this code the Server is waiting for the connection from client. When the Client...
2
by: Stephajn Craig | last post by:
Is it possible to apply remoting techniques to a Windows Service Application? I have an application that I'm building that is primarily ASP.NET based. However, there are some functions that I...
1
by: Loane Sharp | last post by:
Hi there Is it possible to set the environment for a windows service application to anything other than "Environment.UserInteractive = False"? Does this (whether I can or can't set the user...
0
by: ld | last post by:
Hi, I have a Windows service that creates a tray icon. the tray icon gets created no problem however I cannot get any of the events on this icon to be fired in the Service. So if you double click...
8
by: =?Utf-8?B?TWlrZVo=?= | last post by:
How Can Windows Service Application Reatarts itself? Thanks.
0
by: DavidEngler | last post by:
I'm developing a windows service application in C#, that is logging to a website, and check for a certain file, and if it exist, download it and process it. http://watin.sourceforge.net - works...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.