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


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

Пример[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();

}

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

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

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

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

 

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

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


How to: Draw a Line on a Form

This example demonstrates how to draw a line on a form.

Example

System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.DrawLine(myPen, 0, 0, 200, 200); 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.


Рисование линии в форме

В этом примере показано, как нарисовать линию в форме.

Пример[24]

System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.DrawLine(myPen, 0, 0, 200, 200); myPen.Dispose(); formGraphics.Dispose();

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

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

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

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

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

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


How to: Draw a Filled Rectangle on a Form

This example demonstrates how to draw a filled rectangle on a form.

Example

System.Drawing.SolidBrush brush1 = new System.Drawing.SolidBrush(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.FillRectangle(brush1, new System.Drawing.Rectangle(0,0,200,300)); brush1.Dispose(); formGraphics.Dispose();

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




Дата добавления: 2022-05-27; просмотров: 119;


Поиск по сайту:

Воспользовавшись поиском можно найти нужную информацию на сайте.

Поделитесь с друзьями:

Считаете данную информацию полезной, тогда расскажите друзьям в соц. сетях.
Poznayka.org - Познайка.Орг - 2016-2024 год. Материал предоставляется для ознакомительных и учебных целей.
Генерация страницы за: 0.007 сек.