The Grapgics object resembles a canvas or a piece of paper for other objects to draw on. The way to draw a straight line with the Grapgics class is as follows:
Graphics G;
Pen Newpen = new Pen (color.yellow);//define a brush, yellow
Dot STARTP = new Point (20,20);//Line start coordinate
Points ENDP = new Point (100,100),//Line endpoint coordinates
Line line;
line = new Line (STARTP, ENDP);
g = This.pictureBox1.CreateGraphics ();
G.drawline (Newpen, Line.startpoint, line.endpoint);//Draw Straight line
This line is drawn, in order to be able to drag the end point of the line, first when the mouse online termination point, the color of the transform line (the following method is in the MouseMove event):
Get current mouse position
Point P = new Point ();
p.x = e.x;
P.Y = e.y;
Rectangle rect = new Rectangle (line.endpoint.x-3, line.endpoint.y-3, 6, 6);//define an area that changes the color of the line when the mouse is in the area
if (p.x >= rect. X && p.x <= rect. Right && p.y >= rect. Top && p.y <= rect. Bottom)
{
When the mouse is near the end of the line, it turns blue.
Newpen.color = Color.Blue;
G.drawline (Newpen, Line.startpoint, Line.endpoint);
}
To drag a line:
Red when moving a line
This.pictureBox1.Refresh ()//Refresh interface, otherwise the entire interface is full of lines
Newpen.color = color.red;
Line.endpoint = p;
G.drawline (Newpen, Line.startpoint, Line.endpoint);
The above is just a simple example, and then have time to learn more about graphics other content.