سؤال

لدي مشروع VB.NET Windows Forms الذي يقوم في وقت ما برسم النص مباشرةً على النموذج في وقت التشغيل.قبل أن أرسم بالخط، أريد التأكد من وجود الخط وحجم الخط على جهاز المستخدم.إذا لم يفعلوا ذلك، فسأحاول استخدام بعض الخطوط الأخرى المشابهة، وفي النهاية سأختار الخطوط الافتراضية باستخدام Arial أو شيء من هذا القبيل.

ما هي أفضل طريقة لاختبار الخط والتحقق من صحته على كمبيوتر المستخدم؟

هل كانت مفيدة؟

المحلول

من مقالة MSDN بعنوان "كيفية:تعداد الخطوط المثبتة"، لقد وجدت هذا الرمز:



InstalledFontCollection installedFontCollection = new InstalledFontCollection();

// Get the array of FontFamily objects.
FontFamily[] fontFamilies = installedFontCollection.Families;


نصائح أخرى

إليك حل واحد في C#:

public partial class Form1 : Form
{
    public Form1()
    {
        SetFontFinal();
        InitializeComponent();
    }

    /// <summary>
    /// This method attempts to set the font in the form to Cambria, which
    /// will only work in some scenarios. If Cambria is not available, it will
    /// fall back to Times New Roman, so the font is good on almost all systems.
    /// </summary>
    private void SetFontFinal()
    {
        string fontName = "Cambria";
        Font testFont = new Font(fontName, 16.0f, FontStyle.Regular,
            GraphicsUnit.Pixel);

        if (testFont.Name == fontName)
        {
            // The font exists, so use it.
            this.Font = testFont;
        }
        else
        {
            // The font we tested doesn't exist, so fallback to Times.
            this.Font = new Font("Times New Roman", 16.0f,
                FontStyle.Regular, GraphicsUnit.Pixel);
        }
    }
}

وهنا طريقة واحدة في VB:

Public Function FontExists(FontName As String) As Boolean

    Dim oFont As New StdFont
    Dim bAns As Boolean

    oFont.Name = FontName
    bAns = StrComp(FontName, oFont.Name, vbTextCompare) = 0
    FontExists = bAns

End Function

أنظر هذا أيضا نفس السؤال وينتج عن هذا الكود:

    private bool IsFontInstalled(string fontName) {
        using (var testFont = new Font(fontName, 8)) {
            return 0 == string.Compare(
              fontName,
              testFont.Name,
              StringComparison.InvariantCultureIgnoreCase);
        }
    }

من غير المرجح أن يكون Arial Bold Italic خطًا.إنها فئة فرعية من عائلة أريال.

حاول إبقاء الأمر بسيطًا واختبر "Arial".

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top