As someone new to VCL apps, my baseline understanding is that any code included in the FormCreate function will be executed when Form1 is generated and displayed.
So, for example, I call a function that loads two combo boxes, and then another function to display some line and rectangles:
void __fastcall TForm1::FormCreate(TObject *Sender)
{
LoadCombos();
DrawShapes();
}
When the code is run, I only get the loaded combo boxes.
I’ve walked the code in debug mode and it is executing the DrawShapes() function. Below is a snippet of the drawing code:
void __fastcall TForm1::DrawShapes(void)
{
int x,x1,y1,x2,y2;
// draw main outline boxes in two rows
x1=50;
y1=140;
x2=275;
y2=380;
for (x=0; x<4; x++) {
Canvas->MoveTo(x1+(x*275),y1);
Canvas->Rectangle(x1+(x*275),y1,x2+(x*275),y2);
}
x1=50;
y1=440;
x2=275;
y2=680;
for (x=0; x<4; x++) {
Canvas->MoveTo(x1+(x*275),y1);
Canvas->Rectangle(x1+(x*275),y1,x2+(x*275),y2);
}
}
I’m definitely not understanding why the drawing code is not showing up. I am assuming the VCL code is send the invalidate and update event messages along the way. I’ve even tried calling them manually at the beginning and end of the FormCreate() function without success.
Any help in understanding why this is happening would be greatly appreciated.
>Solution :
As someone new to VCL apps, my baseline understanding is that any code included in the
FormCreatefunction will be executed whenForm1is generated and displayed.
The OnCreate event is fired while the Form object is being constructed. This happens before the Form’s UI window is shown onscreen.
When the code is run, I only get the loaded combo boxes.
That is because you are drawing on the Form’s Canvas from outside of the Form’s normal drawing sequence. Every time the Form’s window needs to be (re-)drawn onscreen, any previous drawing is erased. So, do not call your DrawShapes() function in the Form’s OnCreate event, call it in the Form’s OnPaint event instead.