Board index » cppbuilder » Canvas experiments.

Canvas experiments.


2008-03-20 10:44:46 PM
cppbuilder22
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.
 
 

Re:Canvas experiments.

"Penny hapeny" < XXXX@XXXXX.COM >wrote in message
Quote
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.
Quote
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
 

Re:Canvas experiments.

Quote
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
 

{smallsort}

Re:Canvas experiments.

"Penny hapeny" < XXXX@XXXXX.COM >wrote in message
Quote
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.
Quote
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
 

Re:Canvas experiments.

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.
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Penny hapeny" < XXXX@XXXXX.COM >wrote in message
news:47e2785f$ XXXX@XXXXX.COM ...

>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.

>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

 

Re:Canvas experiments.

Quote
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
 

Re:Canvas experiments.

"Penny hapeny" < XXXX@XXXXX.COM >wrote in message
Quote
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);
}
Quote
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?
Quote
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
 

Re:Canvas experiments.

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.
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Penny hapeny" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

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

>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?

>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