如何在选中时更改 System.Windows.Forms.ToolStripButton 突出显示/背景颜色?

发布于 2024-08-18 02:05:14 字数 1056 浏览 6 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月牙弯弯 2024-08-25 02:05:14

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

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

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);
        }
    }
}
家住魔仙堡 2024-08-25 02:05:14

在事件上单击每个工具条按钮

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;

        }

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;

        }
著墨染雨君画夕 2024-08-25 02:05:14

这是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

Here is the VB.net code

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文