单击向下和向上更改组件的颜色

时间:2013-04-29 04:58:08

标签: delphi delphi-xe2

我设计了一个像“Tile”这样的组件,我需要在点击或向上点击时更改颜色。 我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

重写TControl中引入的MouseDown()和MouseUp()程序,以设置组件的颜色。
如果您的组件没有color属性,则需要覆盖Paint过程。

type
  TMyComp = Class(TGraphicControl)
  private
    FColor: TColor;
  protected
    // ....
    Procedure Paint; override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
      X, Y: Integer); override;
    procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
      override;
  End;

implementation

{ TMyComp }

procedure TMyComp.MouseDown(Button: TMouseButton; Shift: TShiftState;
  X, Y: Integer);
begin
  inherited;
  if Button = mbLeft then
  begin
    FColor := clRed;
    invalidate;
  end;
end;

procedure TMyComp.MouseUp(Button: TMouseButton; Shift: TShiftState;
  X, Y: Integer);
begin
  inherited;
  if Button = mbLeft then
  begin
    FColor := clLime;
    invalidate;
  end;

end;

procedure TMyComp.Paint;
begin
  inherited;
  // ...
  Canvas.Brush.Color := FColor;
  Canvas.FillRect(BoundsRect);
  // ....
end;