Click here to Skip to main content
15,846,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
created an application, where i'm creating a system tray icon. If i right click on the system tray, tehn i'm opening a menu. If i click on dektop , then i want to close the menu. Please let me know how to do it? below is my code.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Drawing;
using System.Resources;

namespace Systrayicon
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class RegistrationWindow : Window
    {
        private System.Windows.Forms.NotifyIcon _notifyIcon = null;
        private System.Windows.Controls.ContextMenu _mContextMenu;

        
        public RegistrationWindow()
        {
            InitializeComponent();

            _mContextMenu = new System.Windows.Controls.ContextMenu();

            if (null == _notifyIcon)
            {
                _notifyIcon = new System.Windows.Forms.NotifyIcon
                {
                    Icon = new Icon(@"D:\WPFSystemTray\Images\RedOrb.ico"),
                    Visible = true
                };

                _notifyIcon.MouseUp += new System.Windows.Forms.MouseEventHandler(NotifyIconMouseUp);
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            this._notifyIcon.Dispose();
        }


        void NotifyIconMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            bool CreateTrayMenu = false;

          
                CreateTrayMenu = true;

            // Check if any modal dialog is visible or not
            //if (true == CreateTrayMenu)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    
                    MenuItem menuShow = new MenuItem();
                    menuShow.Header = "ShowAppClick";
                   // menuShow.Click += new RoutedEventHandler(ShowAppClick);


                    MenuItem menuCloseApplication = new MenuItem();
                    menuCloseApplication.Header = "CloseAppClick";
                    //menuCloseApplication.Click += new RoutedEventHandler(CloseAppClick);

                    _mContextMenu.Items.Add(menuShow);
                    _mContextMenu.Items.Add(menuCloseApplication);

                    _mContextMenu.IsOpen = true;

                    _mContextMenu.IsKeyboardFocusWithinChanged += new DependencyPropertyChangedEventHandler(_mContextMenu_IsEnabledChanged);

                    _mContextMenu.LostFocus += new RoutedEventHandler(_mContextMenu_LostFocus);
                }
            }
        }

        void _mContextMenu_LostFocus(object sender, RoutedEventArgs e)
        {
            //throw new NotImplementedException();
            _mContextMenu.IsOpen = false;
        }

        protected override void OnStateChanged(EventArgs e)
        {
            int j = 0;
            ++j;
        }
        void _mContextMenu_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            //throw new NotImplementedException();
            _mContextMenu.IsOpen = false;
        }
        //protected override void Dispose(bool disposing)
        //{
        //    if (disposing)
        //    {
        //        this._notifyIcon.Dispose();
        //    }
        //}
    }
}



Thank you all.
Posted
Comments
vidiking 3-Oct-12 6:06am    
Dear Experts,

Please help me. I am not able to fix this issue till now.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900