为什么FireMonkey TrackBar1.Tag会以这种方式使用?

时间:2013-07-14 21:37:36

标签: delphi firemonkey delphi-xe4

我从FireMonkey MediaPlayerHD示例中提取了此代码段 Delphi XE4附带的程序。我以前从未见过以这种方式使用Tag属性。什么是 这样做的目的是什么?

procedure TForm240.Timer1Timer(Sender: TObject);
begin
  TrackBar1.Tag := 1;
  TrackBar1.Value := MediaPlayer1.CurrentTime;
  TrackBar1.Tag := 0;
end;

更新 这是完整的源代码:

//---------------------------------------------------------------------------

// This software is Copyright (c) 2012 Embarcadero Technologies, Inc. 
// You may only use this software if you are an authorized licensee
// of Delphi, C++Builder or RAD Studio (Embarcadero Products).
// This software is considered a Redistributable as defined under
// the software license agreement that comes with the Embarcadero Products
// and is subject to that software license agreement.

//---------------------------------------------------------------------------
unit MediaPlayerForm;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Objects, FMX.Layouts, FMX.Media,
  FMX.Filter, FMX.Filter.Effects, FMX.StdCtrls;

type
  TForm240 = class(TForm)
    OpenDialog1: TOpenDialog;
    OpenButton: TSpeedButton;
    Rectangle1: TRectangle;
    PauseButton: TSpeedButton;
    TrackBar1: TTrackBar;
    Label1: TLabel;
    Timer1: TTimer;
    CheckBox1: TCheckBox;
    VolumeTrack: TTrackBar;
    MediaPlayer1: TMediaPlayer;
    MediaPlayerControl1: TMediaPlayerControl;
    ClearButton: TSpeedButton;
    procedure OpenButtonClick(Sender: TObject);
    procedure PauseButtonClick(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure CheckBox1Change(Sender: TObject);
    procedure VolumeTrackChange(Sender: TObject);
    procedure ClearButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form240: TForm240;

implementation

{$R *.fmx}

procedure TForm240.CheckBox1Change(Sender: TObject);
begin
  MediaPlayerControl1.Visible := CheckBox1.IsChecked;
end;

procedure TForm240.OpenButtonClick(Sender: TObject);
begin
  OpenDialog1.Filter := TMediaCodecManager.GetFilterString;
  if OpenDialog1.Execute then
  begin
    PauseButton.Enabled := True;
    ClearButton.Enabled := True;
    MediaPlayer1.FileName := OpenDialog1.FileName;

    if MediaPlayer1.Media <> nil then
    begin
      Label1.Text := IntToStr(MediaPlayer1.Media.VideoSize.Truncate.X) + 'x' + IntToStr(MediaPlayer1.Media.VideoSize.Truncate.Y) + 
        'px ' + IntToStr(MediaPlayer1.Media.Duration div MediaTimeScale) + 'ms';
      TrackBar1.Max := MediaPlayer1.Media.Duration;
      VolumeTrack.Value := 1 - MediaPlayer1.Media.Volume;

      MediaPlayer1.Play;
    end;
  end;
end;

procedure TForm240.PauseButtonClick(Sender: TObject);
begin
  if MediaPlayer1.State = TMediaState.Playing then
  begin 
    PauseButton.Text := 'Play';
    MediaPlayer1.Stop;
  end
  else
  begin
    PauseButton.Text := 'Pause';
    MediaPlayer1.Play;
  end;
end;

procedure TForm240.ClearButtonClick(Sender: TObject);
begin
  MediaPlayer1.Clear;
  PauseButton.Enabled := False;
  ClearButton.Enabled := False;
end;

procedure TForm240.Timer1Timer(Sender: TObject);
begin
  TrackBar1.Tag := 1;
  TrackBar1.Value := MediaPlayer1.CurrentTime;
  TrackBar1.Tag := 0;
end;

procedure TForm240.TrackBar1Change(Sender: TObject);
begin
  if TrackBar1.Tag = 0 then
    MediaPlayer1.CurrentTime := Round(TrackBar1.Value);
end;

procedure TForm240.VolumeTrackChange(Sender: TObject);
begin
  MediaPlayer1.Volume := 1 - VolumeTrack.Value; 
end;

end.

1 个答案:

答案 0 :(得分:3)

Tag的值在这里产生任何影响的唯一方法是,如果有一个事件处理程序响应Value的更改。并为该事件处理程序切换Tag的值的行为。

如果您想要对跟踪栏值的编程修改进行不同于用户修改的处理,则通常会这样做。

要准确了解正在发生的事情,您需要在其余代码中查找Tag的其他用途。没有看到其余的代码(应该在问题中),我无法验证解释是否如上所述,但在我看来,这是该代码唯一合理的解释。

无论如何,Tag的使用非常蹩脚。我总是希望看到一个名称描述其目的的变量。

<强>更新

现在已经添加了完整的代码,可以看出Tag的使用完全如上所述。