2

我有带有 TButton 和 TPaintBox 的简单 FreePascal 代码。

我有关于这些元素的事件:

procedure TForm1.Button1Click(Sender: TObject);
begin
     Button1.Enabled := False;
     Button2.Enabled := True;
     Button1.Caption := 'Off';
     Button2.Caption := 'On';
     PaintBox1.Invalidate;
     PaintBox1.Color := clYellow;
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
   PaintBox1.Canvas.Brush.Color := clGreen;
   PaintBox1.Canvas.Ellipse(0,0,100,50);
end;

但它不会在 TButton 上使用 onClick 事件绘制我的 TPaintBox。

有人可以帮我吗?

谢谢你。

4

1 回答 1

2

我认为TPaintBox.Color颜色是错误发布的属性。它不会填充背景或做任何事情(至少在 Delphi 中,在 Lazarus 中它会是一样的,我会说)。

此外,您应该Invalidate在设置该颜色后调用,但如果它什么都不做,您现在不需要关心它。你可以这样写:

procedure TForm1.Button1Click(Sender: TObject);
begin
  // the TPaintBox.Color does nothing, so let's use it for passing the
  // background color we will fill later on in the OnPaint event
  PaintBox1.Color := clYellow;
  // and tell the system we want to repaint our paint box
  PaintBox1.Invalidate;
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  // set the brush color to the TPaintBox.Color
  PaintBox1.Canvas.Brush.Color := PaintBox1.Color;
  // and fill the background by yourself
  PaintBox1.Canvas.FillRect(PaintBox1.ClientRect);
  // and then draw an ellipse
  PaintBox1.Canvas.Brush.Color := clGreen;
  PaintBox1.Canvas.Ellipse(0, 0, 100, 50);
end;
于 2013-08-28T08:52:47.183 回答