23

我有一个用作单选按钮的 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);
4

3 回答 3

47

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

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);
        }
    }
}
于 2010-01-19T21:44:55.180 回答
0

on 事件点击每个 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;

        }
于 2019-01-08T09:00:03.830 回答
0

这是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
于 2020-03-29T15:29:47.967 回答