VCL App in C++ Builder 和 FormCreate 事件更好的理解

huangapple go评论79阅读模式
英文:

VCL App in C++ Builder and FormCreate Event Better Understanding

问题

作为一个对VCL应用程序新手,我的基本理解是,任何包含在FormCreate函数中的代码都将在生成和显示Form1时执行。

所以,例如,我调用一个函数来加载两个组合框,然后调用另一个函数来显示一些线条和矩形:

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    LoadCombos();
    DrawShapes();
}

当运行代码时,我只得到了加载的组合框。

我已经在调试模式下检查了代码,它确实执行了DrawShapes()函数。下面是一段绘图代码的摘录:

void __fastcall TForm1::DrawShapes(void)
{
    int x, x1, y1, x2, y2;

    // 在两行中绘制主要外框
    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);
    }
}

我绝对不明白为什么绘图代码没有显示出来。我假设VCL代码会在途中发送无效和更新事件消息。我甚至尝试在FormCreate()函数的开头和结尾手动调用它们,但没有成功。

非常感谢帮助我理解为什么会发生这种情况。

英文:

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&lt;4; x++) {
	    Canvas-&gt;MoveTo(x1+(x*275),y1);
	    Canvas-&gt;Rectangle(x1+(x*275),y1,x2+(x*275),y2);
   }
   x1=50;
   y1=440;
   x2=275;
   y2=680;
   for (x=0; x&lt;4; x++) {
	   Canvas-&gt;MoveTo(x1+(x*275),y1);
	   Canvas-&gt;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.

答案1

得分: 2

以下是翻译好的部分:

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.
作为一个对VCL应用程序新手,我基本了解到,包含在FormCreate函数中的任何代码将在生成和显示Form1时执行。

The OnCreate event is fired while the Form object is being constructed. This happens before the Form's UI window is shown onscreen.
OnCreate事件在构建Form对象时触发。这发生在Form的UI窗口显示在屏幕上之前。

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.
这是因为您正在在Form的正常绘制顺序之外绘制在Form的Canvas上。每当Form的窗口需要在屏幕上被(重新)绘制时,任何先前的绘制都会被擦除。所以,不要在Form的OnCreate事件中调用您的DrawShapes()函数,而是在Form的OnPaint事件中调用它。

英文:

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

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.

huangapple
  • 本文由 发表于 2023年6月9日 02:29:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定