Discussion:
Canvas experiments.
(too old to reply)
Penny hapeny
2008-03-20 14:44:46 UTC
Permalink
Hi All,

I hope some of you can help me.
I am experimenting with the canvas runtime Properties.
I have a Main Form with two buttons on it. In the code for the Button1Click
I have a few Canvas calls IE

Canvas->MoveTo(5,5);
Canvas->LineTo(20,80);
Canvas->MoveTo(100,400);
Canvas->LineTo(100,600);

Canvas->Pen->Color = clRed;

Canvas->MoveTo(200,100);
Canvas->Ellipse(70,80,200,400);

All Ok. The second button,
Button2Click opens a second Form as follows

SecondForm->Show();
MainForm->Refresh();
MainForm->Update();

The second Form has a button on it and Code in its Unit.cpp file as follows

ReturnMainFormClick(TObject *Sender)
{
ReturnMainForm;
Close();
}

This returns to the first form and closes the second.

The first form is then displayed with the buttons Ok,
but not with the lines and Ellipse that were generated by the first button,
and before transfering to the Second Form.
If one clicks the button1 again then they are shown.
But what I want is to be able to return to the FirstForm with its
drawings intact as they were when I left it.
The refresh() and Update() don't seem to have any effect, I added them
more in hope rather than being sure what I was doing.

Thanks in advance
Penny.
Vladimir Stefanovic
2008-03-20 15:53:53 UTC
Permalink
Post by Penny hapeny
But what I want is to be able to return to the FirstForm
with its drawings intact as they were when I left it.
In short, drawing lines over Canvas is not permanent (effect).

You'll have to move all drawing code in TForm::OnPaint()
event. Then every overlap, repaint, update will trigger OnPaint()
and so the Canvas will be uopdated with your lines again.
--
Best Regards,
Vladimi Stefanovic
Bruce Salzman
2008-03-20 15:51:14 UTC
Permalink
Post by Penny hapeny
The first form is then displayed with the buttons Ok,
but not with the lines and Ellipse that were generated by the first button,
and before transfering to the Second Form.
If one clicks the button1 again then they are shown.
But what I want is to be able to return to the FirstForm with its
drawings intact as they were when I left it.
Do any drawing on the Canvas in the Form's OnPaint event handler.
Post by Penny hapeny
The refresh() and Update() don't seem to have any effect, I added them
more in hope rather than being sure what I was doing.
Those method just cause the form to be repainted.
--
Bruce
Remy Lebeau (TeamB)
2008-03-20 16:48:25 UTC
Permalink
Post by Penny hapeny
The first form is then displayed with the buttons Ok,
but not with the lines and Ellipse that were generated
by the first button
Because you explicitally told the first form to redraw itself, but did not
re-draw your lines afterwards.
Post by Penny hapeny
But what I want is to be able to return to the FirstForm
with its drawings intact as they were when I left it.
You have to re-draw your lines every time the form is re-drawn for any
reason. You need to move your drawing code into the form's OnPaint event
for that.


Gambit
Penny hapeny
2008-03-21 12:28:49 UTC
Permalink
Many thanks to you all,
I have associated the OnPaint event with the Button1 event,
this was the only way I could find to do it. This has worked fine
and when I return to my original Form the drawings are as they were.

Now I have another question. After I have returned to the original Form
with my previous drawings displayed, (as above) I now want to add
some more drawings and then go back to the second Form or even to a
Third form, then when I have finished on this second or third Form I
want to return to the First Form but this time for it to display the
Original drawings and the additionally added drawings, but only on
this second return. I hope I have made myself clear.

Thanks
Penny.
Post by Remy Lebeau (TeamB)
Post by Penny hapeny
The first form is then displayed with the buttons Ok,
but not with the lines and Ellipse that were generated
by the first button
Because you explicitally told the first form to redraw itself, but did not
re-draw your lines afterwards.
Post by Penny hapeny
But what I want is to be able to return to the FirstForm
with its drawings intact as they were when I left it.
You have to re-draw your lines every time the form is re-drawn for any
reason. You need to move your drawing code into the form's OnPaint event
for that.
Gambit
Vladimir Stefanovic
2008-03-21 15:05:36 UTC
Permalink
I now want to add some more drawings and then go
Then you have to redesign your code.

For example, you could implement an array of TPoint
and Counter of objects to draw.

/pseudo/
OnPaint()
{
if ( Counter > 0 )
{
Canvas->MoveTo( pt[0].x, pt[0].y );

for ( int i=1; i<Counter; i++ )
Canvas->LineTo( pt[i].x, pt[i].y );
}
}
--
Best Regards,
Vladimi Stefanovic
Remy Lebeau (TeamB)
2008-03-21 16:22:28 UTC
Permalink
Post by Penny hapeny
I have associated the OnPaint event with the Button1 event
I would not recommend that. You should move the drawing code directly into
the OnPaint's own event handler, ie:

void __fastcall TForm1::FormPaint(TObject *Sender)
{
Canvas->MoveTo(5,5);
Canvas->LineTo(20,80);
Canvas->MoveTo(100,400);
Canvas->LineTo(100,600);
Canvas->Pen->Color = clRed;
Canvas->MoveTo(200,100);
Canvas->Ellipse(70,80,200,400);
}
Post by Penny hapeny
this was the only way I could find to do it.
Why did you not simply double-click on the TForm's OnPaint event in the
Object Inspector instead?
Post by Penny hapeny
After I have returned to the original Form with my previous drawings
displayed, (as above) I now want to add some more drawings and
then go back to the second Form or even to a Third form, then when
I have finished on this second or third Form I want to return to the
First Form but this time for it to display the Original drawings and
the additionally added drawings, but only on this second return. I
hope I have made myself clear.
I suggest you do all of your drawing onto TBitmap objects instead, and then
have the OnPaint event handler draw the appropriate TBitmap onto the Canvas
when needed.


Gambit
Penny hapeny
2008-03-22 16:02:36 UTC
Permalink
Many thanks Gambit & Vladmir,

When I had looked at the OnPaint in the Forms events and double clicked
nothing happened, but it seems I didn't do it quickly enough.

Thanks for your help and advice.
Now I have another problem but think I had better start a new post

Penny.
Post by Remy Lebeau (TeamB)
Post by Penny hapeny
I have associated the OnPaint event with the Button1 event
I would not recommend that. You should move the drawing code directly
void __fastcall TForm1::FormPaint(TObject *Sender)
{
Canvas->MoveTo(5,5);
Canvas->LineTo(20,80);
Canvas->MoveTo(100,400);
Canvas->LineTo(100,600);
Canvas->Pen->Color = clRed;
Canvas->MoveTo(200,100);
Canvas->Ellipse(70,80,200,400);
}
Post by Penny hapeny
this was the only way I could find to do it.
Why did you not simply double-click on the TForm's OnPaint event in the
Object Inspector instead?
Post by Penny hapeny
After I have returned to the original Form with my previous drawings
displayed, (as above) I now want to add some more drawings and
then go back to the second Form or even to a Third form, then when
I have finished on this second or third Form I want to return to the
First Form but this time for it to display the Original drawings and
the additionally added drawings, but only on this second return. I
hope I have made myself clear.
I suggest you do all of your drawing onto TBitmap objects instead, and
then have the OnPaint event handler draw the appropriate TBitmap onto the
Canvas when needed.
Gambit
Continue reading on narkive:
Loading...