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

Надежное программирование

Исключение может возникнуть при следующих условиях.

  • Имя пути имеет слишком большую длину.

Drawing Text and Graphics

This topic is designed to help you find code that demonstrates how to perform common graphics programming tasks by using Visual C# Express Edition.

How to: Draw Text on a Form

This example demonstrates how to draw text on a form.

Example

private void DrawString()

{

System.Drawing.Graphics formGraphics = this.CreateGraphics();

string drawString = "Sample Text";

System.Drawing.Font drawFont = new System.Drawing.Font(

"Arial", 16);

System.Drawing.SolidBrush drawBrush = new

System.Drawing.SolidBrush(System.Drawing.Color.Black);

float x = 150.0f;

float y = 50.0f;

formGraphics.DrawString(drawString, drawFont, drawBrush, x, y);

drawFont.Dispose();

drawBrush.Dispose();

formGraphics.Dispose();

}

Compiling the Code

This example requires:

  • A Windows Forms Application project.

  • Call the DrawString() method from an event handler. For example, you can add a Button to the form, and call DrawString from the click event handler for the button.

Robust Programming

You should always call Dispose on any objects that consume system resources, such as Font and Graphics objects.

The following condition may cause an exception:

  • The Arial font is not installed.

Рисование текста и графики

Этот раздел предназначен для помощи в поиске кодов, демонстрирующих способы выполнения общих задач по графическому программированию с использованием Visual C#, экспресс-выпуск.

Отрисовка текста в форме

В этом примере демонстрируется отрисовка текста в форме.

Пример

private void DrawString()

{

System.Drawing.Graphics formGraphics = this.CreateGraphics();

string drawString = "Sample Text";

System.Drawing.Font drawFont = new System.Drawing.Font(

"Arial", 16);

System.Drawing.SolidBrush drawBrush = new

System.Drawing.SolidBrush(System.Drawing.Color.Black);

float x = 150.0f;

float y = 50.0f;

formGraphics.DrawString(drawString, drawFont, drawBrush, x, y);

drawFont.Dispose();

drawBrush.Dispose();

formGraphics.Dispose();

}

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

Для этого примера необходимы следующие компоненты.

  • Проект приложения Windows Forms.

  • Вызовите метод DrawString() из обработчика событий. Например, в форму можно добавить Button и вызвать DrawString из обработчика событий "Click" для кнопки.

Надежное программирование

Для любого объекта, потребляющего системные ресурсы (например, для объектов Font и Graphics), всегда нужно вызывать метод Dispose.

Следующее условие может вызвать исключение.

  • В системе не установлен шрифт Arial.

How to: Change the Color of Text on a Windows Forms Control

This example demonstrates how to change the color of text that is displayed on a Label control.

Example

label1.ForeColor=System.Drawing.Color.Pink;

Compiling the Code

This example requires:

  • A Windows Form Application project.

  • A Label named label1 on the form.

Изменение цвета текста в элементе управления Windows Forms

В этом примере показано изменение цвета текста, отображаемого в элементе управления Label.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]