Dark/Light mode of system tray icon in WPF

Rajib Chandra Pal 1 Reputation point
2021-10-06T09:51:03.21+00:00

I am using notify icon in my WPF app. When I click this icon, always there is a white background of context menu but I want to change this for Dark mode. But how to apply Dark & Light mode in notify icon?

_notifyIcon = new Forms.NotifyIcon();
_notifyIcon.Icon = MySystray.Resources.Systray_icon;
_notifyIcon.Text = APP_NAME;
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2021-10-07T05:30:13.767+00:00

    For changing the background color of the icon context menu, you could refer to the following code to use ContextMenuStrip and set ContextMenuStrip.BackColor.
    If you want to use ContextMenu, you can also try to refer to the answer here.

    The code of xaml:

    <Grid>  
            <Button Click="Button_Click" Width="100" Height="80"> click</Button>  
    </Grid>  
    

    The code of xaml.cs:

    using System;  
    using System.Windows;  
    using System.Windows.Forms;  
    using MessageBox = System.Windows.MessageBox;  
    namespace DarkLightofTrayIcon  
    {  
      public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
        }  
        ContextMenuStrip menuStrip;  
        private void Button_Click(object sender, RoutedEventArgs e)  
        {  
          NotifyIcon notifyIcon = new NotifyIcon();  
          notifyIcon.Icon = new System.Drawing.Icon(@"icon.ico");  
          notifyIcon.Visible = true;  
          menuStrip = new ContextMenuStrip();  
          menuStrip.BackColor = System.Drawing.Color.Gray;  
          menuStrip.Opening+= new System.ComponentModel.CancelEventHandler(cms_Opening);  
          notifyIcon.ContextMenuStrip = menuStrip;  
        }  
        void cms_Opening(object sender, System.ComponentModel.CancelEventArgs e)  
        {  
          System.Windows.Forms.Control c = menuStrip.SourceControl as System.Windows.Forms.Control;  
          ToolStripDropDownItem tsi = menuStrip.OwnerItem as ToolStripDropDownItem;  
          menuStrip.Items.Clear();  
          if (c != null)  
          {  
            menuStrip.Items.Add("Source: " + c.GetType().ToString());  
          }  
          else if (tsi != null)  
          {  
            menuStrip.Items.Add("Source: " + tsi.GetType().ToString());  
          }  
       
          menuStrip.Items.Add("Apples",null, this.MenuTest1_Click);  
          menuStrip.Items.Add("Oranges", null, this.MenuTest2_Click);  
          menuStrip.Items.Add("Pears", null, this.MenuTest3_Click);  
          e.Cancel = false;  
        }  
      
        private void MenuTest3_Click(object sender, EventArgs e)  
        {  
          MessageBox.Show("Pears");  
        }  
      
        private void MenuTest2_Click(object sender, EventArgs e)  
        {  
          MessageBox.Show("Oranges");  
        }  
      
        private void MenuTest1_Click(object sender, EventArgs e)  
        {  
          MessageBox.Show("Apples");  
        }  
      
      }  
    }  
    

    The picture of result:
    138421-imi.jpg


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.