2014-08-30 70 views
0

我在我的應用程序中有很多按鈕,其中一些在各種情況下都被禁用。 問題是,當「.Enabled = False」時,按鈕「看上去不對」禁用禁用按鈕的「禁用外觀」(VB.net桌面)

以下是可能類似地應用於所有按鈕的屬性列表示例。

.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」已啓用。 「保存」已禁用。雖然「新」和「保存」看起來很相似,但是「保存」用文字和圖像的低對比度顏色沖掉。

我希望所有禁用的按鈕看起來更像「RELOAD」。也就是說,我希望文本和圖像保持純黑色,以獲得更好的可讀性,但是我可以設置BackgroundImage = Nothing,因此它不會顯示3D。 (對用戶來說,模型是「如果它不是3D的,它不是可點擊的。」)我可能還會修改禁用按鈕的背景顏色,但這一部分很容易。當我設置Enabled = False時,我只需要系統停止「變灰」文本和圖像。

要獲得此截圖,「RELOAD」實際上已啓用,但我已刪除其背景圖像。問題是,它仍然可以被點擊。

Buttons

我怎樣才能得到我找一下?

+0

可以繪製禁用圖像你的自我。 – 2014-08-30 08:53:40

+0

不是真的。任何圖像I畫將成爲灰色的(較低的對比色)當按鈕被禁用。我想,當一個按鈕被禁止覆蓋「變灰」的文字和圖像。 – PaulOTron2000 2014-08-30 14:06:24

回答

3

你不能達到你想要使用Enabled屬性是什麼,Button類實現了Windows圖形用戶界面風格指南,殘疾控制應以灰色他們的外表看起來禁用。另一個限制是按鈕渲染器不能被修飾,它們不能被覆蓋。

您需要通過控制動作禁用來實現您的目標。爲您的項目添加一個新類並粘貼下面顯示的代碼。編譯。將新控件從工具箱頂部拖到窗體中,替換現有的按鈕控件。當您想禁用按鈕時,在代碼中將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 
+0

感謝。這就是我需要的。 – PaulOTron2000 2014-08-30 19:29:58

0

我做到這一點在C方式(是極貴的東西的方式更強大這個例子很簡單!)取代禁用狀態並畫出我的圖像(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,而你是你的形象取決於狀態上繪製(下,回車, 禁用或離開)。我知道很難寫vb from c但是如果你有足夠的耐心,你有一個起點。

瓦爾特