Search code examples
inno-setuppascalscript

How to add Music ON/OFF functionality button?


How can I include a Music ON/OFF functionality button in the bottom left corner of all wizard pages except the Finished page. And the music being active should stop only after the user hit Finish button.

enter image description here

    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 = $80000000;
  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;

Solution

  • To create a button on the bottom panel, see:
    How to create new About button in Inno Setup?


    For background music, see:
    Playing sound during an Inno Setup install


    To stop music playback on button click, just call DSStopMediaPlay (Inno Media Player) from the OnClick handler.

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

    To hide the button on Finished page, you need to save a button reference to a global variable and set .Visible = false in CurPageChanged(wpFinished):

    [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;
    

    Of course, you want to name the variable StopButton, not AboutButton.