Drawing a line using Graphics class

  • Thread starter Thread starter Coralin Feierbach
  • Start date Start date
C

Coralin Feierbach

I created a Windows Form Project using VS.net.
I'm trying to draw a line between two points.
(Plotting a graph, dynamically)
This is the code I have so far:
What do I have to do to actually see the line painted on
my form?


private: System::Void button1_Click(System::Object *
sender, System::EventArgs * e)
{
Graphics* g;
Pen* myPen = new Pen(Color::Red);
myPen->Width = 5;
g->DrawLine(myPen, 1, 1, 45, 65);
}
 
You are going to need a pointer to the graphics context of your form.
Typically you might want to use something like the Paint event, it gets
called any time your form needs to be redrawn for any reason - you could
call the Paint eventHandler from the button click function.

void frmPaint(Object * sender, System::Windows::Forms::PaintEventArgs * e)

{

Graphics * grfx = e->Graphics;

grfx->DrawLine(myPen, 1, 1, 45, 65);

}



Add something like this to your form constructor to tie the forms Paint
event to the frmPaint function.

this->add_Paint(new PaintEventHandler(this, frmPaint));



Adios,

January
 
private: System::Void button1_Click(System::Object *
sender, System::EventArgs * e)
{
Graphics* g;
Pen* myPen = new Pen(Color::Red);
myPen->Width = 5;
g->DrawLine(myPen, 1, 1, 45, 65);
}

in your example, replace with :
//
Graphics* g = this->CreateGraphics() ;
//
 
Back
Top