开发者

Delphi 2009 - create a TPanel at runtime and change its color

got a strange problem: I create a TPanele at runtime and change its color - however, the color is still clBtnFace.

Here' the code:

procedure TForm1.Button1Click(Sender: TObject);
var
  pnlTest : TPanel;
begin
    pnlTest := TPanel.Create(Form1);
    pnl开发者_高级运维Test.Parent := Form1;
    pnlTest.Width := 100;
    pnlTest.Height := 100;
    pnlTest.Color := clRed;
end; 

Any ideas? Thanks!


When you want to have colored panels under a themed OS you have to set ParentBackground to False.


Try this :-)

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;


This the code for maXbox scripting:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜