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. Image for the cookie policy date
close icon

How to show ContextMenu of NotifyIcon by rightclick?

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

3 Replies

SR Sabaridass Ramamoorthy Syncfusion Team 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 



GU guize 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.


SP Subburaj Pandian Veluchamy Syncfusion Team 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
 


Loader.
Live Chat Icon For mobile
Up arrow icon