Discussion:
Forms Canvas
(too old to reply)
bar
2007-06-26 15:14:13 UTC
Permalink
Hello all
I am using BCB6.

In the form paints event i have

TPicture *g = new TPicture();
g->LoadFromFile(strG);

for(int y = 0; y < (frmMain->Height); y += g->Height)
{
for(int x = 0; x < (frmMain->Width); x += g->Width)
{
frmMain->Canvas->Draw(x, y, g->Graphic);
}
}

So i am tiling a graphic on the form. Everything is fine.
I am moving a panel on this form in a timer with the interval 25 milli
seconds, so there the form paint event occurs frequently.
The panel is scrolling and the form is repainting but after 1 or 2 minutes i
get an exception
"Canvas does not allow for drawin".
I searched and find that the Cavas handle should have device context, but i
am not creating any canvas dynamically, i am using the forms canvas.
How to resolve the problem
Thanks for the suggestions
SA
Remy Lebeau (TeamB)
2007-06-26 16:53:21 UTC
Permalink
Post by bar
TPicture *g = new TPicture();
g->LoadFromFile(strG);
It would be better to load the image only once when the form is
initially created, such as in the constructor, and then reuse it for
each event. Do not re-load the image every time the event occurs.
Painting happens very often, hundreds of times a second, so you should
minimize the overhead as much as possible.
Post by bar
for(int y = 0; y < (frmMain->Height); y += g->Height)
{
for(int x = 0; x < (frmMain->Width); x += g->Width)
{
frmMain->Canvas->Draw(x, y, g->Graphic);
}
}
Do not use the form's global pointer when you are inside the form's
own code. Use the 'this' pointer instead, ie:

for(int y = 0; y < Height; y += g->Height)
{
for(int x = 0; x < Width; x += g->Width)
{
Canvas->Draw(x, y, g->Graphic);
}
}
Post by bar
after 1 or 2 minutes i get an exception
"Canvas does not allow for drawin".
You are trying to draw on the Canvas when it is not ready for drawing.
Please show a more complete snippet of what you are really doing.


Gambit
bar
2007-06-27 16:54:46 UTC
Permalink
Thanks Remy your suggestion solved the issue.
SA

Loading...