0

I designed a component like "Tile" and I need to change the color when clicking down or up. How can I do this?

4

1 回答 1

3

覆盖 TControl 中引入的过程 MouseDown() 和 MouseUp() 来设置组件的颜色。
如果您的组件没有颜色属性,您将需要覆盖 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;
于 2013-04-29T05:26:37.150 回答