托盘上下文菜单工具条的渲染器 [英] Renderer for toolstrip of Tray Context menu

查看:72
本文介绍了托盘上下文菜单工具条的渲染器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序有一个任务栏图标(notifyicon),为此我添加了contextmenu(上下文菜单),该菜单按顺序具有工具tripmenuitem(s),这些菜单在右键单击notifyicon后展开.
我想通过渲染器对toolstripmenuitems进行自定义.
我在多个论坛上进行了搜索,然后发现需要将toolstripmenu渲染器以某种方式链接到我的上下文菜单.

Toolstrip渲染器(非常适合toolstripmenu):

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();
        }
    }
}


然后以designer.cs的形式出现:


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();
...
            // 
            // 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表示:



VS2008 says that:

this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;


无法将类型"TrayRender.TrayMenuClass"隐式转换为"System.Windows.Forms.ContextMenuStrip"

我也尝试过这种方式:


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();
...
            // 
            // 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:

this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.Close});


无法将类型"TrayRender.TrayMenuClass"隐式转换为"System.Windows.Forms.ToolStripItem"

谢谢您对此事的任何事先想法.


Cannot implicitly convert type ''TrayRender.TrayMenuClass'' to ''System.Windows.Forms.ToolStripItem''

Thx for any thoughts on this one in advance.

推荐答案

您正在尝试从MenuStrip
继承TrayMenuClass .
尝试使用ContextMenuStrip ToolStripDropDownMenu代替MenuStrip


You are try to inherit TrayMenuClass from MenuStrip

try ContextMenuStrip or ToolStripDropDownMenu instead of MenuStrip


public class TrayMenuClass : ContextMenuStrip 



并尝试投射并分配



and try to cast it and assign

this.notifyIcon1.ContextMenuStrip = (ContextMenuStrip)this.contextMenuStrip1;


这篇关于托盘上下文菜单工具条的渲染器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆