2013-08-28 57 views
2

我有TButton和TPaintBox簡單的FreePascal代碼。TPaintBox不畫在TButton事件「onClick」

我對這些元素的事件:

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。

有人可以幫我嗎?

謝謝。

回答

2

我認爲TPaintBox.Color顏色是誤發佈屬性。它不會填充背景或做任何事情(至少在Delphi中,在拉扎路斯它將是相同的,我會說)。

另外,設置該顏色後應該打電話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; 
+0

非常感謝。它有很多幫助。有用。我是FreePascal(Lazarus)的初學者,沒有好的教程。 – petesalt

+0

不客氣! – TLama