2011-12-27 28 views
8
filtreleyen C# için Özel Yazı Diyalog/Seçici tasarlama

, bir Özel'i oluşturmaya çalışıyorum Gerçek olmayan yazı tiplerini filtreleyen Yazı tipi-ailelerini kullanan Yazı Tipi İletişim Kutusu.olmayan true type yazı tipi

Control mükemmel çalışıyor ama bu diyalog bir boyut ve stil seçicileri gerekir. Mevcut kodu gönderiyorum. Lütfen buna bir boyut ve stil seçicisi eklememe yardımcı olun. Aynı zamanda sizin için de yararlı olabilir.

public class FontListBox : ListBox 
{ 
    private List<Font> _fonts = new List<Font>(); 
    private Brush _foreBrush; 

    public FontListBox() 
    { 
     DrawMode = DrawMode.OwnerDrawFixed; 
     ItemHeight = 20; 
     foreach (FontFamily ff in FontFamily.Families) 
     { 
      // determine the first available style, as all fonts don't support all styles 
      FontStyle? availableStyle = null; 
      foreach (FontStyle style in Enum.GetValues(typeof(FontStyle))) 
      { 
       if (ff.IsStyleAvailable(style)) 
       { 
        availableStyle = style; 
        break; 
       } 
      } 

      if (availableStyle.HasValue) 
      { 
       Font font = null; 
       try 
       { 
        // do your own Font initialization here 
        // discard the one you don't like :-) 
        font = new Font(ff, 12, availableStyle.Value); 
       } 
       catch 
       { 
       } 
       if (font != null) 
       { 
        _fonts.Add(font); 
        Items.Add(font); 
       } 
      } 
     } 
    } 

    protected override void Dispose(bool disposing) 
    { 
     base.Dispose(disposing); 
     if (_fonts != null) 
     { 
      foreach (Font font in _fonts) 
      { 
       font.Dispose(); 
      } 
      _fonts = null; 
     } 
     if (_foreBrush != null) 
     { 
      _foreBrush.Dispose(); 
      _foreBrush = null; 
     } 
    } 

    public override Color ForeColor 
    { 
     get 
     { 
      return base.ForeColor; 
     } 
     set 
     { 
      base.ForeColor = value; 
      if (_foreBrush != null) 
      { 
       _foreBrush.Dispose(); 
      } 
      _foreBrush = null; 
     } 
    } 

    private Brush ForeBrush 
    { 
     get 
     { 
      if (_foreBrush == null) 
      { 
       _foreBrush = new SolidBrush(ForeColor); 
      } 
      return _foreBrush; 
     } 
    } 

    protected override void OnDrawItem(DrawItemEventArgs e) 
    { 
     base.OnDrawItem(e); 
     if (e.Index < 0) 
      return; 

     e.DrawBackground(); 
     e.DrawFocusRectangle(); 
     Rectangle bounds = e.Bounds; 
     Font font = (Font)Items[e.Index]; 
     e.Graphics.DrawString(font.Name, font, ForeBrush, bounds.Left, bounds.Top); 
    } 
} 

public partial class MyFontDialog : Form 
{ 
    private FontListBox _fontListBox; 

    public MyFontDialog() 
    { 
     InitializeComponent(); 

     _fontListBox = new FontListBox(); 
     _fontListBox.Dock = DockStyle.Fill; 
     Controls.Add(_fontListBox); 
    } 
} 

Ben https://sourceforge.net/p/newfontpicker/

+0

FontDialog sınıfı zaten TrueType olmayan yazı tiplerini filtreler. Buradaki gerçek çözüm, kötü meta verilere sahip yazı tipini kaldırmaktır. –

+0

http://c-madeeasy.blogspot.com/2011/11/unsolved-this-is-not-true-type-font.html – techno

+0

bkz No.Please Bu açıştürü fontları filtrelemek için çalışır ancak bazı yazı tipleri hala var Olmazsa http://connect.microsoft.com/VisualStudio/feedback/details/708872/this-is-not-a-true-type-font-only-true-type-fonts-are-accepted-exception – techno

cevap

1

Böyle MyFontDialog değiştirebilir:

public partial class MyFontDialog : Form 
{ 
    private FontListBox _fontListBox; 
    private ListBox _fontSizeListBox; 

    public MyFontDialog() 
    { 
     //InitializeComponent(); 

     _fontListBox = new FontListBox(); 
     _fontListBox.SelectedIndexChanged += OnfontListBoxSelectedIndexChanged; 
     _fontListBox.Size = new Size(200, Height); 
     Controls.Add(_fontListBox); 

     _fontSizeListBox = new ListBox(); 
     _fontSizeListBox.Location = new Point(_fontListBox.Width, 0); 

     Controls.Add(_fontSizeListBox); 
    } 

    private void OnfontListBoxSelectedIndexChanged(object sender, EventArgs e) 
    { 
     _fontSizeListBox.Items.Clear(); 
     Font font = _fontListBox.SelectedItem as Font; 
     if (font != null) 
     { 
      foreach (FontStyle style in Enum.GetValues(typeof(FontStyle))) 
      { 
       if (font.FontFamily.IsStyleAvailable(style)) 
       { 
        _fontSizeListBox.Items.Add(style); 
       } 
      } 
     } 
    } 
} 

O kenara bir liste kutusu mevcut yazı stilleri listesi ile yazı liste kutusu oluşturulur. Boyut seçimi ile ilgili olarak, kodlanmış liste büyüklüğüne sahip bir liste kutusu ekleyebilirsiniz: 8,9,10,11,12, 14,16,18,20,22,24,26,28,36,48 ve 72 tıpkı standart FontDialog gibi, gerçek tip yazı tipleriyle uğraştığımızdan. Sen denemelisiniz

0

http://www.developerfusion.com/code/254/determine-if-a-font-is-truetype/ bir yazı bir TT yazı olup olmadığını belirlemek için bazı VB kodu vardır sourceforge de projeyi ağırladık. Tüm gerçekten bazı Win32 API işlevlerini çağırır ve sonuçları inceler.

(FontDialog muhtemelen zaten yapıyor) Win32 API ile incelendiğinde bile, TT gibi görünen ama olmayan bazı yazı tipleri olabilir. Win32 sorununuzu çözmezse, muhtemelen bir yazı tipinin geçersiz olup olmadığını öğrenmenin tek yolu bir istisna olup olmadığını kontrol etmektir.

0

Tamam, Ömer, :/önlemek yakalamak istisnalar ihtiyacını en aza indirmek için 'FontFamily.IsStyleAvailable' kullanma

1) - böylece ve bazı mevcut Yazı Tipleri üzerine atlamak. 2)

Mutlu

:) çalışıyor ... en iyi görünüyor VE size eşit yükseklikte sütunlar alacak her yazı tipi için boyutunu ayarlamak için Graphics.MeasureString ile küçük Çal Jens, Danimarka.