C# 选中时如何更改System.Windows.Forms.ToolStripButton高亮显示/背景色?

C# 选中时如何更改System.Windows.Forms.ToolStripButton高亮显示/背景色?,c#,.net,c#-2.0,toolstripbutton,C#,.net,C# 2.0,Toolstripbutton,我有一个ToolStripButton,用作单选按钮。选中时,按钮周围会有一个蓝色轮廓,但没有背景色。对于用户来说,按钮是否被选中还不够清楚,因此我想更改背景颜色,以使选中状态更加可见 当Checked属性设置为true时,如何更改高光颜色 以下是一段代码片段: this.hideInactiveVehiclesToolstripButton.CheckOnClick = true; this.hideInactiveVehiclesToolstripButton.ForeColor = Sys

我有一个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);

您可以提供自己的工具条渲染器,以所需的方式绘制按钮的背景。此示例代码为选中按钮提供了非常可见的黑色背景:

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

在事件上单击每个toolStripButton

private void toolStripButton4_Click(object sender, EventArgs e)
        {
            toolStrip1.Items[0].BackColor = SystemColors.ActiveCaption;
            toolStrip1.Items[1].BackColor = SystemColors.Control;
            toolStrip1.Items[2].BackColor = SystemColors.Control;
            toolStrip1.Items[3].BackColor = SystemColors.Control;

        }

下面是VB.net代码

Public Class Form1

   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
      toolStrip1.Renderer = New MyRenderer()
   End Sub

   Public Class MyRenderer
      Inherits ToolStripProfessionalRenderer

      Protected Overrides Sub OnRenderButtonBackground(ByVal e As ToolStripItemRenderEventArgs)
          Dim btn As ToolStripButton = e.Item
          If (Not IsDBNull(btn) And btn.CheckOnClick And btn.Checked) Then
              Dim bounds As Rectangle = New Rectangle(Point.Empty, e.Item.Size)
              e.Graphics.FillRectangle(Brushes.Black, bounds)
          End If
      End Sub
End Class

如果不喜欢此示例中绘制的矩形的大小,如果不希望覆盖背景边框,则可能需要使用按钮的ContentRectangle属性。