4

这是TPanel当鼠标光标悬停在它上面时“动画”的代码。我也有一个代码块来取消它的动画。

procedure Tmain.pStarting1MouseEnter(Sender: TObject);
begin
  if sender = pStarting1 then pStarting1.BevelInner := bvLowered;
  if sender = pStarting2 then pStarting2.BevelInner := bvLowered;
  if sender = pStarting3 then pStarting3.BevelInner := bvLowered;
  if sender = pStarting4 then pStarting4.BevelInner := bvLowered;
  if sender = pStarting5 then pStarting5.BevelInner := bvLowered;
  if sender = pStarting6 then pStarting6.BevelInner := bvLowered;
  if sender = pStarting7 then pStarting7.BevelInner := bvLowered;
  if sender = pStarting8 then pStarting8.BevelInner := bvLowered;
  if sender = pStarting9 then pStarting9.BevelInner := bvLowered;
end;

正如你所看到的,它非常重复,因为我还有另外 27TPanel秒的动画,这意味着另外 27 行重复的代码。那么有没有办法可以优化呢?

我也尝试过将该代码块放入一个单独的过程中(在同一个单元中)。但德尔福告诉我这sender是未声明的。

4

2 回答 2

11
if Sender is TPanel then
  TPanel(Sender).BevelInner := bvLowered;

或者,如果您确定它Sender始终是 a TPanel,只需

(Sender as TPanel).BevelInner := bvLowered;

或(如果你真的确定)

TPanel(Sender).BevelInner := bvLowered;
于 2012-05-19T09:26:59.400 回答
1

第四种方法是利用absolute关键字。

procedure Tmain.pStarting1MouseEnter(Sender: TObject);
var
    Panel: TPanel absolute Sender;

begin
    if Sender is TPanel then
        Panel.BevelInner := bvLowered;
end;
于 2012-05-20T03:34:58.753 回答