gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:37:11 24 4
gpt4 key购买 nike

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

最佳答案

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

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

关于c# - 选中时如何更改 System.Windows.Forms.ToolStripButton 突出显示/背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2097164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com