2010-01-19 68 views
16

我有一個用作單選按鈕的ToolStripButton。當它被選中時,一個藍色的輪廓包圍着按鈕,但沒有背景色。用戶不太清楚該按鈕是否已被選中,所以我想更改背景顏色以使檢查狀態更加明顯。如何在選中時更改System.Windows.Forms.ToolStripButton突出顯示/背景顏色?

當Checked屬性設置爲true時,如何更改高亮顏色?

下面的代碼片段:

this.hideInactiveVehiclesToolstripButton.CheckOnClick = true; 
     this.hideInactiveVehiclesToolstripButton.ForeColor = System.Drawing.Color.Blue; 
     this.hideInactiveVehiclesToolstripButton.AutoSize = false; 
     this.hideInactiveVehiclesToolstripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; 
     this.hideInactiveVehiclesToolstripButton.Image = global::ClientUI.Properties.Resources.toggleInactive; 
     this.hideInactiveVehiclesToolstripButton.ImageTransparentColor = System.Drawing.Color.Black; 
     this.hideInactiveVehiclesToolstripButton.Name = "hideInactiveVehiclesToolstripButton"; 
     this.hideInactiveVehiclesToolstripButton.Size = new System.Drawing.Size(48, 48); 
     this.hideInactiveVehiclesToolstripButton.Text = "Hide Inactive Vehicles"; 
     this.hideInactiveVehiclesToolstripButton.Click +=new System.EventHandler(this.hideInactiveVehiclesToolstripButton_Click); 

回答

36

您可以提供自己的工具條渲染繪製按鈕的背景,你想要的方式。此示例代碼爲選中的按鈕提供了非常明顯的黑色背景:

public partial class Form1 : Form { 
    public Form1() { 
     InitializeComponent(); 
     toolStrip1.Renderer = new MyRenderer(); 
    } 
    private class MyRenderer : ToolStripProfessionalRenderer { 
     protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e) { 
      var btn = e.Item as ToolStripButton; 
      if (btn != null && btn.CheckOnClick && btn.Checked) { 
       Rectangle bounds = new Rectangle(Point.Empty, e.Item.Size); 
       e.Graphics.FillRectangle(Brushes.Black, bounds); 
      } 
      else base.OnRenderButtonBackground(e); 
     } 
    } 
} 
+0

非常感謝! – mwalsher 2010-01-19 23:05:08