My program got a tray icon (notifyicon) for which I have added contextmenu which in its order has toolstripmenuitem(s) that expands after right-click on notifyicon.
I want to do the custom-look of toolstripmenuitems through renderer.
I've searched through multiple forum how to do that and to what I came up is that I need to link somehow toolstripmenu renderer to my contextmenu.
Toolstrip renderer (works perfectly for toolstripmenu):
namespace TrayRender
{
public class TrayMenuRenderer : ToolStripRenderer
{
public TrayMenuRenderer()
: base()
{
}
protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
e.TextColor = Color.White;
base.OnRenderItemText(e);
}
protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
base.OnRenderImageMargin(e);
Graphics g = e.Graphics;
...
}
protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
{
base.OnRenderButtonBackground(e);
Graphics g = e.Graphics;
...
}
protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
{
base.OnRenderToolStripBackground(e);
Graphics g = e.Graphics;
...
}
}
public class TrayMenuClass : MenuStrip
{
public TrayMenuClass()
{
this.Renderer = new TrayMenuRenderer();
}
}
}
Then in the form designer.cs:
private void InitializeComponent()
{
...
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenuStrip1 = new TrayRender.TrayMenuClass();
this.Close = new System.Windows.Forms.ToolStripMenuItem();
this.contextMenuStrip1.SuspendLayout();
...
this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
resources.ApplyResources(this.notifyIcon1, "notifyIcon1");
this.notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
this.contextMenuStrip1.BackColor = System.Drawing.Color.Black;
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.Close});
this.contextMenuStrip1.Name = "contextMenuStrip1";
resources.ApplyResources(this.contextMenuStrip1, "contextMenuStrip1");
this.Close.Name = "Close";
resources.ApplyResources(this.Close, "Close");
this.Close.Click += new System.EventHandler(this.Close_Click);
...
#endregion
...
private System.Windows.Forms.NotifyIcon notifyIcon1;
private TrayRender.TrayMenuClass contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem Close;
VS2008 says that:
this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
Cannot implicitly convert type 'TrayRender.TrayMenuClass' to 'System.Windows.Forms.ContextMenuStrip'
I have also tried to do that in this way:
private void InitializeComponent()
{
...
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip();
this.Close = new TrayRender.TrayMenuClass();
this.contextMenuStrip1.SuspendLayout();
...
this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
resources.ApplyResources(this.notifyIcon1, "notifyIcon1");
this.notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
this.contextMenuStrip1.BackColor = System.Drawing.Color.Black;
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.Close});
this.contextMenuStrip1.Name = "contextMenuStrip1";
resources.ApplyResources(this.contextMenuStrip1, "contextMenuStrip1");
this.Close.Name = "Close";
resources.ApplyResources(this.Close, "Close");
this.Close.Click += new System.EventHandler(this.Close_Click);
...
#endregion
...
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private TrayRender.TrayMenuClass Close;
In this case it gives this error:
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.Close});
Cannot implicitly convert type 'TrayRender.TrayMenuClass' to 'System.Windows.Forms.ToolStripItem'
Thx for any thoughts on this one in advance.