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

回眸只為那壹抹淺笑 提交于 2019-11-28 09:42:55

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

on Event click for every 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;

        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!