Delphi 2009 - 在运行时创建TPanel并更改其颜色

时间:2010-09-23 12:05:45

标签: delphi delphi-2009

遇到了一个奇怪的问题:我在运行时创建了一个TPanele并改变了它的颜色 - 但是,颜色仍然是clBtnFace。

这里是代码:

procedure TForm1.Button1Click(Sender: TObject);
var
  pnlTest : TPanel;
begin
    pnlTest := TPanel.Create(Form1);
    pnlTest.Parent := Form1;
    pnlTest.Width := 100;
    pnlTest.Height := 100;
    pnlTest.Color := clRed;
end; 

有什么想法吗?谢谢!

3 个答案:

答案 0 :(得分:19)

如果您想在主题操作系统下设置彩色面板,则必须将ParentBackground设置为False。

答案 1 :(得分:0)

试试这个: - )

procedure TForm1.Button1Click(Sender: TObject);
var
  pnlTest : TPanel;
begin
    pnlTest := TPanel.Create(Form1);
    pnlTest.Parent := Form1;
    pnlTest.Width := 100;
    pnlTest.Height := 100;
    pnlTest.ParentBackground := false;
    pnlTest.Color := clRed;
end;

答案 2 :(得分:0)

这是maXbox脚本的代码:

procedure SetArrayLength2Panels(var arr: array of array of TPanel;
                                            asize1, asize2: Integer);
var i: Integer;
begin setlength(arr, asize1);
   for i:= 0 to asize1-1 do SetLength(arr[i], asize2);
end;

procedure TMyFormInitialisePanels(aform: Tform; RowCount,ColCount: Integer);
var
  aLeft,aTop,aWidth,aHeight, row,col: Integer;
  Panel: TPanel;
  FPanels: array of array of TPanel;
begin
  //SetLength(FPanels, RowCount, ColCount);
  SetArrayLength2Panels(Fpanels, RowCount, ColCount)
  aTop:= 0;
  for Row:= 0 to RowCount-1 do begin
    aLeft:= 0;
    aHeight:= (aform.ClientHeight-aTop) div (RowCount-Row);
    for Col:= 0 to ColCount-1 do begin
      Panel:= TPanel.Create(Self);
      FPanels[Row][Col]:= Panel;
      Panel.Parent:= aform; //Self;
      //panel.parentcolor:= false;
      panel.ParentBackground:= false;
      panel.color:= random(clred)
      aWidth:= (aform.ClientWidth-aLeft) div (ColCount-Col);
      Panel.SetBounds(aLeft, aTop, aWidth, aHeight);
      inc2(aLeft, aWidth);
    end;
    inc2(aTop, aHeight);
  end;
end;