[RESOLVED] [2.0] Drawing inside winforms
i'm working on winforms right now.
and i'm a little curious, whether i can draw a line, or maybe a shape in my forms by using tool like paintbrush or something.
i've done some search, and all i can see from msdn is that : all are drawn programmatically.
does .net doesn't support other tool ?
Re: [2.0] Drawing inside winforms
The System.Drawing namespace can be used for draing lines, arc's painting etc. The .net framework doesn't provide any components that allow you to drag on a line, but there may be some 3rd party components.
I usually take care of any drawing requirements I have (mostly horozontal lines) by using a group box with no text.
Regards,
Darren
Re: [2.0] Drawing inside winforms
If you want to draw on a control you should do so in its Paint event handler. You set up control variables at the class level and then tell the control to repaint itself. In the Paint event handler you read those control variables and use them to draw on the control. The reason you use the Paint event handler is that every time the control is repainted any existing drawing is lost, so you need to redraw every time the control is painted. Here's some code that will draw a line in a random position on a form each time you click a button:
Code:
private Point start = Point.Empty;
private Point end = Point.Empty;
private Random rand = new Random();
private void button1_Click(object sender, EventArgs e)
{
// Select random points for the line to start and end at.
this.start = new Point(this.rand.Next(0, this.ClientSize.Width),
this.rand.Next(0, this.ClientSize.Height));
this.end = new Point(this.rand.Next(0, this.ClientSize.Width),
this.rand.Next(0, this.ClientSize.Height));
// Tell the form to repaint.
this.Refresh();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
// Draw the line at the current coordinates.
e.Graphics.DrawLine(Pens.Black, this.start, this.end);
}
Re: [2.0] Drawing inside winforms