I've been working on a project for class where I need to display on screen polygons (drawed in a Panel), but I've been reading arround here that I should work with Paint event, tho I can't make it work (started learning C# a little ago).
private void drawView()
{
//"playerView" is the panel I'm working on
Graphics gr = playerView.CreateGraphics();
Pen pen = new Pen(Color.White, 1);
//Left Wall 1
Point lw1a = new Point(18, 7);
Point lw1b = new Point(99, 61);
Point lw1c = new Point(99, 259);
Point lw1d = new Point(18, 313);
Point[] lw1 = { lw1a, lw1b, lw1c, lw1d };
gr.DrawPolygon(pen, lw1);
}
I was doing something like this to draw it on screen, it would be possible to do this with a Paint method? (it is called method, or event? I'm really lost here).
Thanks!
I think you are referring to the Control.Paint
event from windows forms.
Basically, you would attach a listener to the Paint event of a windows forms element, like this :
//this should happen only once! put it in another handler, attached to the load event of your form, or find a different solution
//as long as you make sure that playerView is instantiated before trying to attach the handler,
//and that you only attach it once.
playerView.Paint += new System.Windows.Forms.PaintEventHandler(this.playerView_Paint);
private void playerView_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Create a local version of the graphics object for the playerView.
Graphics g = e.Graphics;
//you can now draw using g
//Left Wall 1
Point lw1a = new Point(18, 7);
Point lw1b = new Point(99, 61);
Point lw1c = new Point(99, 259);
Point lw1d = new Point(18, 313);
Point[] lw1 = { lw1a, lw1b, lw1c, lw1d };
//we need to dispose this pen when we're done with it.
//a handy way to do that is with a "using" clause
using(Pen pen = new Pen(Color.White, 1))
{
g.DrawPolygon(pen, lw1);
}
}