2017-04-01 43 views
1

我在下拉列表中有一個帶有各種元素的ToolStripSplitButton。 其中之一是放在ToolStripControlHost中的Trackbar,名爲ToolStripTrackbarItem。它的代碼(我從計算器了它):在下拉菜單中顯示ToolStripControlHost的圖像

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

我成功的一個簡單的ToolStripMenuItem通過設置Image屬性。但是,設置我的ToolStripTrackbarItem的Image屬性(從ToolStripControlHost繼承,請參閱上面的代碼)是無效的。根據MSDN,Image屬性與ToolStripControlHost無關。

這是什麼意思? ToolStripControlHost包含的圖像是否甚至不可能?

如果可能無論如何,該怎麼做?

回答

1

你應該在這裏解決2個問題:

  • ToolStripControlHost不顯示Image財產以及當您保存表單不序列圖像。
  • ToolStripProfessionalRendered不繪製圖像ToolStripControlHost

您需要覆蓋ToolStripControlHostImage屬性,並使其可瀏覽和可序列化。此外,您需要創建一個自定義渲染器以正確的位置和大小繪製圖像。然後,如果你簡單地使用下面的代碼設定渲染爲ToolStrip,你會得到預期的結果:

this.toolStrip1.Renderer = new MyCustomRenderer(); 

enter image description here

ToolStripTrackBar

該項目使Image財產屬性網格顯示,讓它在保存表單時序列化。

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

MyCustomRenderer

該渲染器將繪製圖像的ToolStripTrackBar

using System.Drawing; 
using System.Linq; 
using System.Windows.Forms; 
public class MyCustomRenderer : ToolStripProfessionalRenderer 
{ 
    protected override void OnRenderImageMargin(ToolStripRenderEventArgs e) 
    { 
     base.OnRenderImageMargin(e); 
     e.ToolStrip.Items.OfType<ToolStripTrackBar>() 
     .ToList().ForEach(item => 
     { 
      if (item.Image != null) 
      { 
       var size = item.GetCurrentParent().ImageScalingSize; 
       var location = item.Bounds.Location; 
       location = new Point(5, location.Y + 1); 
       var imageRectangle = new Rectangle(location, size); 
       e.Graphics.DrawImage(item.Image, imageRectangle, 
        new Rectangle(Point.Empty, item.Image.Size), 
        GraphicsUnit.Pixel); 
      } 
     }); 
    } 
}