如何在选中时更改System.Windows.Forms.ToolStripButton高亮/背景颜色?

mwa*_*her 21 .net c# toolstripbutton c#-2.0

我有一个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);
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 45

您可以提供自己的工具条渲染器,以按照您希望的方式绘制按钮的背景.此示例代码为选中的按钮提供了非常明显的黑色背景:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)