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:
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.