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

2023-09-02 11:58:28 作者:闪闪惹乆爱︺

我有一个用作一个单选按钮ToolStripButton。当检查,一个蓝色的轮廓包围按钮,但没有背景颜色。这是不够明确为被选中的按钮的用户,所以我想改变背景颜色,使检查状态更加明显。

我如何去改变高亮颜色当Checked属性设置为true?

下面是一个code片断:

  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 =全​​球:: ClientUI.Properties.Resources.toggleInactive;
        this.hideInactiveVehiclesToolstripButton.ImageTransparentColor = System.Drawing.Color.Black;
        this.hideInactiveVehiclesToolstripButton.Name =hideInactiveVehiclesToolstripButton;
        this.hideInactiveVehiclesToolstripButton.Size =新System.Drawing.Size(48,48);
        this.hideInactiveVehiclesToolstripButton.Text =隐藏不活动的车辆;
        this.hideInactiveVehiclesToolstripButton.Click + =新System.EventHandler(this.hideInactiveVehiclesToolstripButton_Click);
 

解决方案 Using System.Windows.Forms.Label and System.Windows.Forms.FontDialog weixin 30535565的博客 CSDN博客

您可以提供自己的工具条渲染器来绘制按钮的背景,你想要的方式。这个例子code给出的检查按钮,一个非常明显的黑色背景:

 公共部分Form1类:表格{
    公共Form1中(){
        的InitializeComponent();
        toolStrip1.Renderer =新MyRenderer();
    }
    私有类MyRenderer:ToolStripProfessionalRenderer {
        保护覆盖无效OnRenderButtonBackground(ToolStripItemRenderEventArgs E){
            VAR BTN = e.Item为ToolStripButton;
            如果(BTN = NULL和放大器;!&安培; btn.CheckOnClick和放大器;&安培; btn.Checked){
                矩形范围=新的Rectangle(Point.Empty,e.Item.Size);
                e.Graphics.FillRectangle(Brushes.Black,界限);
            }
            其他base.OnRenderButtonBackground(E);
        }
    }
}
 

I have a ToolStripButton that is used as a radio button. When it is checked, a blue outline surrounds the button, but there is no background color. It is not clear enough for the user that the button is checked, so I would like to change the background color to make the check state more visible.

How do I go about changing the highlight color when the Checked property is set to true?

Here is a code snippet:

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

解决方案

You can provide your own tool strip renderer to draw the button's background the way you want them. This example code gives the checked button a very visible black background:

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