C#graphics class draw line and drag

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.