Drawing a line using Graphics class

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);
}
 
J

January Smith

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
 
R

Red Orchid

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() ;
//
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top