We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to show ContextMenu of NotifyIcon by rightclick?

Thread ID:

Created:

Updated:

Platform:

Replies:

144119 Apr 19,2019 11:05 AM UTC Apr 25,2019 01:34 PM UTC WPF 3
loading
Tags: NotifyIcon
guize
Asked On April 19, 2019 11:05 AM UTC

How can i show the ContextMenu when i rightclick the notifyicon?

Sabaridass Ramamoorthy [Syncfusion]
Replied On April 22, 2019 10:35 AM UTC

Hi Guize, 
 
Thanks for contacting Syncfusion Support. 
 
We have analyzed your reported requirement –“Open context menu when right clicking on notify icon” and it can be achieved by “MouseRightButtonDown” event handler. We have prepared a simple to achieve your requirement and it can be downloaded from below location. 
 
Refer to the below code example. 
 
private void ChromelessWindow_Loaded(object sender, RoutedEventArgs e) 
        {             
            defaults.ShowInTaskBar = true; 
 
            System.Windows.Media.Imaging.BitmapImage bim = new System.Windows.Media.Imaging.BitmapImage(); 
            bim.BeginInit(); 
            bim.DecodePixelWidth = 16; 
            bim.UriSource = new Uri("pack://application:,,,/NotifyIcon_2008;Component/Icon.ico"); 
            bim.EndInit(); 
            defaults.Icon = bim; 
            defaults.MouseRightButtonDown += Defaults_MouseRightButtonDown; 
        } 
 
        private void Defaults_MouseRightButtonDown(object sender, MouseButtonEventArgs e) 
        { 
            ContextMenu contextMenu = new ContextMenu(); 
            contextMenu.Items.Add(new MenuItem() { Header = "Balloon" }); 
            contextMenu.Items.Add(new MenuItem() { Header = "Rectangle" }); 
            contextMenu.Items.Add(new MenuItem() { Header = "RoundedRectangle" }); 
            contextMenu.PlacementTarget = sender as NotifyIcon; 
            contextMenu.Items.Cast<MenuItem>().ToList().ForEach(x => x.Click += X_Click); 
            contextMenu.IsOpen = true; 
        } 
 
        private void X_Click(object sender, RoutedEventArgs e) 
        { 
            var menuItem = e.OriginalSource as MenuItem; 
            if(menuItem != null) 
            { 
                switch (menuItem.Header.ToString()) 
                { 
                    case "Balloon": 
                        defaults.BalloonTipShape = BalloonTipShapes.Balloon; 
                        break; 
                    case "Rectangle": 
                        defaults.BalloonTipShape = BalloonTipShapes.Rectangle; 
                        break; 
                    case "RoundedRectangle": 
                        defaults.BalloonTipShape = BalloonTipShapes.RoundedRectangle; 
                        break; 
                } 
            } 
        } 
 
Regards, 
Sabaridass R 


guize
Replied On April 25, 2019 02:47 AM UTC

Thanks for your reply, this method works good.But another question is How to make the menu be Hidden when i click my mouse leftbutton in other place.

Subburaj Pandian Veluchamy [Syncfusion]
Replied On April 25, 2019 01:34 PM UTC

Hi Guize 
  
Thank you for the update. 
  
We have checked your query and we suspect that your requirement is to hide the menu on the MouseLeftButton. But when we have observed the sample, the ContextMenu has been hidden on left clicking the window and notify icon at our end. 
  
If you are mentioning this, we could not suspect how it may not working at your end. However, you can handle the opening of ContextMenu by comparing the mousebutton in MouseLeftButtonDown event of notify icon or the window. 
  
Please find the modified code below,  
[C#]  
private void Defaults_MouseLeftButtonDown(object sender, MouseButtonEventArgse) 
{ 
if(e.ChangedButton == MouseButton.Left) 
contextMenu.IsOpen = false; 
} 
  
  
Please try this solution and let us know if it is helpful. Please let us know your requirement with clear details, if we have misunderstood it. 
  
Regards,
Subburaj Pandian V
 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon