2014-04-25 69 views
0

有沒有人知道顯示標準顏色名稱的Visual Studio(Visual Basic)的color-picker顯示顏色名稱的顏色拾取器

例如,在Visual Studio中,您可以使用具有"Custom","Web""System"標籤的color-picker來更改控件的顏色。 Web & System選項顯示顏色名稱的列表,而Custom提供(主要)RGB(這是VB ColorPicker控件的作用)。

謝謝!

+0

http://social.msdn.microsoft.com/Forums/it-IT/e21c8b6f-6750-46b1-86be-c69ecac9e7a5/colorpicker-combobox?forum=winforms –

+0

嗨,非常感謝您的輸入,最有幫助。漢斯,感謝您的鏈接 - 正是我需要的! – user3012629

回答

0

對於其中之一,除非你想像VS一樣做,並且將系統顏色與命名的顏色區分開來,使它成爲一個彈出窗口或其他窗口,否則它們之間幾乎沒有任何關係。使用顏色作爲背景例如:

' capture the names 
Private _Colors As String() 

' get the names 
' add qualifiers to skip SystemCOlors or 
' Transparent as needed 
Function GetColorNames As String() 
    For Each colorName As String In KnownColor.GetNames(GetType(KnownColor)) 
     _Colors.Add(colorName) 
    End If 
Next 

' post the names to a CBO: 
cboBackColor.Items.AddRange(_Colors) 

在形式CBO的DrawMode設置爲OwnerDrawFixed,則:

Private Sub cboSheetBackColor_DrawItem(ByVal sender As Object, 
      ByVal e As System.Windows.Forms.DrawItemEventArgs) 
      Handles cboSheetBackColor.DrawItem 
    Dim Bclr As Color, Fclr As Color 

    ' get the colors to use for this item for this 
    Bclr = Color.FromName(_Colors(e.Index).ToString) 
    Fclr = GetContrastingColor(Bclr)  ' see below 

    With e.Graphics 
     Using br As New SolidBrush(Bclr) 
      .FillRectangle(br, e.Bounds) 
     End Using 
     Using br As New SolidBrush(Fclr) 
      .DrawString(cboSheetBackColor.Items(e.Index).ToString, 
      cboSheetBackColor.Font, br, 
      e.Bounds.X, e.Bounds.Y) 
     End Using 

    End With 
    e.DrawFocusRectangle() 

End Sub 

可以只畫如Windows/VS中的樣本並通過定義矩形填充。通常情況下,這會膨脹,但是如果您正在定義背景顏色,那麼它有助於展示它如何看起來具有文本以及比小小的色板更多的顏色 - 因此是填充的CBO項目矩形。

標準窗口文字顏色不會顯示在所有的文字上。對於「輕」主題,紫羅蘭和黑色等將隱藏/使顏色名稱不可讀。 GetContrastingColor是評估當前顏色的亮度的功能,然後返回白色或黑色:

Public Function GetContrastingColor(ByVal clrBase As Color) As Color 
    ' Y is the "brightness" 
    Dim Y As Double = (0.299 * clrBase.R) _ 
      + (0.587 * clrBase.G) _ 
      + (0.114 * clrBase.B) 

    If (Y < 140) Then 
     Return Color.White 
    Else 
     Return Color.Black 
    End If 
End Function 

然後就可以使用這一切,從ComboBox中繼承的類,或建立一個UserControlif你喜歡不同的控制。您也可以將它作爲代碼保存在這些場合調用的DLL中。我應該提到CodeProject上也許有十幾個這樣的生物。

0

我不知道現有的控件,但可以使用KnownColor枚舉類和SystemColors類來獲取這些Color值的所有名稱。然後您可以構建自己的控件,例如自定義ComboBox,與該數據。