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

Выравнивание рисуемого текста

При рисовании элементов может потребоваться выровнять текст по центру формы или элемента управления. Чтобы выровнять текст, нарисованный с помощью метода DrawString или DrawText, необходимо создать нужный объект форматирования и установить соответствующие параметры форматирования.

Рисование центрированного текста с использованием GDI+ (DrawString)

  • Чтобы указать текст, требующий центрирования, используйте объект StringFormat, передаваемый методу DrawString.

-----

Рисование центрированного текста с использованием gdi (DrawText)

  • Для переноса текста по строкам, а также для горизонтального и вертикального выравнивания текста по центру, используйте перечисление TextFormatFlags и соответствующий метод DrawText.

-------

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

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

How to: Create Vertical Text

You can use a StringFormat object to specify that text be drawn vertically rather than horizontally.

Example

The following example assigns the value DirectionVertical to the FormatFlags property of a StringFormat object. That StringFormat object is passed to the DrawString method of the Graphics class. The value DirectionVertical is a member of the StringFormatFlags enumeration.

The following illustration shows the vertical text.

string myText = "Vertical text";

FontFamily fontFamily = new FontFamily("Lucida Console");

Font font = new Font(

fontFamily,

14,

FontStyle.Regular,

GraphicsUnit.Point);

PointF pointF = new PointF(40, 10);

StringFormat stringFormat = new StringFormat();

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

stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;

e.Graphics.DrawString(myText, font, solidBrush, pointF, stringFormat);

Compiling the Code

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

Вывод текста по вертикали

Чтобы указать, что текст должен выводиться по вертикали, а не по горизонтали, можно использовать объект StringFormat.

Пример

В следующем примере значение DirectionVertical присваивается свойству FormatFlags объекта StringFormat. Этот объект StringFormat передается методу DrawString класса Graphics. Значение DirectionVertical является членом перечисления StringFormatFlags.

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

----

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

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

How to: Set Tab Stops in Drawn Text

You can set tab stops for text by calling the SetTabStops method of a StringFormat object and then passing that StringFormat object to the DrawString method of the Graphics class.

Note:

The System.Windows.Forms..::.TextRenderer does not support adding tab stops to drawn text, although you can expand existing tab stops using the TextFormatFlags..::.ExpandTabs flag.

Example

The following example sets tab stops at 150, 250, and 350. Then, the code displays a tabbed list of names and test scores.

The following illustration shows the tabbed text.

The following code passes two arguments to the SetTabStops method. The second argument is an array that contains tab offsets. The first argument passed to SetTabStops is 0, which indicates that the first offset in the array is measured from position 0, the left edge of the bounding rectangle.