自定义tSpeedButton绘画

时间:2018-06-14 20:59:37

标签: firemonkey custom-painting

表单上有两个TSpeedButton按钮。启用一个按钮时,另一个按钮被禁用,反之亦然。我想用自定义颜色绘制按钮,以反映它们是启用还是禁用。

我编写了一个名为SetButtonsColors的程序。此过程设置(一次一个按钮),变量ButtonFillColorButtonStrokeColor使用自定义颜色,相应地设置按钮状态,并立即命令重新绘制按钮。

ButtonPaint处理程序中,我将Canvas.Fill.ColorCanvas.Stroke.Color分别设置为ButtonFillColorButtonStrokeColor,然后填充和绘制aRect

令我失望的是,按钮总是涂上为最后一个按钮设置的颜色。我认为,这种情况会发生,因为Windows会异步绘制按钮,因此会使用Canvas.Fill.ColorCanvas.Stroke.Color的最后设置。

如果这是真的,我如何强制Windows在重新命令后立即画出按钮?

遵循SetButtonsColorsButtonPaint的代码:

procedure tForm1.SetButtonsColors;
begin
  if Button1.Enabled then begin
     ButtonFillColor   := tAlphaColorRec.White;
     ButtonStrokeColor := tAlphaColorRec.Black;
     Button1.Repaint;
     ButtonFillColor   := tAlphaColorRec.Black;
     ButtonStrokeColor := tAlphaColorRec.White;
     Button1.Repaint;
  end;
  else begin
         ButtonFillColor   := tAlphaColorRec.Black;
         ButtonStrokeColor := tAlphaColorRec.White;
         Button1.Repaint;
         ButtonFillColor   := tAlphaColorRec.White;
         ButtonStrokeColor := tAlphaColorRec.Black;
         Button2.Repaint;
       end;
end;

procedure tForm1.ButtonPaint (Sender: tObject; Canvas: tCanvas; const aRect: tRectF);
begin
  Canvas.BeginScene;
  Canvas.Fill  .Color := lvButtonFillColor;
  Canvas.Stroke.Color := lvButtonStrokeColor;
  Canvas.FillRect (aRect, 0, 0,  []       , 1, CornerTypeBevel);
  Canvas.DrawRect (aRect, 0, 0, AllCorners, 1, CornerTypeBevel);
  Canvas.EndScene;
end;

感谢。

0 个答案:

没有答案