Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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):
C#
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:
C#
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();
...
            // 
            // notifyIcon1
            // 
            this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
            resources.ApplyResources(this.notifyIcon1, "notifyIcon1");
            this.notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
            // 
            // contextMenuStrip1
            // 
            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");
            // 
            // Close
            // 
            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:
C#
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:
C#
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();
...
            // 
            // notifyIcon1
            // 
            this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
            resources.ApplyResources(this.notifyIcon1, "notifyIcon1");
            this.notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
            // 
            // contextMenuStrip1
            // 
            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");
            // 
            // Close
            // 
            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:
C#
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.
Posted

1 solution

You are try to inherit TrayMenuClass from MenuStrip

try ContextMenuStrip or ToolStripDropDownMenu instead of MenuStrip


C#
public class TrayMenuClass : ContextMenuStrip 


and try to cast it and assign

C#
this.notifyIcon1.ContextMenuStrip = (ContextMenuStrip)this.contextMenuStrip1;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900