קוד לבוחר פונטים בשביל C#
-
הייתי צריך משהו יותר ממוקד מfontdialog המובנה של windows form
אולי למישהו אחר גם יהיה מזה תועלת
יש להוסיף class חדש לפרוייקט להעתיק לתוכו את הקוד כמות שהוא.using System; using System.Drawing; using System.Windows.Forms; using System.Linq; public class CustomFontSelectionDialog : Form { private ListBox fontListBox; private Label previewLabel; private Button okButton; private TextBox searchBox; public Font SelectedFont { get; private set; } public CustomFontSelectionDialog() { this.Text = "בחר גופן"; this.Size = new Size(450, 280); this.RightToLeft = RightToLeft.Yes; this.RightToLeftLayout = true; searchBox = new TextBox(); searchBox.Location = new Point(10, 10); searchBox.Size = new Size(200, 20); searchBox.TextChanged += SearchBox_TextChanged; this.Controls.Add(searchBox); fontListBox = new ListBox(); fontListBox.Location = new Point(10, searchBox.Bottom + 10); fontListBox.Size = new Size(200, 180); fontListBox.SelectedIndexChanged += FontListBox_SelectedIndexChanged; this.Controls.Add(fontListBox); previewLabel = new Label(); previewLabel.Location = new Point(fontListBox.Right + 10, 10); previewLabel.Size = new Size(180, this.ClientSize.Height - 2 * 20 - 40); // Adjusted for searchBox and margin previewLabel.Font = new Font("Arial", 12); previewLabel.Text = "תצוגה מקדימה"; // Sample Hebrew text for preview previewLabel.BackColor = Color.White; this.Controls.Add(previewLabel); okButton = new Button(); okButton.Text = "אישור"; okButton.Location = new Point(previewLabel.Right - okButton.Width, this.ClientSize.Height - okButton.Height - 20); okButton.Click += OkButton_Click; this.Controls.Add(okButton); // Populate the fontListBox with available fonts foreach (FontFamily fontFamily in FontFamily.Families) { fontListBox.Items.Add(fontFamily.Name); } } private void SearchBox_TextChanged(object sender, EventArgs e) { string searchText = searchBox.Text.ToLower(); // Convert search text to lowercase fontListBox.Items.Clear(); // Filter and display fonts that match the search text (case-insensitive) foreach (FontFamily fontFamily in FontFamily.Families) { if (fontFamily.Name.ToLower().Contains(searchText)) { fontListBox.Items.Add(fontFamily.Name); } } //fontListBox.SelectedIndex = 0; } private void FontListBox_SelectedIndexChanged(object sender, EventArgs e) { string selectedFontName = fontListBox.SelectedItem as string; if (selectedFontName != null) { SelectedFont = new Font(selectedFontName, 12); // Set a default size previewLabel.Font = SelectedFont; } } private void OkButton_Click(object sender, EventArgs e) { // The user has selected a font, you can use the SelectedFont property here. DialogResult = DialogResult.OK; this.Close(); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new CustomFontSelectionDialog()); } }וזה הקוד שלי לפתיחת הבוחר פונטים
שימו לב: אפשר להשתמש עם זה כדי להגדיר פונט בתוכנה שלכם על ידי שימוש ב resources >Settings שם תוכלו להגדיר משתנה שישמור את שם הפונט שהשתמשתם בו כמוצג בקוד דלהלן.private void chooseFontForBooks_Click(object sender, EventArgs e) { using (CustomFontSelectionDialog fontDialog = new CustomFontSelectionDialog()) { if (fontDialog.ShowDialog() == DialogResult.OK) { Properties.Settings.Default.fontName = fontDialog.SelectedFont.Name; Properties.Settings.Default.Save(); } } }
שלום! נראה שהשיחה הזו מעניינת אותך, אבל עדיין אין לך חשבון.
נמאס לכם לגלול בין אותם הפוסטים בכל ביקור? כשנרשמים לחשבון, תמיד תחזרו בדיוק למקום שבו הייתם קודם, ותוכלו לבחור לקבל התראות על תגובות חדשות (בין אם במייל, ובין אם בהתראת פוש). תוכלו גם לשמור סימניות ולפרגן ב-upvote לפוסטים כדי להביע הערכה לחברי קהילה אחרים.
בעזרת התרומה שלך, הפוסט הזה יכול להיות אפילו טוב יותר 💗
הרשמה התחברות