如何让禁用的 ToolStripButton 为其图像绘制彩色?

发布于 2024-07-14 04:23:47 字数 179 浏览 9 评论 0原文

我们有一个按钮允许用户“锁定”表单。 不允许用户“解锁”表单,因此当按下该按钮时,我们希望禁用该按钮,以便用户收到适当的视觉反馈。

然而,客户报告说,灰色的“锁定”图标向他们表明表单未锁定,因此我们希望以按下状态显示按钮,但图标为彩色,即使该按钮已禁用。

我们如何做到这一点,以绝对最少的覆盖绘画量?

We have a button which allows users to 'lock' a form. Users are not permitted to 'unlock' the form, so when pressed, we want the button to be disabled, so that the user receives appropriate visual feedback.

However, the customer reports that the greyed 'lock' icon suggests to them that the form is not locked, so we would like to display the button in a pressed state, but with the icon in colour, even though the button is disabled.

How do we do that, with the absolute minimum amount of overridden painting?

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

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

发布评论

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

评论(4

甜是你 2024-07-21 04:23:47

事实上我不同意这种做法。 您的客户可能有正确的观点,但我认为他们没有正确的修复建议。 您应该寻找其他方法使表单显示为“锁定”。 更改边框或字体颜色、出现大挂锁图标或从打开变为关闭等。按钮看起来一定是这样,因为这是用户所期望的。 如果您有一个看起来像是已启用的禁用按钮,这会让用户感到困惑,他们可能不明白为什么他们无法单击它。

Actually I disagree with the approach. Your customer might have a valid point, but I don't think they have the correct suggestion for the fix. You should look into other ways to make the form appear "locked". Changing borders or font colours, having a big padlock icon appear or change from open to closed etc. Buttons look a certain way because that's what users expect. If you have a disabled button that looks like it might be enabled, that's going to confuse users who might not then understand why they can't click it.

十级心震 2024-07-21 04:23:47

您可以将 ToolStripButton Image 设置为 BackgroundImage
然后将 DiplayStyle 设置为 None

无论按钮 Enabled 值是什么,图片都应保持彩色。

You can set the ToolStripButton Image as BackgroundImage
and then set the DiplayStyle to None.

The picture should stay in colour no matter what's the button Enabled value.

岁月如刀 2024-07-21 04:23:47

几周前我就遇到过这种情况,这个问题没有答案,所以这是我 4 年后的解决方案:

在许多情况下,我使用此解决方案重新绘制 ToolStripButton:禁用、选中、选择

  1. 覆盖 OnPaint 事件或更改 ToolStrip 的渲染器
  2. 使用此方法绘制没有灰色滤镜的图像,只需设置按钮的图像和按钮上的图像位置: http://msdn.microsoft.com/en-us/library/chas0s9c(v=vs.110).aspx

两个好帮助我的示例:

ToolStrip 类:

ToolStrip ts = new ToolStrip();
//[...]
ts.RenderMode = ToolStripRenderMode.Professional; //Professional is just an example
ts.Renderer = new CustomRenderer();

CustomRenderer 类:

public class CustomRenderer: ToolStripProfessionalRenderer
{
    protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
    {
        if (!e.Item.Enabled) //in this case you will just have the image in your button, you need to add text etc in this if
        {
             //to draw the image
             Graphics g = e.Graphics;
             g.DrawImageUnscaled(e.Item.Image, new Point(2, 2)); //you need to specify the correct position
        }
        else //other cases
            base.OnRenderButtonBackground(e);
     }
}

问题是要知道 ToolStripButton 中所有元素的正确位置,这就是我在每种情况下绘制 ToolStripButton 的原因

I was in this case few weeks ago and this question doesn't have an answer, so this is my solution 4 years later :

I'm using this solution to repaint the ToolStripButton in many cases : disabled, checked, selected

  1. override OnPaint event or change renderer of your ToolStrip
  2. draw your image without grey filter with this method, just set the image of the button and image's position on your button : http://msdn.microsoft.com/en-us/library/chas0s9c(v=vs.110).aspx

Two good examples who helped me :

ToolStrip class :

ToolStrip ts = new ToolStrip();
//[...]
ts.RenderMode = ToolStripRenderMode.Professional; //Professional is just an example
ts.Renderer = new CustomRenderer();

CustomRenderer class :

public class CustomRenderer: ToolStripProfessionalRenderer
{
    protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
    {
        if (!e.Item.Enabled) //in this case you will just have the image in your button, you need to add text etc in this if
        {
             //to draw the image
             Graphics g = e.Graphics;
             g.DrawImageUnscaled(e.Item.Image, new Point(2, 2)); //you need to specify the correct position
        }
        else //other cases
            base.OnRenderButtonBackground(e);
     }
}

The problem is to know the correct position of all elements in your ToolStripButton , that's why I drawn the ToolStripButton in every cases

双手揣兜 2024-07-21 04:23:47

另一种方法:根本不设置 button.Enabled = false 。 只需设置 button.Checked = truefalse,当有人单击按钮时测试 Checked 状态并执行您所做的任何操作。 然后按钮将按照您想要的方式运行并保持颜色。

Another way: don't set the button.Enabled = false at all. Just set the button.Checked = true or false and when someone clicks on the button test for the Checked state and do whatever you do. The button will then function as you want and stay colored too.

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