473,574 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10297
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.ContextMe nu trayMenu;
private System.Windows. Forms.MenuItem mnuAbout;
private System.Windows. Forms.MenuItem mnuExit;
private System.Windows. Forms.NotifyIco n trayIcon;

public TrayIcon()
{
InitializeCompo nent();

Application.Run ();
}

private void InitializeCompo nent()
{
System.Resource s.ResourceManag er resources = new
System.Resource s.ResourceManag er(typeof(TrayI con));
this.mnuAbout = new System.Windows. Forms.MenuItem( );
this.mnuExit = new System.Windows. Forms.MenuItem( );
this.trayMenu = new System.Windows. Forms.ContextMe nu();
this.trayIcon = new System.Windows. Forms.NotifyIco n();

//
// mnuAbout
//
this.mnuAbout.I ndex = 8;
this.mnuAbout.T ext = "About";
this.mnuAbout.C lick += new System.EventHan dler(this.MenuC licks);
//
// mnuExit
//
this.mnuExit.In dex = 10;
this.mnuExit.Te xt = "Exit";
this.mnuExit.Cl ick += new System.EventHan dler(this.MenuC licks);

//
// trayMenu
//
this.trayMenu.M enuItems.AddRan ge(new System.Windows. Forms.MenuItem[]
{
this.mnuAbout,
this.mnuExit});
//
// trayIcon
//
this.trayIcon.C ontextMenu = this.trayMenu;
this.trayIcon.I con =
(System.Drawing .Icon)(resource s.GetObject("tr ayIcon.Icon"));
this.trayIcon.T ext = "TrayIcon Test";
this.trayIcon.V isible = true;
}

private void MenuClicks(obje ct sender, EventArgs e)
{
if (sender == mnuExit) Application.Exi t();
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.ContextMe nu trayMenu;
private System.Windows. Forms.MenuItem mnuAbout;
private System.Windows. Forms.MenuItem mnuExit;
private System.Windows. Forms.NotifyIco n trayIcon;

public TrayIcon()
{
InitializeCompo nent();

Application.Run ();
}

private void InitializeCompo nent()
{
System.Resource s.ResourceManag er resources = new
System.Resource s.ResourceManag er(typeof(TrayI con));
this.mnuAbout = new System.Windows. Forms.MenuItem( );
this.mnuExit = new System.Windows. Forms.MenuItem( );
this.trayMenu = new System.Windows. Forms.ContextMe nu();
this.trayIcon = new System.Windows. Forms.NotifyIco n();

//
// mnuAbout
//
this.mnuAbout.I ndex = 8;
this.mnuAbout.T ext = "About";
this.mnuAbout.C lick += new System.EventHan dler(this.MenuC licks);
//
// mnuExit
//
this.mnuExit.In dex = 10;
this.mnuExit.Te xt = "Exit";
this.mnuExit.Cl ick += new System.EventHan dler(this.MenuC licks);

//
// trayMenu
//
this.trayMenu.M enuItems.AddRan ge(new System.Windows. Forms.MenuItem[]
{
this.mnuAbout,
this.mnuExit});
//
// trayIcon
//
this.trayIcon.C ontextMenu = this.trayMenu;
this.trayIcon.I con =
(System.Drawing .Icon)(resource s.GetObject("tr ayIcon.Icon"));
this.trayIcon.T ext = "TrayIcon Test";
this.trayIcon.V isible = true;
}

private void MenuClicks(obje ct sender, EventArgs e)
{
if (sender == mnuExit) Application.Exi t();
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
ServiceControll er 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
ServiceControll er 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
3008
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 launching flag file. If the flag was found the service will trigger to run a finance calculating program (VMPE4SRVAPP.exe, a VB 6.0/Access application but...
2
1349
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
5425
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
1547
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 there is no problem but while I am closing any of them which is shown in modal form throw an exception which is described below : Message :...
5
4260
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 connects to the Server, the server will process and send result to the client.(This is Client Server Application. The Server side Code is implementd...
2
1370
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 want to have run all the time in the background. So I am implementing a Windows service to accomplish this. However, I'd like to expose some...
1
5741
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 interactive mode to true) depend on whether the service is running on the local system account or a user account? For instance, when the service is...
0
1390
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 on the icon nothing happens. Also I am interested in having a context menu associated to this icon. Is this impossible to do because I am dealing...
8
9795
by: =?Utf-8?B?TWlrZVo=?= | last post by:
How Can Windows Service Application Reatarts itself? Thanks.
0
851
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 great as a Windows application but doesnt run as windows service. Anyone have an idea how can I make this program work? Thank you
0
7738
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8077
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7828
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3755
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3765
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1076
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.