Tipp 0052 Farbwerte konvertieren
Autor/Einsender:
Datum:
  Michael Werner
01.08.2004
Entwicklungsumgebung:   VB.Net 2003
Framework:   1.1
Dieser Tipp demonstriert, wie man den ColorDialog einsetzen kann und mit Hilfe der Klasse ColorTranslator Farbwerte in RGB-, Html- und Hexadizimal-Werte konvertieren kann.
 
Public Class Form1
  Inherits System.Windows.Forms.Form
Vom Windows Form Designer generierter Code
  Dim ColTrans As ColorTranslator
  Dim r, g, b As Long

  Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    ColorDialog1.ShowDialog()
    PictureBox1.BackColor = ColorDialog1.Color
    ColorToRGB(PictureBox1.BackColor)
    SetColorValue(r, g, b)
  End Sub

  Private Sub ColorToRGB(ByVal col As Color)
    r = ColTrans.ToOle(col) And &HFF
    g = ColTrans.ToOle(col) \ &H100 And &HFF
    b = ColTrans.ToOle(col) \ &H10000 And &HFF
    lblRed.Text = CStr(r)
    lblGreen.Text = CStr(g)
    lblBlue.Text = CStr(b)
  End Sub

  Private Sub SetColorValue(ByVal r, ByVal g, ByVal b)
    PictureBox1.BackColor = ColTrans.FromWin32(RGB(r, g, b))

    Label1.Text = Hex(ColTrans.ToOle(PictureBox1.BackColor))
    Label2.Text = ColTrans.ToHtml(PictureBox1.BackColor)
    Label3.Text = "RGB(" & CStr(r) & ", " & CStr(g) & ", " & _
                           CStr(b) & ")"

    If RadioButton1.Checked Then
      Clipboard.SetDataObject(Label1.Text)
    ElseIf RadioButton2.Checked Then
      Clipboard.SetDataObject(Label2.Text)
    ElseIf RadioButton3.Checked Then
      Clipboard.SetDataObject(Label3.Text)
    End If
  End Sub

  Private Sub TrackBar1_Scroll(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles TrackBar1.Scroll
    r = TrackBar1.Value
    lblRed.Text = CLng(r)
    SetColorValue(r, g, b)
  End Sub

  Private Sub TrackBar2_Scroll(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles TrackBar2.Scroll
    g = TrackBar2.Value
    lblGreen.Text = CLng(g)
    SetColorValue(r, g, b)
  End Sub

  Private Sub TrackBar3_Scroll(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles TrackBar3.Scroll
    b = TrackBar3.Value
    lblBlue.Text = CLng(b)
    SetColorValue(r, g, b)
  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
    lblRed.Text = CStr(0)
    lblGreen.Text = CStr(0)
    lblBlue.Text = CStr(0)
    SetColorValue(0, 0, 0)
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button2.Click
    Me.Close()
  End Sub
End Class
 
Weitere Links zum Thema
Bekannte Farben auslesen (KnownColors)
Color Picker
Farb-Bitmaps zu Graustufen konvertieren

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (7,5 kB) Downloads bisher: [ 644 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Freitag, 20. Januar 2012