在下拉菜单中显示ToolStripControlHost的图像

时间:2017-04-01 13:22:32

标签: c# .net winforms toolstrip toolstripcontrolhost

我有一个ToolStripSplitButton,下拉列表中包含各种元素。 其中一个是封装在ToolStripControlHost中的Trackbar,名为ToolStripTrackbarItem。这是代码(我从stackoverflow得到它):

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Application
{
    [System.ComponentModel.DesignerCategory("code")]
    [System.Windows.Forms.Design.ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ContextMenuStrip | ToolStripItemDesignerAvailability.MenuStrip)]

    public class ToolStripTrackbarItem : ToolStripControlHost
    {
        public ToolStripTrackbarItem()
            : base(CreateControlInstance())
        {
            this.Size = Control.Size;
        }
        public TrackBar TrackBar
        {
            get { return Control as TrackBar; }
        }
        private static Control CreateControlInstance()
        {
            TrackBar t = new TrackBar();
            t.AutoSize = false;
            return t;
        }
        [DefaultValue(0)]
        public int Value
        {
            get { return TrackBar.Value; }
            set { TrackBar.Value = value; }
        }
        protected override void OnSubscribeControlEvents(Control control)
        {
            base.OnSubscribeControlEvents(control);
            TrackBar trackBar = control as TrackBar;
            trackBar.ValueChanged += new EventHandler(trackBar_ValueChanged);
        }
        protected override void OnUnsubscribeControlEvents(Control control)
        {
            base.OnUnsubscribeControlEvents(control);
            TrackBar trackBar = control as TrackBar;
            trackBar.ValueChanged -= new EventHandler(trackBar_ValueChanged);
        }
        void trackBar_ValueChanged(object sender, EventArgs e)
        {
            if (this.ValueChanged != null)
                ValueChanged(sender, e);
        }
        public event EventHandler ValueChanged;
        protected override Size DefaultSize
        {
            get { return new Size(300, 16); }
        }
    }

它有效,但我需要在下拉列表的左侧显示图像:

menu

通过设置Image属性,我成功使用了一个简单的ToolStripMenuItem。但是,设置ToolStripTrackbarItem的Image属性是无效的(继承自ToolStripControlHost,请参阅上面的代码)。根据MSDN,Image属性与ToolStripControlHost无关。

这是什么意思?是否甚至不能将图像包含在ToolStripControlHost中?

如果有可能,怎么做?

1 个答案:

答案 0 :(得分:1)

你应该在这里解决2个问题:

  • ToolStripControlHost未显示Image属性,也不会在保存表单时序列化图片。
  • ToolStripProfessionalRendered不会为ToolStripControlHost绘制图片。

您需要覆盖Image的{​​{1}}属性,并使其可浏览和序列化。您还需要创建自定义渲染器,以正确的位置和大小绘制图像。然后,如果您只是使用下面的代码设置ToolStripControlHost的渲染器,您将得到预期的结果:

ToolStrip

enter image description here

ToolStripTrackBar

该项使this.toolStrip1.Renderer = new MyCustomRenderer(); 属性显示在属性网格中,并在保存表单时进行序列化。

Image

MyCustomRenderer

此渲染器为using System.ComponentModel; using System.Drawing; using System.Linq; using System.Windows.Forms; using System.Windows.Forms.Design; [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)] public class ToolStripTrackBar : ToolStripControlHost { public TrackBar TrackBar { get { return (TrackBar)Control; } } public ToolStripTrackBar() : base(CreateControl()) { } private static TrackBar CreateControl() { var t = new TrackBar() { TickStyle = TickStyle.None, AutoSize = false, Height = 28 }; return t; } [Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public override Image Image { get { return base.Image; } set { base.Image = value; } } /*Expose properties and events which you need.*/ public int Value { get { return TrackBar.Value; } set { TrackBar.Value = value; } } } 绘制图像。

ToolStripTrackBar