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

发布于 2024-09-24 14:40:59 字数 352 浏览 7 评论 0原文

遇到一个奇怪的问题:我在运行时创建一个 TPaele 并更改其颜色 - 但是,颜色仍然是 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; 

有什么想法吗?谢谢!

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);
    pnlTest.Parent := Form1;
    pnlTest.Width := 100;
    pnlTest.Height := 100;
    pnlTest.Color := clRed;
end; 

Any ideas? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡淡绿茶香 2024-10-01 14:40:59

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

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

晒暮凉 2024-10-01 14:40:59

试试这个:-)

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;

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;
烟酒忠诚 2024-10-01 14:40:59

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

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;
不再让梦枯萎 2024-10-01 14:40:59

对于到达这里的 cpp builder 开发人员来说:

ParentBackgroundParentColorFullRepaint 应设置为 false

For those of you, cpp builder developers who arrive here:

ParentBackground, ParentColor and FullRepaint should be set to false

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文