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

Установка позиций табуляции для выводимого текста

Для текста можно устанавливать позиции табуляции. Для этого необходимо вызвать метод SetTabStops объекта StringFormat, а затем передать этот объект StringFormat методу DrawString класса Graphics.

Примечание.

Класс System.Windows.Forms..::.TextRenderer не поддерживает добавление позиций табуляции к выведенному тексту, хотя и возможно расширение существующих позиций табуляции с помощью флага TextFormatFlags..::.ExpandTabs.

Пример

В следующем примере позиции табуляции устанавливаются на 150, 250 и 350. Затем код выводит табулированный список имен и результатов теста.

Табулированный текст показан на следующем рисунке.

Приведенный ниже код передает методу SetTabStops два параметра. Вторым параметром является массив, содержащий смещения позиций табуляции. В качестве первого параметра методу SetTabStops передается 0; это означает, что первое смещение в массиве отсчитывается от позиции 0, т. е. от левого края ограничивающего прямоугольника.

string text = "Name\tTest 1\tTest 2\tTest 3\n";

text = text + "Joe\t95\t88\t91\n";

text = text + "Mary\t98\t84\t90\n";

text = text + "Sam\t42\t76\t98\n";

text = text + "Jane\t65\t73\t92\n";

FontFamily fontFamily = new FontFamily("Courier New");

Font font = new Font(

fontFamily,

12,

FontStyle.Regular,

GraphicsUnit.Point);

Rectangle rect = new Rectangle(10, 10, 450, 100);

StringFormat stringFormat = new StringFormat();

SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));

float[] tabs = { 150, 100, 100, 100 };

stringFormat.SetTabStops(0, tabs);

e.Graphics.DrawString(text, font, solidBrush, rect, stringFormat);

Pen pen = Pens.Black;

e.Graphics.DrawRectangle(pen, rect);

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: Enumerate Installed Fonts

The InstalledFontCollection class inherits from the FontCollection abstract base class. You can use an InstalledFontCollection object to enumerate the fonts installed on the computer. The Families property of an InstalledFontCollection object is an array of FontFamily objects.

Example

The following example lists the names of all the font families installed on the computer. The code retrieves the Name property of each FontFamily object in the array returned by the Families property. As the family names are retrieved, they are concatenated to form a comma-separated list. Then the DrawString method of the Graphics class draws the comma-separated list in a rectangle.

If you run the example code, the output will be similar to that shown in the following illustration.