TPaintBox不会在TButton事件上绘制“onClick”

时间:2013-08-28 08:39:01

标签: pascal freepascal lazarus

我有使用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。

有人可以帮助我吗?

谢谢。

1 个答案:

答案 0 :(得分: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;