【问题标题】:Disabling the "disabled look" of disabled buttons (VB.net for desktop)禁用禁用按钮的“禁用外观”(桌面版 VB.net)
【发布时间】:2014-08-30 00:47:27
【问题描述】:

我的应用中有很多按钮,其中一些按钮在各种情况下被禁用。 问题是,当 .Enabled = False 时,按钮“看起来不对”

以下是可以类似地应用于所有按钮的属性列表示例。

.BackColor = Color.Goldenrod
.Flatstyle = FlatStyle.Flat
.FlatAppearance.MouseOverBackColor = Color.White
.FlatAppearance.BorderSize = 0
.BackgroundImageLayout = ImageLayout.Stretch
.BackGroundImage = My.Resources.Resources.ButtonFade 'This image is translucent, giving the rounded 3D look as shown below.
.ForeColor = Color.Black
.Image = My.Resources.Resources.refresh 'May be other images.
.Text = "RELOAD"

.BackColor 属性可以是各种颜色,由用户通过“主题”设置。

为了说明我的担忧,下面是三个按钮的截图。 “新”已启用。 “保存”被禁用。尽管“NEW”和“SAVE”看起来很相似,但“SAVE”的文本和图像对比度较低。

我希望所有禁用的按钮看起来更像“重新加载”。也就是说,为了更好的易读性,我希望文本和图像保持纯黑色,但我可以设置 BackgroundImage = Nothing 所以它看起来不是 3D。 (对用户而言,模型是“如果不是 3D,则不可点击。”)我可能还会修改禁用按钮的背景颜色,但这部分很简单。当我设置 Enabled = False 时,我只需要系统停止“灰显”文本和图像。

为了得到这个截图,“RELOAD”实际上是启用的,但我已经删除了它的背景图片。问题是,它仍然可以点击。

如何获得我想要的外观?

【问题讨论】:

  • 您可以自己绘制禁用图像。
  • 并非如此。当按钮被禁用时,我绘制的任何图像都会变灰(对比度较低)。当按钮被禁用时,我想覆盖文本和图像的“灰显”。

标签: vb.net winforms look-and-feel


【解决方案1】:

您无法通过使用 Enabled 属性来实现您想要的,Button 类实现了 Windows GUI 样式指南,禁用的控件应该通过灰显它们的外观来看起来是禁用的。另一个限制是按钮渲染器不能被修改,它们不能被覆盖。

您需要通过禁用控件act 来实现您的目标。向您的项目添加一个新类并粘贴如下所示的代码。编译。将新控件从工具箱顶部拖到表单中,替换现有的按钮控件。当您想要禁用按钮时,在您的代码中将 Disabled 属性设置为 True。您可能想要修改更改外观的代码。

Imports System.ComponentModel
Public Class MyButton
    Inherits Button

    <DefaultValue(False)> _
    Public Property Disabled As Boolean
        Get
            Return IsDisabled
        End Get
        Set(value As Boolean)
            If Value = IsDisabled Then Return
            IsDisabled = Value
            MyBase.SetStyle(ControlStyles.Selectable, Not IsDisabled)
            If IsDisabled And Me.Focused Then Me.Parent.SelectNextControl(Me, True, True, True, True)
            '' Change appearance...
            If IsDisabled Then
                Me.FlatStyle = Windows.Forms.FlatStyle.Flat
            Else
                Me.FlatStyle = Windows.Forms.FlatStyle.Standard
            End If
        End Set
    End Property

    Protected Overrides Sub OnMouseEnter(e As EventArgs)
        If Not IsDisabled Then MyBase.OnMouseEnter(e)
    End Sub

    Protected Overrides Sub OnMouseDown(mevent As MouseEventArgs)
        If Not IsDisabled Then MyBase.OnMouseDown(mevent)
    End Sub

    Protected Overrides Sub OnKeyDown(kevent As KeyEventArgs)
        If Not IsDisabled Then MyBase.OnKeyDown(kevent)
    End Sub

    Private IsDisabled As Boolean
End Class

【讨论】:

  • 谢谢。这正是我所需要的。
【解决方案2】:

我在 c 中这样做的方式(对于极端 gui 的东西更强大。这个例子很简单!)覆盖禁用状态并绘制我的图像(在 c 中):

NMHDR *nmr;
NMCUSTOMDRAW *nmcd;

case WM_NOTIFY:
    nmr = (NMHDR *)lParam;
    nmcd = (NMCUSTOMDRAW *)lParam;

    if(nmr->idFrom == IDC_BUTTON && nmr->code == NM_CUSTOMDRAW){
        if(nmcd->dwDrawStage == CDDS_PREERASE){
            if(nmcd->uItemState & 0x1)      {StretchBlt(nmcd->hdc,...);} //Down
            else if(nmcd->uItemState & 0x40){StretchBlt(nmcd->hdc,...);} //Enter
            else if(nmcd->uItemState & 0x4) {StretchBlt(nmcd->hdc,...);} //Disable
            else                            {StretchBlt(nmcd->hdc,...);} //Leave

            return CDRF_SKIPDEFAULT;
         }
     }

break;

WM_NOTIFY 被发送到您的主窗体,以便您可以捕获它。 nmcd->hdc 是您的按钮 hdc,您可以根据状态在其上绘制图像(Down、Enter、 禁用或离开)。我知道从 cvb 很难,但如果你有足够的耐心,你会有一个起点。

瓦尔特

【讨论】:

    猜你喜欢
    • 2018-03-19
    • 2011-01-07
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2015-02-23
    相关资源
    最近更新 更多