在form上动态创建多个panel后(),如何控制form在resize后动态创建的panel大小的实时改变(50分)

  • 主题发起人 ddzhouqian
  • 开始时间
D

ddzhouqian

Unregistered / Unconfirmed
GUEST, unregistred user!
如题:我在form1上动态创建了多个panel后,如何在对form1大小的改变(resize事件中),实时的改变panel的大小(动态创建的),创建的panel比较多,如何防止form1处于假死状态,并能够快速刷新。部分代码:
procedure TMainForm.FormResize(Sender: TObject);
var i,j:Integer;
v_count:Integer;
s_count:Integer;
begin
v_count:=FCount.x;
s_count:=FCount.y;
for i:=Low(FPanelList) to High(FPanelList)do
begin
for j:=Low(FPanelList) to High(FPanelList)do
begin
if FPanelList[i,j]<>nil then
begin
FPanelList[i,j].Width:=(panel4.Width) div v_count;
FPanelList[i,j].DefaultWith:=FPanelList[i,j].Width;
FPanelList[i,j].Height:=(panel4.Height) div s_count;
FPanelList[i,j].DefaultHeight:=FPanelList[i,j].Height;
FPanelList[i,j].Left:=panel4.Left+i*(FPanelList[i,j].Width);
FPanelList[i,j].DefaultLeft:=FPanelList[i,j].Left;
FPanelList[i,j].Top:=panel4.Top+j*(FPanelList[i,j].Height);
FPanelList[i,j].DefaultTop:=FPanelList[i,j].Top;
//
FPanelList[i,j].ChangeTopAndBottom_PanelSize();
end;
end;
end;
end;
 
设Panel的Anchor属性。
 
to Passion:我的panel大小是变化的,是随着窗体的大小的缩放而变化的,还有坐标也是跟着变化的,光设置Anchor好像是不行的
 
这个在根据窗体变化,分辨率等方面考虑
 
和分辨率没关系的,大小是根据你当前窗体中panel4的大小来改变的,panel4的align:=alclient.大概如下图
panel01 panle02 panle03
panel04 panel05 panle06
panle07 panel08 panle09
主窗体大小变化的时候这些panel01~panle09都要变化,高度,宽度,坐标,都要做相应的调整的
panel01 panel02 panel03
panel04 panel05 panel06
panel07 panel08 panel09
 
楼主的panel不是标准的TPanel吧.....ChangeTopAndBottom_PanelSize();是做什么用的?
如果是标准TPanel 有几个地方要注意:
1.你在计算的时候应该先计算好高和宽,否则每次循环都要计算一边效率很低
2.标准panel每次设置left或者top、width、height的时候都会刷新一遍,所以你分开设置一次循环就会产生4次刷新,不知道你用的那个控件会不会这样,应该让他一次就设置好
3.在开始循环时,首先应该控制让panel的parent停止刷新,全部设置完成后再统一刷新显示
所以你的代码这样改改看看会不会提高效率,我测试过30*30个标准panel,刷新速度在200毫秒以内:

procedure TMainForm.FormResize(Sender: TObject);
var i,j,w,h:Integer;
v_count:Integer;
s_count:Integer;
begin
v_count:=FCount.x;
s_count:=FCount.y;

{循环前先计算好}
w:=(panel4.Width) div v_count;
h:=(panel4.Height) div s_count;

panel4.Visible:=false;
{应该是FPanelList里panel的parent,看你的代码似乎是panel4,如果不是,改成parent}
try
for i:=Low(FPanelList) to High(FPanelList)do
begin
for j:=Low(FPanelList) to High(FPanelList)do
begin
if FPanelList[i,j]<>nil then
begin
FPanelList[i,j].SetBounds(panel4.Left+i*w,panel4.Top+j*h,w,h);
{下面的属性不是标准panel的属性,不知道是否必须设置}
{ FPanelList[i,j].DefaultWith:=FPanelList[i,j].Width;
FPanelList[i,j].DefaultHeight:=FPanelList[i,j].Height;
FPanelList[i,j].DefaultLeft:=FPanelList[i,j].Left;
FPanelList[i,j].DefaultTop:=FPanelList[i,j].Top;}
// FPanelList[i,j].ChangeTopAndBottom_PanelSize();{这个不知道做什么用,似乎是控制刷新的?}
end;
end;
end;
finally
panel4.Visible:=true;
{全部循环完成后统一刷新显示}
end;
end;
 
to:hs-kill先谢谢了
的确我的panel不是标准的panel,是我自己写的一个方便控制的panel,只是在原有的基础上加了些功能的。“FPanelList[i,j].ChangeTopAndBottom_PanelSize();{这个不知道做什么用,似乎是控制刷新的?}”这句是改变当前panel内的两个子panel的大小。
我先试试,多谢了,希望可以继续交流!
 
to hs-kill
速度确实有大幅度提升,呵呵
不过我还想看看有没有其他答案,呵呵
我在等等,希望大家踊跃参与哦!
 
顶部