Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_Graphics.doc
Скачиваний:
22
Добавлен:
16.11.2019
Размер:
3 Мб
Скачать
☆

Перебор установленных шрифтов

Класс InstalledFontCollection наследуется из абстрактного базового класса FontCollection. Объект InstalledFontCollection служит для хранения перечисления шрифтов, установленных на компьютере. Свойство Families объекта InstalledFontCollection является массивом объектов FontFamily.

Пример

В следующем примере выводится список названий всех семейств шрифтов, установленных на компьютере. Приведенный код извлекает свойство Name каждого объекта FontFamily, содержащегося в массиве, возвращаемом свойством Families. По мере извлечения имена семейств шрифтов объединяются в список имен, разделенных запятыми. Затем метод DrawString класса Graphics рисует список имен, разделенных запятыми, в прямоугольнике.

Результат выполнения этого кода будет аналогичен приведенному на следующем рисунке.

-----------------

FontFamily fontFamily = new FontFamily("Arial");

Font font = new Font(

fontFamily,

8,

FontStyle.Regular,

GraphicsUnit.Point);

RectangleF rectF = new RectangleF(10, 10, 500, 500);

SolidBrush solidBrush = new SolidBrush(Color.Black);

string familyName;

string familyList = "";

FontFamily[] fontFamilies;

InstalledFontCollection installedFontCollection = new InstalledFontCollection();

// Get the array of FontFamily objects.

fontFamilies = installedFontCollection.Families;

// The loop below creates a large string that is a comma-separated

// list of all font family names.

int count = fontFamilies.Length;

for (int j = 0; j < count; ++j)

{

familyName = fontFamilies[j].Name;

familyList = familyList + familyName;

familyList = familyList + ", ";

}

// Draw the large string (list of all families) in a rectangle.

e.Graphics.DrawString(familyList, font, solidBrush, rectF);

Compiling the Code

The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of PaintEventHandler.

----------

Компиляция кода

Предыдущий пример предназначен для работы с Windows Forms, для него необходим объект PaintEventArgs e, передаваемый в качестве параметра обработчику событий PaintEventHandler.

How to: Obtain Font Metrics

The FontFamily class provides the following methods that retrieve various metrics for a particular family/style combination:

  • GetEmHeight(FontStyle)

  • GetCellAscent(FontStyle)

  • GetCellDescent(FontStyle)

  • GetLineSpacing(FontStyle)

The numbers returned by these methods are in font design units, so they are independent of the size and units of a particular Font object.

The following illustration shows the various metrics.