Color.FromKnownColor-Methode
Erstellt eine Color-Struktur aus der angegebenen vordefinierten Farbe.
Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)
Syntax
'Declaration
Public Shared Function FromKnownColor ( _
color As KnownColor _
) As Color
'Usage
Dim color As KnownColor
Dim returnValue As Color
returnValue = Color.FromKnownColor(color)
public static Color FromKnownColor (
KnownColor color
)
public:
static Color FromKnownColor (
KnownColor color
)
public static Color FromKnownColor (
KnownColor color
)
public static function FromKnownColor (
color : KnownColor
) : Color
Parameter
- color
Ein Element der KnownColor-Enumeration.
Rückgabewert
Die Color, die von dieser Methode erstellt wird.
Hinweise
Vordefinierte Farben werden auch als bekannte Farben bezeichnet und durch ein Element der KnownColor-Enumeration dargestellt.
Beispiel
Das folgende Codebeispiel ist für die Verwendung mit Windows Forms vorgesehen und erfordert PaintEventArgse, wobei es sich um einen Parameter des Paint-Ereignishandlers handelt. Der Code führt die folgenden Aktionen aus:
Erstellt eine Instanz einer Color-Struktur mit dem Namen
redShade
, die für Vergleiche verwendet wird.Durchläuft die KnownColor-Enumerationselemente, um alle bekannten Farben mit derselben Helligkeit wie
redShade
zu suchen. Der Durchlauf wird abgebrochen, wenn 15 Übereinstimmungen gefunden wurden oder der Wert des Schleifenzählers größer als das letzte KnownColor-Element ist.In jedem Durchlauf wird das KnownColor-Element in einem Array gespeichert, sofern es den Kriterien entspricht.
Verwendet einen Pinsel zum Zeichnen von Rechtecken.
Das erste Rechteck wird mit der durch redShade
dargestellten Farbe gezeichnet. Alle anderen Rechtecke werden mit einer KnownColor gezeichnet, deren Helligkeit mit der von redShade
übereinstimmt.
Public Sub KnownColorBrightnessExample1(ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics
' Color structures. One is used for temporary storage. The other
' is a constant used for comparisons.
Dim someColor As Color = Color.FromArgb(0)
Dim redShade As Color = Color.FromArgb(255, 200, 0, 100)
' Array to store KnownColor values that match the brightness of the
' redShade color.
Dim colorMatches(15) As KnownColor
' Number of matches found.
Dim count As Integer = 0
' iterate through the KnownColor enums until 15 matches are found.
Dim enumValue As KnownColor
For enumValue = 0 To KnownColor.YellowGreen
someColor = Color.FromKnownColor(enumValue)
If (someColor.GetBrightness()) = (redShade.GetBrightness()) Then
colorMatches(count) = enumValue
count += 1
If count > 15 Then
Exit For
End If
End If
Next enumValue
' Display the redShade color and its argb value.
Dim myBrush1 As New SolidBrush(redShade)
Dim myFont As New Font("Arial", 12)
Dim x As Integer = 20
Dim y As Integer = 20
someColor = redShade
g.FillRectangle(myBrush1, x, y, 100, 30)
g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
x + 120, y)
' Iterate through the matches that were found and display each
' color that corresponds with the enum value in the array.
' Display the name of the KnownColor.
Dim i As Integer
For i = 0 To count - 1
y += 40
someColor = Color.FromKnownColor(colorMatches(i))
myBrush1.Color = someColor
g.FillRectangle(myBrush1, x, y, 100, 30)
g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
x + 120, y)
Next i
End Sub
public void KnownColorBrightnessExample1(PaintEventArgs e)
{
Graphics g = e.Graphics;
// Color structures. One is a variable used for temporary storage. The other
// is a constant used for comparisons.
Color someColor = Color.FromArgb(0);
Color redShade = Color.FromArgb(255, 200, 0, 100);
// Array to store KnownColor values that match the brightness of the
// redShade color.
KnownColor[] colorMatches = new KnownColor[15];
// Number of matches found.
int count = 0;
// Iterate through the KnownColor enums until 15 matches are found.
for (KnownColor enumValue = 0;
enumValue <= KnownColor.YellowGreen && count < 15; enumValue++)
{
someColor = Color.FromKnownColor(enumValue);
if (someColor.GetBrightness() == redShade.GetBrightness())
colorMatches[count++] = enumValue;
}
// Display the redShade color and its argb value.
SolidBrush myBrush1 = new SolidBrush(redShade);
Font myFont = new Font("Arial", 12);
int x = 20;
int y = 20;
someColor = redShade;
g.FillRectangle(myBrush1, x, y, 100, 30);
g.DrawString(someColor.ToString(), myFont, Brushes.Black, x + 120, y);
// Iterate through the matches that were found and display each color that
// Corresponds with the enum value in the array. also display the name of
// The KnownColor.
for (int i = 0; i < count; i++)
{
y += 40;
someColor = Color.FromKnownColor(colorMatches[i]);
myBrush1.Color = someColor;
g.FillRectangle(myBrush1, x, y, 100, 30);
g.DrawString(someColor.ToString(), myFont, Brushes.Black, x + 120, y);
}
}
void KnownColorBrightnessExample1( PaintEventArgs^ e )
{
Graphics^ g = e->Graphics;
// Color structures. One is a variable used for temporary storage. The other
// is a constant used for comparisons.
Color someColor = Color::FromArgb( 0 );
Color redShade = Color::FromArgb( 255, 200, 0, 100 );
// Array to store KnownColor values that match the brightness of the
// redShade color.
array<KnownColor>^colorMatches = gcnew array<KnownColor>(15);
// Number of matches found.
int count = 0;
// Iterate through the KnownColor enums until 15 matches are found.
for ( KnownColor enumValue = (KnownColor)0; enumValue <= KnownColor::YellowGreen && count < 15; enumValue = enumValue + (KnownColor)1 )
{
someColor = Color::FromKnownColor( enumValue );
if ( someColor.GetBrightness() == redShade.GetBrightness() )
colorMatches[ count++ ] = enumValue;
}
// Display the redShade color and its argb value.
SolidBrush^ myBrush1 = gcnew SolidBrush( redShade );
System::Drawing::Font^ myFont = gcnew System::Drawing::Font( "Arial",12 );
int x = 20;
int y = 20;
someColor = redShade;
g->FillRectangle( myBrush1, x, y, 100, 30 );
g->DrawString( someColor.ToString(), myFont, Brushes::Black, (float)x + 120, (float)y );
// Iterate through the matches that were found and display each color that
// Corresponds with the enum value in the array. also display the name of
// The KnownColor.
for ( int i = 0; i < count; i++ )
{
y += 40;
someColor = Color::FromKnownColor( colorMatches[ i ] );
myBrush1->Color = someColor;
g->FillRectangle( myBrush1, x, y, 100, 30 );
g->DrawString( someColor.ToString(), myFont, Brushes::Black, (float)x + 120, (float)y );
}
}
public void KnownColorBrightnessExample1(PaintEventArgs e)
{
Graphics g = e.get_Graphics();
// Color structures. One is a variable used for temporary storage.
// The other is a constant used for comparisons.
Color someColor = Color.FromArgb(0);
Color redShade = Color.FromArgb(255, 200, 0, 100);
// Array to store KnownColor values that match the brightness of the
// redShade color.
KnownColor colorMatches[] = new KnownColor[15];
// Number of matches found.
int count = 0;
// Iterate through the KnownColor enums until 15 matches are found.
for (KnownColor enumValue = (KnownColor)0;
(enumValue.CompareTo(KnownColor.YellowGreen) <= 0)
&& (count < 15); enumValue++) {
someColor = Color.FromKnownColor(enumValue);
if (someColor.GetBrightness() == redShade.GetBrightness()) {
colorMatches.set_Item(count++, enumValue);
}
}
// Display the redShade color and its argb value.
SolidBrush myBrush1 = new SolidBrush(redShade);
Font myFont = new Font("Arial", 12);
int x = 20;
int y = 20;
someColor = redShade;
g.FillRectangle(myBrush1, x, y, 100, 30);
g.DrawString(someColor.ToString(), myFont, Brushes.get_Black(),
x + 120, y);
// Iterate through the matches that were found and display each color
// that Corresponds with the enum value in the array. also display the
// name of The KnownColor.
for (int i = 0; i < count; i++) {
y += 40;
someColor = Color.FromKnownColor(
(KnownColor)colorMatches.get_Item(i));
myBrush1.set_Color(someColor);
g.FillRectangle(myBrush1, x, y, 100, 30);
g.DrawString(someColor.ToString(), myFont, Brushes.get_Black(),
x + 120, y);
}
} //KnownColorBrightnessExample1
Plattformen
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0