Delphi 中 动态创建的Panel无法改变颜色的解决办法

刚开始代码如下:

procedure TForm1.Button1Click(Sender: TObject);
var
  Panel: TPanel;
begin
  Panel := TPanel.Create(Form1);
  Panel.Name := 'pnlPreVideo';
  Panel.Parent := Form1;
  Panel.OnClick := Panel1Click;
  MoveWindow(Panel.Handle, 10, 10, 100, 100, True);
end;

procedure TForm1.Panel1Click(Sender: TObject);
begin
  if  (Sender as TPanel).Name ='pnlPreVideo' then
    (Sender as TPanel).Color := clGreen;
end;

单击时,颜色不变,问网友,原来是一个BUG,更正后代码如下:

procedure TForm1.Button1Click(Sender: TObject);
var
  Panel: TPanel;
begin
  Panel := TPanel.Create(Form1);
  Panel.Name := 'pnlPreVideo';
  Panel.ParentBackground := False;
  Panel.Parent := Form1;
  Panel.OnClick := Panel1Click;
  MoveWindow(Panel.Handle, 10, 10, 100, 100, True);
end;

procedure TForm1.Panel1Click(Sender: TObject);
begin
  if  (Sender as TPanel).Name ='pnlPreVideo' then
    (Sender as TPanel).Color := clGreen;
end;

 

你可能感兴趣的:(Delphi)