如何获取ColorDialog中使用的基本颜色的值?

时间:2009-08-16 23:31:24

标签: vb.net colors colordialog

我可以使用ColorDialog获取myColorDialog.CustomColors(vb.net)中使用的自定义颜色,这将返回一个颜色数组作为整数值。

是否有可能以类似的方式获得48种基本色彩?

3 个答案:

答案 0 :(得分:1)

幸运的是,我刚刚制作了一个自定义颜色对话框。 看看你是否喜欢它..

表格代码

Imports System.Drawing
    Public Class Form1
    Dim lbl() As Windows.Forms.Label
     Public t_Color As Color
       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim colorsArray As System.Array = [Enum].GetValues(GetType(KnownColor))
    Dim colorNames As System.Array = [Enum].GetNames(GetType(KnownColor))
    Dim clr As Color
    clr = Color.FromKnownColor(colorsArray(30))
    'MsgBox(colorsArray.Length)
    Dim indx() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
    Dim Xindex, Yindex As Integer
    Dim tmp
    ReDim lbl(colorsArray.Length)
    Me.Size = New Size(505, 312)
    For i = 0 To colorsArray.Length - 1
        tmp = i Mod 14
        Xindex = indx(tmp)
        Yindex = Int(i / 14)

        lbl(i) = New Windows.Forms.Label
        lbl(i).AutoSize = False
        lbl(i).Size = New Size(30, 15)
        lbl(i).BackColor = Color.FromKnownColor(colorsArray(i))
        lbl(i).Tag = colorNames(i).ToString
        lbl(i).Location = New Point((Xindex * 35) + 5, (Yindex * 20) + 40)
        lbl(i).BorderStyle = Windows.Forms.BorderStyle.FixedSingle
        'lbl(i)
        AddHandler lbl(i).MouseMove, AddressOf Label1_MouseMove
        AddHandler lbl(i).MouseHover, AddressOf Label1_MouseHover
        AddHandler lbl(i).Click, AddressOf Label1_Click
        Me.Controls.Add(lbl(i))
    Next
    lbl(colorsArray.Length) = New Windows.Forms.Label
    lbl(colorsArray.Length).Location = New Point(lbl(colorsArray.Length - 1).Location.X + 5, lbl(colorsArray.Length - 1).Location.Y)
    lbl(colorsArray.Length).AutoSize = True
    lbl(colorsArray.Length).Font = Me.Font
    Me.Size = New Size(502, 328)
    Me.Show()

End Sub

Private Sub Label1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.Click
    Me.DialogResult = Windows.Forms.DialogResult.OK
    sender.backcolor()
    Me.Close()
End Sub

Private Sub Label1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseHover
    'Label2.Text = sender.backcolor.toknowncolor.ToString

End Sub


Private Sub Label1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
    Label2.Text = sender.backcolor.toknowncolor.ToString
    t_Color = sender.backcolor
End Sub

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

End Sub
  End Class

Forrm DEsign

       $
     <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
     Partial Class Form1
    Inherits System.Windows.Forms.Form

  'Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.components = New System.ComponentModel.Container()
    Me.Label1 = New System.Windows.Forms.Label()
    Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
    Me.Label2 = New System.Windows.Forms.Label()
    Me.SuspendLayout()
    '
    'Label1
    '
    Me.Label1.BackColor = System.Drawing.Color.Black
    Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
    Me.Label1.Location = New System.Drawing.Point(18, 11)
    Me.Label1.Margin = New System.Windows.Forms.Padding(4, 0, 4, 0)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(60, 0)
    Me.Label1.TabIndex = 0
    Me.Label1.Text = "Label1"
    '
    'Label2
    '
    Me.Label2.AutoSize = True
    Me.Label2.BackColor = System.Drawing.Color.Transparent
    Me.Label2.Location = New System.Drawing.Point(164, 9)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New System.Drawing.Size(55, 16)
    Me.Label2.TabIndex = 1
    Me.Label2.Text = "Label2"
    '
    'Form1
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(9.0!, 16.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(1185, 494)
    Me.ControlBox = False
    Me.Controls.Add(Me.Label2)
    Me.Controls.Add(Me.Label1)
    Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
    Me.Margin = New System.Windows.Forms.Padding(4)
    Me.Name = "Form1"
    Me.Text = "Click on Colour to Select"
    Me.ResumeLayout(False)
    Me.PerformLayout()

End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
Friend WithEvents Label2 As System.Windows.Forms.Label
End Class

答案 1 :(得分:0)

它们可用作struct System.Drawing.Color

的静态属性

示例:

Color.AliceBlue
Color.AntiqueWhite
Color.Aqua

编辑:对不起,这里的内容多于对话框中的内容,因此可能不是您想要的内容。

答案 2 :(得分:0)

我花了很多时间寻找一种以编程方式获取基本颜色列表的方法,但没有找到这样的方法。 我目前的解决方案=):

List<Color> basicColors = new List<Color>
            {
                Color.FromArgb(255, 255, 128, 128),
                Color.FromArgb(255, 255, 255, 128),
                Color.FromArgb(255, 128, 255, 128),
                Color.FromArgb(255, 0, 255, 128),
                Color.FromArgb(255, 128, 255, 255),
                Color.FromArgb(255, 0, 128, 255),
                Color.FromArgb(255, 255, 128, 192),
                Color.FromArgb(255, 255, 128, 255),
                Color.FromArgb(255, 255, 0, 0),
                Color.FromArgb(255, 255, 255, 0),
                Color.FromArgb(255, 128, 255, 0),
                Color.FromArgb(255, 0, 255, 64),
                Color.FromArgb(255, 0, 255, 255),
                Color.FromArgb(255, 0, 128, 192),
                Color.FromArgb(255, 128, 128, 192),
                Color.FromArgb(255, 255, 0, 255),
                Color.FromArgb(255, 128, 64, 64),
                Color.FromArgb(255, 255, 128, 64),
                Color.FromArgb(255, 0, 255, 0),
                Color.FromArgb(255, 0, 128, 128),
                Color.FromArgb(255, 0, 64, 128),
                Color.FromArgb(255, 128, 128, 255),
                Color.FromArgb(255, 128, 0, 64),
                Color.FromArgb(255, 255, 0, 128),
                Color.FromArgb(255, 128, 0, 0),
                Color.FromArgb(255, 255, 128, 0),
                Color.FromArgb(255, 0, 128, 0),
                Color.FromArgb(255, 0, 128, 64),
                Color.FromArgb(255, 0, 0, 255),
                Color.FromArgb(255, 0, 0, 160),
                Color.FromArgb(255, 128, 0, 128),
                Color.FromArgb(255, 128, 0, 255),
                Color.FromArgb(255, 64, 0, 0),
                Color.FromArgb(255, 128, 64, 0),
                Color.FromArgb(255, 0, 64, 0),
                Color.FromArgb(255, 0, 64, 64),
                Color.FromArgb(255, 0, 0, 128),
                Color.FromArgb(255, 0, 0, 64),
                Color.FromArgb(255, 64, 0, 64),
                Color.FromArgb(255, 64, 0, 128),
                Color.FromArgb(255, 0, 0, 0),
                Color.FromArgb(255, 128, 128, 0),
                Color.FromArgb(255, 128, 128, 64),
                Color.FromArgb(255, 128, 128, 128),
                Color.FromArgb(255, 64, 128, 128),
                Color.FromArgb(255, 192, 192, 192),
                Color.FromArgb(255, 64, 0, 64),
                Color.FromArgb(255, 255, 255, 255)
            };