Font-Klasse
Aktualisiert: November 2007
Definiert ein bestimmtes Format für Text, einschließlich der Attribute für Schriftart, Schriftgrad und Schriftschnitt. Diese Klasse kann nicht geerbt werden.
Assembly: System.Drawing (in System.Drawing.dll)
[SerializableAttribute] [ComVisibleAttribute(true)] [TypeConverterAttribute(typeof(FontConverter))] public sealed class Font : MarshalByRefObject, ICloneable, ISerializable, IDisposable
/** @attribute SerializableAttribute */ /** @attribute ComVisibleAttribute(true) */ /** @attribute TypeConverterAttribute(FontConverter) */ public final class Font extends MarshalByRefObject implements ICloneable, ISerializable, IDisposable
Weitere Informationen zum Erstellen von Schriftarten finden Sie unter Gewusst wie: Erstellen von Schriftartfamilien und Schriftarten. Windows Forms-Anwendungen unterstützen TrueType-Schriftarten und verfügen über eine begrenzte Unterstützung für OpenType-Schriftarten. Wenn Sie versuchen, eine Schriftart zu verwenden, die nicht unterstützt wird, oder wenn die Schriftart auf dem Computer mit der Anwendung nicht installiert ist, wird die Schriftart Microsoft Sans Serif ersetzt.
Im folgenden Codebeispiel wird die Verwendung des Font-Konstruktors und der Eigenschaften Size, SizeInPoints und Unit veranschaulicht. Dieses Beispiel ist für die Verwendung mit einem Windows Form vorgesehen, das die ComboBox mit der Bezeichnung ComboBox1 mit den Zeichenfolgen "Bigger" und "Smaller" und ein Label mit der Bezeichnung Label1 enthält. Fügen Sie den folgenden Code in das Formular ein, und ordnen Sie die ComboBox1_SelectedIndexChanged-Methode demSelectedIndexChanged-Ereignis des ComboBox-Steuerelements zu.
private void ComboBox1_SelectedIndexChanged(System.Object sender, System.EventArgs e) { // Cast the sender object back to a ComboBox. ComboBox ComboBox1 = (ComboBox) sender; // Retrieve the selected item. string selectedString = (string) ComboBox1.SelectedItem; // Convert it to lowercase. selectedString = selectedString.ToLower(); // Declare the current size. float currentSize; // Switch on the selected item. switch(selectedString) { // If Bigger is selected, get the current size from the // Size property and increase it. Reset the font to the // new size, using the current unit. case "bigger": currentSize = Label1.Font.Size; currentSize += 2.0F; Label1.Font = new Font(Label1.Font.Name, currentSize, Label1.Font.Style, Label1.Font.Unit); // If Smaller is selected, get the current size, in points, // and decrease it by 1. Reset the font with the new size // in points. break; case "smaller": currentSize = Label1.Font.SizeInPoints; currentSize -= 1; Label1.Font = new Font(Label1.Font.Name, currentSize, Label1.Font.Style); break; } }
private void comboBox1_SelectedIndexChanged(Object sender,
System.EventArgs e)
{
// Cast the sender object back to a ComboBox.
ComboBox comboBox1 = (ComboBox)sender;
// Retrieve the selected item.
String selectedString = (String)comboBox1.get_SelectedItem();
// Convert it to lowercase.
selectedString = selectedString.ToLower();
// Declare the current size.
float currentSize;
// Switch on the selected item.
// If Bigger is selected, get the current size from the
// Size property and increase it. Reset the font to the
// new size, using the current unit.
if (selectedString.Equals("bigger")) {
currentSize = label1.get_Font().get_Size();
currentSize += 2;
label1.set_Font(new Font(label1.get_Font().get_Name(),
currentSize, label1.get_Font().get_Style(),
label1.get_Font().get_Unit()));
}
else {
// If Smaller is selected, get the current size, in points,
// and decrease it by 1. Reset the font with the new size
// in points.
if (selectedString.Equals("smaller")) {
currentSize = label1.get_Font().get_SizeInPoints();
currentSize -= 1;
label1.set_Font(new Font(label1.get_Font().get_Name(),
currentSize, label1.get_Font().get_Style()));
}
}
} //comboBox1_SelectedIndexChanged
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile für Smartphone, Windows Mobile für Pocket PC
.NET Framework und .NET Compact Framework unterstützen nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.