Wie kann ich eine Combo-Box mit einer Liste aller verfügbaren Schriftarten im System füllen?Füllen ComboBox mit Liste der verfügbaren Schriftarten
Antwort
können Sie System.Drawing.FontFamily.Families
verwenden, um die verfügbaren Schriftarten zu erhalten.
Ich habe Montserrat Font installiert. Dieser Code listet die Montserrat-Schriftarten nicht auf, aber Microsoft Word und Systemsteuerung-Darstellung und Visualisierung führt die Schriftart außerdem auf. Also was ist los? Download URl Schriftart ist https://www.fontsquirrel.com/fonts/montserrat – qub1n
Ich habe es. Diese Code-Liste nur TrueType-Schriften, siehe dieses Update http://stackoverflow.com/questions/329225/fonts-missing-in-winforms-fontdialog – qub1n
Verwendung installiert Font Collection Klasse:
http://msdn.microsoft.com/en-us/library/system.drawing.text.installedfontcollection.aspx
Dies ist gleichwertige Alternative Ansatz von Zach Johnson zu beantworten.
List<string> fonts = new List<string>();
InstalledFontCollection installedFonts = new InstalledFontCollection();
foreach (FontFamily font in installedFonts.Families)
{
fonts.Add(font.Name);
}
Nicht sicher, warum wir foreach
hier brauchen.
IList<string> fontNames = FontFamily.Families.Select(f => f.Name).ToList();
Ich fragte mich. Aber da es ein Aufzählungszeichen ist, braucht man nicht immer eine 'foreach'? Oder gibt es eine C# -Syntax für etwas wie 'Python's Generator? – Jamie
'IList
Sie können einfach so binden:
ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}"
ComboBox1.ItemsSource = new InstalledFontCollection().Families;
und zum ersten Mal ausgewählten Element:
private void Combo1_Loaded(object sender, RoutedEventArgs e)
{
ComboBox1.Text = "Tahoma";
}
Dies ist der einfache Weg, es zu tun. Es umfasst zwei Comboboxen 1 für den Namen der Schriftart und eine für die Schriftgröße
public FontFamily[] Families { get; }
private void Form1_Load(object sender, EventArgs e)
{
foreach (FontFamily oneFontFamily in FontFamily.Families)
{
comboBox1.Items.Add(oneFontFamily.Name);
}
comboBox1.Text = this.richTextBox1.Font.Name.ToString();
comboBox2.Text = this.richTextBox1.Font.Size.ToString();
richTextBox1.Focus();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
float size = Convert.ToSingle(((ComboBox)sender).Text);
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, size);
}
Bitte haben Sie einen Blick auf diese Beispiele (http://www.java2s.com/Code/CSharp/GUI-Windows-Form/Loadallsysteminstalledfonts .htm) (http://www.java2s.com/Code/CSharp/GUI-Windows-Form/Loadallsysteminstalledfonts.htm), (http://www.java2s.com/Code/CSharp/GUI-Windows-Form/ Fontlist.htm) (http://www.java2s.com/Code/CSharp/GUI-Windows-Form/Fontlist.htm). – thelost
@thelost der Link existiert nicht mehr –