Monday 2 January 2012

graphics draw line and ractange and c# or vb

' VB
' Create a graphics object from the form
Dim g As Graphics = Me.CreateGraphics
' Create a pen object with which to draw
Dim p As Pen = New Pen(Color.Red, 7)
' Draw the line
g.DrawLine(p, 1, 1, 100, 100)
// C#
// Create a graphics object from the form
Graphics g = this.CreateGraphics();
// Create a pen object with which to draw
Pen p = new Pen(Color.Red, 7);
// Draw the line
g.DrawLine(p, 1, 1, 100, 100);
Lesson 1: Drawing Graphics 229
Figure 6-1 Use Graphics.DrawLine to create straight lines
Similarly, the following code draws a blue pie shape with a 60-degree angle, as shown
in Figure 6-2:
' VB
Dim g As Graphics = Me.CreateGraphics
Dim p As Pen = New Pen(Color.Blue, 3)
g.DrawPie(p, 1, 1, 100, 100, -30, 60)
// C#
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Blue, 3);
g.DrawPie(p, 1, 1, 100, 100, -30, 60);
Figure 6-2 Use Graphics.DrawPie to create pie shapes
230 Chapter 6 Graphics
The Graphics.DrawLines, Graphics.DrawPolygon, and Graphics.DrawRectangles methods
accept arrays as parameters to allow you to create more complex shapes. For example,
the following code draws a purple, five-sided polygon, as shown in Figure 6-3:
' VB
Dim g As Graphics = Me.CreateGraphics
Dim p As Pen = New Pen(Color.MediumPurple, 2)
' Create an array of points
Dim points As Point() = New Point() {New Point(10, 10), _
New Point(10, 100), _
New Point(50, 65), _
New Point(100, 100), _
New Point(85, 40)}
' Draw a shape defined by the array of points
g.DrawPolygon(p, points)
// C#
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.MediumPurple, 2);
// Create an array of points
Point[] points = new Point[]
{new Point(10, 10),
new Point(10, 100),
new Point(50, 65),
new Point(100, 100),
new Point(85, 40)};
// Draw a shape defined by the array of points
g.DrawPolygon(p, points);

No comments:

Post a Comment