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

Compiling the Code

This example requires:

  • A Windows Forms Application project, with a form named formGraphics.

The code must be in the scope of the Form class. The instance of the form is represented by this.

Robust Programming

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

Рисование кривой в форме

В этом примере представлено несколько разных способов изображения кривых в форме.

Пример

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

System.Drawing.Pen myPen;

myPen = new System.Drawing.Pen(System.Drawing.Color.Black);

// Draw head with an ellipse.

formGraphics.DrawEllipse(myPen, 0, 0, 200, 200);

// Draw winking eye with an arc.

formGraphics.DrawArc(myPen, 40, 40, 40, 40, 180, -180);

// Draw open eye with an ellipse.

formGraphics.DrawEllipse(myPen, 120, 40, 40, 40);

// Draw nose with a Bezier spline.

formGraphics.DrawBezier(myPen, 100, 60, 120, 100, 90, 120, 80, 100);

// Draw mouth with a canonical spline.

Point[] apt = new Point[4];

apt[0] = new Point(60, 140);

apt[1] = new Point(140, 140);

apt[2] = new Point(100, 180);

apt[3] = new Point(60, 140);

formGraphics.DrawCurve(myPen, apt, 0, 3, 0.9f);

myPen.Dispose();

formGraphics.Dispose();

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

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

  • Проект приложения Windows Forms с формой, имеющей имя formGraphics.

Код должен находиться в области действия класса Form. Экземпляр формы представлен this.

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

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

How to: Draw Outlined Shapes

This example demonstrates how to draw outlined ellipses and rectangles on a form.

Example

private void DrawEllipse()

{

System.Drawing.Pen myPen;

myPen = new System.Drawing.Pen(System.Drawing.Color.Red);

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

formGraphics.DrawEllipse(myPen, new Rectangle(0,0,200,300));

myPen.Dispose();

formGraphics.Dispose();

}

private void DrawRectangle()

{ System.Drawing.Pen myPen;

myPen = new System.Drawing.Pen(System.Drawing.Color.Red);

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

formGraphics.DrawRectangle(myPen, new Rectangle(0,0,200,300));

myPen.Dispose();

formGraphics.Dispose();

}

Compiling the Code

This example requires:

  • A Windows Forms Application project with a form named formGraphics.

The code must be within the scope of the Form class. The instance of the form is represented by this.

Robust Programming

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

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

В этом примере показано рисование контуров эллипсов и прямоугольников в форме.

Пример23

private void DrawEllipse()

{

System.Drawing.Pen myPen;

myPen = new System.Drawing.Pen(System.Drawing.Color.Red);

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

formGraphics.DrawEllipse(myPen, new Rectangle(0,0,200,300));

myPen.Dispose();

formGraphics.Dispose();

}

private void DrawRectangle()

{ System.Drawing.Pen myPen;

myPen = new System.Drawing.Pen(System.Drawing.Color.Red);

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

formGraphics.DrawRectangle(myPen, new Rectangle(0,0,200,300));

myPen.Dispose();

formGraphics.Dispose();

}

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