How to change System.Windows.Forms.ToolStripButton highlight/background color when checked?

前端 未结 3 893
执念已碎
执念已碎 2020-12-09 16:56

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 fo

3条回答
  •  温柔的废话
    2020-12-09 17:08

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

提交回复
热议问题