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

问题描述 投票:0回答:4

遇到一个奇怪的问题:我在运行时创建一个 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; 

有什么想法吗?谢谢!

delphi delphi-2009
4个回答
21
投票

当你想在主题操作系统下有彩色面板时,你必须将 ParentBackground 设置为 False。


1
投票

试试这个:-)

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;

1
投票

这是 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;

0
投票

对于你们中的cpp builder到达这里的开发人员:

ParentBackground
ParentColor
FullRepaint
应设置为 false

© www.soinside.com 2019 - 2024. All rights reserved.