[英] Resize won't execute untill I manually call ClientHeigh or until I manually resize it

查看:195
本文介绍了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个派生自TPanel的自定义控件,它包含一个图像和一堆其他控件。
编写代码后,我的程序中有一些奇怪的行为。我意识到在TDisplay.Resize(override)中应该初始化的一些变量从未被初始化,因为Resize从未被执行过。

I want to create a custom control derived from TPanel that contains an image and a bunch of other controls on it. After writing the code I have some weird behavior in my program. I realized that some vars that were supposed to be initialized in TDisplay.Resize (override) was never initialized because the Resize was never executed.

为了解决这个问题,我在表单上放了一个按钮,并调用了LoadSample函数调用ClientHeight调用Resize FOR THE FIRST TIME!

To 'solve it' I put a button on a form and called the LoadSample function which calls ClientHeight which calls Resize FOR THE FIRST TIME!

constructor TDisplay.Create(AOwner: TComponent);
begin
 inherited Create(AOwner);
 Ready         := FALSE;                                                       
 Parent        := Owner as TWinControl;
 Width         := 200;
 Height        := 86;                 
 Color         := clSilver;
 Caption       := '';
 DoubleBuffered:= TRUE;


  InternalDisplay:= TImage32.Create(Self);
  with Display DO
   begin
    Parent        := Self;
    Bitmap.Width  := 1;                                                        
    Bitmap.Height := 1;
    RepaintMode   := rmOptimizer;     
    Align         := alClient;
    SetupBitmap(TRUE, clBlack32);    
    Visible       := TRUE;
    OnMouseDown   := DMouseDown;
   end;
 ...
end;

更新:

此外InternalDisplay赢得' t与其父大小对齐,直到我在运行时手动调整窗体(控件)的大小。只有这样,它才会采取行动(保持与alClient保持一致)。

Update:
Also the InternalDisplay won't be aligned to its parent size until I manually resize the form (the control) at runtime. Only then it will act as it was supposed to act (to stay aligned to alClient).

更新2:

调整大小是这样声明的:procedure Resize;覆盖;

Update 2:
Resize is declared like that: procedure Resize; override;

更新3:

我从我的修改器中删除了ClientHeight行,并将其移动到这里:

Update 3:
I removed the ClientHeight line from my construnctor and move it here:

procedure TDisplay.LoadSample(VAR Obj: TMySample; CONST bReleaseOnExit: boolean)
begin
 ClientHeight;  <--------- this will call Resize for the first time and my code will be finally initialized. But until this point my display will look odd because the code was never initialized. So the user will see weird stuff until it pushes the 'LoadSample' button.
 more code here....
end;

更新4:

我用HandleNeeded为David建议并解决了初始化问题。但是,除非我手动调整表单/控件大小,否则图像仍然不会与整个客户端对齐。

Update 4:
I used HandleNeeded as David suggested and it solved the initialization problem. However, the Image still won't align to the entire client area unless I manually resize the form/control.

更新5

如David所建议,继续这里: TImage不会与父级对齐

推荐答案

您的表单尚未显示,因此尚无法接收Windows消息(例如调整大小的消息触发OnResize事件)。

Your form isn't showing yet, so it isn't yet able to receive Windows messages (such as the resize message that triggers the OnResize event).

这篇关于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆