Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 25 Jan 2008 CPOL
This article shows how to create a system tray icon and a dialog for a Windows Service.
bin.zip
Interop.SensEvents.dll
SystemTrayIconInSvc.exe
SystemTrayIconInSvc.vshost.exe
Interop.COMAdmin.dll
binary.zip
Interop.SensEvents.dll
SystemTrayIconInSvc.exe
SystemTrayIconInSvc.vshost.exe
Interop.COMAdmin.dll
source.zip
SystemTrayIconInSvc.csproj.user
SystemTrayIconInSvc.suo
obj
Debug
Interop.COMAdmin.dll
Interop.SensEvents.dll
Refactor
SystemTrayIconInSvc.csproj.GenerateResource.Cache
SystemTrayIconInSvc.csproj.ResolveComReference.cache
SystemTrayIconInSvc.exe
SystemTrayIconInSvc.pdb
SystemTrayIconInSvc.SettingDlg.resources
TempPE
Release
Interop.COMAdmin.dll
Interop.SensEvents.dll
SystemTrayIconInSvc.csproj.GenerateResource.Cache
SystemTrayIconInSvc.csproj.ResolveComReference.cache
SystemTrayIconInSvc.exe
SystemTrayIconInSvc.pdb
SystemTrayIconInSvc.SettingDlg.resources
TempPE
Properties
SourceAndBinary.zip
SystemTrayIconInSvc.csproj.user
bin
Release
Interop.COMAdmin.dll
Interop.SensEvents.dll
SystemTrayIconInSvc.exe
SystemTrayIconInSvc.vshost.exe
sourcecode.zip
SystemTrayIconInSvc.csproj.user
SystemTrayIconInSvc.suo
Interop.COMAdmin.dll
Interop.SensEvents.dll
SystemTrayIconInSvc.csproj.GenerateResource.Cache
SystemTrayIconInSvc.csproj.ResolveComReference.cache
SystemTrayIconInSvc.exe
SystemTrayIconInSvc.pdb
SystemTrayIconInSvc.SettingDlg.resources
Interop.COMAdmin.dll
Interop.SensEvents.dll
SystemTrayIconInSvc.csproj.GenerateResource.Cache
SystemTrayIconInSvc.csproj.ResolveComReference.cache
SystemTrayIconInSvc.exe
SystemTrayIconInSvc.pdb
SystemTrayIconInSvc.SettingDlg.resources
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace SystemTrayIconInSvc
{
    public partial class SettingDlg : Form
    {
        private Desktop m_Desktop = new Desktop();
        private IContainer m_Container = null;
        private NotifyIcon m_NotifyIcon = null;
        private Button btnHide;
        private ContextMenu m_ContextMenu = null;

        /// <summary>
        /// Start the UI thread
        /// </summary>
        public static SettingDlg StartUIThread()
        {
            SettingDlg dlg = new SettingDlg();

            Thread thread = new Thread(new ThreadStart(dlg.UIThread));
            thread.Start();

            return dlg;
        }

        /// <summary>
        /// UI thread
        /// </summary>
        public void UIThread()
        {
            if( !m_Desktop.BeginInteraction() )
                return;
            Application.Run(this);
        }

        protected SettingDlg()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Move the window to the right-bottom corner
        /// </summary>
        /// <param name="e"></param>
        protected override void OnShown(EventArgs e)
        {
            this.Left = Screen.PrimaryScreen.WorkingArea.Left
                + Screen.PrimaryScreen.WorkingArea.Width
                - this.Width
                ;
            this.Top = Screen.PrimaryScreen.WorkingArea.Top
                + Screen.PrimaryScreen.WorkingArea.Height
                - this.Height
                ;
        }


        private void SettingDlg_Load(object sender, EventArgs e)
        {
            m_ContextMenu = new ContextMenu();
            m_ContextMenu.MenuItems.Add(new MenuItem("Open Dialog", this.OpenDialog));
            
            Icon icon = new Icon(SystemIcons.Application, 16, 16);
            m_Container = new Container();
            m_NotifyIcon = new NotifyIcon(m_Container);
            m_NotifyIcon.ContextMenu = m_ContextMenu;
            m_NotifyIcon.Icon = icon;
            m_NotifyIcon.Visible = true;

            m_NotifyIcon.ShowBalloonTip( 200
                , "SystemTrayIconInSvc"
                , "The system tray icon is implemented in the windows service itself."
                , ToolTipIcon.Info
                );
        }



        public void OpenDialog(Object sender, EventArgs e)
        {
            this.Visible = true;
            BringToFront();
        }

        protected override void OnClosed(EventArgs e)
        {
            m_NotifyIcon.Dispose();
            m_ContextMenu.Dispose();
            m_Container.Dispose();
        }

        private void btnHide_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

   

    }
}

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)

Share

About the Author

Jerry.Wang
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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 25 Jan 2008
Article Copyright 2008 by Jerry.Wang
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid