Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#

Create a system tray icon and a dialog for a Windows Service

Rate me:
Please Sign up or sign in to vote.
4.84/5 (20 votes)
25 Jan 2008CPOL2 min read 365K   7.3K   120  
This article shows how to create a system tray icon and a dialog for a Windows Service.
using System;
using System.Collections.Generic;
using System.Text;

using COMAdmin;
using SensEvents;

namespace SystemTrayIconInSvc
{
    /// <summary>
    /// ISensLogon2 Event Args
    /// </summary>
    public class SensLogon2EventArgs : EventArgs
    {
        public string Username;
        public uint SessionId;
    }

    /// <summary>
    /// subscribe SENS notification
    /// Ref MSDN:Accessing System Power and Network Status Using SENS
    /// ms-help://MS.MSDN.vAug06.en/dntablet/html/tbconFFFSENS.htm
    /// </summary>
    public sealed class SensAdvisor : ISensLogon2
    {
        public const string ISensLogon2_ID = "{d5978650-5b9f-11d1-8dd2-00aa004abd5e}";

        public SensAdvisor()
        {
            COMAdminCatalogClass comAdmin = new COMAdminCatalogClass();
            ICatalogCollection subCollection = (ICatalogCollection)comAdmin.GetCollection("TransientSubscriptions");

            SubscribeToEvent(subCollection, "PostShell", ISensLogon2_ID);
            SubscribeToEvent(subCollection, "Logon", ISensLogon2_ID);
            SubscribeToEvent(subCollection, "Logoff", ISensLogon2_ID);
            SubscribeToEvent(subCollection, "SessionReconnect", ISensLogon2_ID);
            SubscribeToEvent(subCollection, "SessionDisconnect", ISensLogon2_ID);
        }
            
        private void SubscribeToEvent(ICatalogCollection subCollection, string methodName, string guidString)
        {
           ICatalogObject catalogObject = (ICatalogObject)subCollection.Add();

           // Specify the parameters of the desired subscription.
           catalogObject.set_Value("EventCLSID", guidString);
           catalogObject.set_Value("Name", "Subscription to " + methodName + " event");
           catalogObject.set_Value("MethodName", methodName);
           catalogObject.set_Value("SubscriberInterface", this);
           catalogObject.set_Value("Enabled", true);
           // This setting allows subscriptions to work for non-Administrator users.
           catalogObject.set_Value("PerUser", true);  

           // Save the changes made to the transient subscription collection.
           subCollection.SaveChanges();
        }


        public delegate void PostShellEventHandler(object sender, SensLogon2EventArgs e);
        public delegate void SessionReconnectEventHandler(object sender, SensLogon2EventArgs e);
        public delegate void SessionDisconnectEventHandler(object sender, SensLogon2EventArgs e);
        public delegate void LogonEventHandler(object sender, SensLogon2EventArgs e);
        public delegate void LogoffEventHandler(object sender, SensLogon2EventArgs e);

        public event PostShellEventHandler OnShellStarted;
        public event SessionReconnectEventHandler OnSessionReconnected;
        public event SessionDisconnectEventHandler OnSessionDisconnected;
        public event LogonEventHandler OnLogon;
        public event LogoffEventHandler OnLogoff;


        public void PostShell(string bstrUserName, uint dwSessionId)
        {
            if (OnShellStarted != null)
            {
                SensLogon2EventArgs args = new SensLogon2EventArgs();
                args.Username = bstrUserName;
                args.SessionId = dwSessionId;
                OnShellStarted(this, args);
            }
        }


        public void SessionReconnect(string bstrUserName, uint dwSessionId)
        {
            if (OnSessionReconnected != null)
            {
                SensLogon2EventArgs args = new SensLogon2EventArgs();
                args.Username = bstrUserName;
                args.SessionId = dwSessionId;
                OnSessionReconnected(this, args);
            }
        }

        public void SessionDisconnect(string bstrUserName, uint dwSessionId)
        {
            if (OnSessionDisconnected != null)
            {
                SensLogon2EventArgs args = new SensLogon2EventArgs();
                args.Username = bstrUserName;
                args.SessionId = dwSessionId;
                OnSessionDisconnected(this, args);
            }
        }

        public void Logoff(string bstrUserName, uint dwSessionId)
        {
            if (OnLogoff != null)
            {
                SensLogon2EventArgs args = new SensLogon2EventArgs();
                args.Username = bstrUserName;
                args.SessionId = dwSessionId;
                OnLogoff(this, args);
            }
        }

        public void Logon(string bstrUserName, uint dwSessionId)
        {
            if (OnLogon != null)
            {
                SensLogon2EventArgs args = new SensLogon2EventArgs();
                args.Username = bstrUserName;
                args.SessionId = dwSessionId;
                OnLogon(this, args);
            }
        }

        
        
    }


    

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
China China
Jerry is from China. He was captivated by computer programming since 13 years old when first time played with Q-Basic.



  • Windows / Linux & C++
  • iOS & Obj-C
  • .Net & C#
  • Flex/Flash & ActionScript
  • HTML / CSS / Javascript
  • Gaming Server programming / video, audio processing / image & graphics


Contact: vcer(at)qq.com
Chinese Blog: http://blog.csdn.net/wangjia184

Comments and Discussions