如何添加音乐 ON/OFF 功能按钮?

How to add Music ON/OFF functionality button?

如何在除完成页面之外的所有向导页面的左下角包含音乐 ON/OFF 功能按钮。只有在用户点击完成按钮后,音乐才会停止。

    procedure InitializeWizard();
begin
  // Welcome page
  // Hide the labels
  WizardForm.WelcomeLabel1.Visible := False;
  WizardForm.WelcomeLabel2.Visible := False;
  // Stretch image over whole page
  WizardForm.WizardBitmapImage.Width := WizardForm.WizardBitmapImage2.Parent.Width;

  begin with WizardForm.WizardSmallBitmapImage do SetBounds(Parent.Left, Parent.Top, Parent.Width, Parent.Height);
  WizardForm.PageDescriptionLabel.Visible := False;
  WizardForm.PageNameLabel.Visible := False; end;

  // Finished page
  // Hide the labels
  WizardForm.FinishedLabel.Visible := False;
  WizardForm.FinishedHeadingLabel.Visible := False;
  // Stretch image over whole page
  WizardForm.WizardBitmapImage2.Width := WizardForm.WizardBitmapImage2.Parent.Width;
end;

procedure AboutButtonOnClick(Sender: TObject);
begin
  MsgBox('This is the about message!', mbInformation, mb_Ok);
end;

procedure InitializeWizard();
  AboutButton : TNewButton;
begin
  // create an instance of the button and assign it to the local variable AboutButton
  AboutButton := TNewButton.Create(WizardForm);
  // set the parent to the just created button control
  AboutButton.Parent := WizardForm;
  // adjust the position to the created button control; it gets the horizontal indent
  // by the right indent of the Cancel button; the vertical position as well as width
  // and height are the same as the Cancel button has
  AboutButton.Left := WizardForm.ClientWidth - WizardForm.CancelButton.Left -
    WizardForm.CancelButton.Width;
  AboutButton.Top := WizardForm.CancelButton.Top;
  AboutButton.Width := WizardForm.CancelButton.Width;
  AboutButton.Height := WizardForm.CancelButton.Height;
  // set its caption
  AboutButton.Caption := '&About';
  // and assign the AboutButtonOnClick method to the OnClick event of the button
  AboutButton.OnClick := @AboutButtonOnClick;
end;

procedure AboutButtonOnClick(Sender: TObject);
begin
  MsgBox('This is the about message!', mbInformation, mb_Ok);
end;

const
  BASS_SAMPLE_LOOP = 4;
  BASS_UNICODE = 000000;
  BASS_CONFIG_GVOL_STREAM = 5;
const
  #ifndef UNICODE
    EncodingFlag = 0;
  #else
    EncodingFlag = BASS_UNICODE;
  #endif
type
  HSTREAM = DWORD;

function BASS_Init(device: LongInt; freq, flags: DWORD;
  win: HWND; clsid: Cardinal): BOOL;
  external 'BASS_Init@files:bass.dll stdcall';
function BASS_StreamCreateFile(mem: BOOL; f: string; offset1: DWORD;
  offset2: DWORD; length1: DWORD; length2: DWORD; flags: DWORD): HSTREAM;
  external 'BASS_StreamCreateFile@files:bass.dll stdcall';
function BASS_ChannelPlay(handle: DWORD; restart: BOOL): BOOL;
  external 'BASS_ChannelPlay@files:bass.dll stdcall';
function BASS_SetConfig(option: DWORD; value: DWORD ): BOOL;
  external 'BASS_SetConfig@files:bass.dll stdcall';
function BASS_Free: BOOL;
  external 'BASS_Free@files:bass.dll stdcall';

procedure InitializeWizard;
var
  StreamHandle: HSTREAM;
begin
  ExtractTemporaryFile('AudioFile.mp3');
  if BASS_Init(-1, 44100, 0, 0, 0) then
  begin
    StreamHandle := BASS_StreamCreateFile(False,
      ExpandConstant('{tmp}\AudioFile.mp3'), 0, 0, 0, 0,
      EncodingFlag or BASS_SAMPLE_LOOP);
    BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, 2500);
    BASS_ChannelPlay(StreamHandle, False);
  end;
end;

procedure DeinitializeSetup;
begin
  BASS_Free;
end;

procedure AboutButtonOnClick(Sender: TObject);
begin
  DSStopMediaPlay;
end;

var
  // Global variable
  AboutButton: TNewButton;

procedure InitializeWizard;
begin
  // create an instance of the button and assign it to the global variable AboutButton
  Music ON\OFF  := TNewButton.Create(WizardForm);
  ...
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  // Hide button on Finished page
  if CurPageID = wpFinished then
  begin
    AboutButton.Visible := False;
  end;

要在底部面板上创建按钮,请参阅:


背景音乐参见:
Playing sound during an Inno Setup install


要在单击按钮时停止音乐播放,只需从 OnClick 处理程序调用 DSStopMediaPlay (Inno Media Player)。

procedure AboutButtonOnClick(Sender: TObject);
begin
  DSStopMediaPlay;
end;

要在完成页面上隐藏按钮,您需要将按钮引用保存到全局变量并在 CurPageChanged(wpFinished):

中设置 .Visible = false
[Code]

var
  { Global variable }
  AboutButton: TNewButton;

procedure InitializeWizard;
begin
  { create an instance of the button and assign it to the global variable AboutButton }
  AboutButton := TNewButton.Create(WizardForm);
  ...
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  { Hide button on Finished page }
  if CurPageID = wpFinished then
  begin
    AboutButton.Visible := False;
  end;
end;

当然,您要将变量命名为StopButton,而不是AboutButton