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

时间:2010-01-19 21:18:56

标签: c# .net c#-2.0 toolstripbutton

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

3 个答案:

答案 0 :(得分:44)

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

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

答案 1 :(得分:0)

在事件上单击每个工具的StripButton

sess.Exec(stmt, valueArgs...)

答案 2 :(得分: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
相关问题