C++ Builder

La Isla Bonita



Detect Volume Changes
Detect Volume Changes


Description

This program detects volume changes generated by external applications.

MAINFORM.cpp

#include <vcl\vcl.h>
#pragma hdrstop

#include "MAINFORM.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
    waveOutGetVolume(0, (LPDWORD) &OldVolume); //store the volume
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  if(OpenDialog1->Execute())
  {
    MediaPlayer1->FileName=OpenDialog1->FileName;

    try
    {
        MediaPlayer1->Open();
    }
    catch (...)
    {
        // If there is an exception, terminate the application
        Application->MessageBox("Exception opening file:", mtWarning, MB_ICONWARNING|MB_OK);
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MediaPlayer1Click(TObject *Sender, TMPBtnType Button,
  bool &DoDefault)
{
  DoDefault = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    waveOutGetVolume(0, (LPDWORD) &NewVolume);

    if ( OldVolume != NewVolume )
    {
        OldVolume = NewVolume;
        Timer1->Enabled = false;
 
        if ( Application->MessageBox( "Wave Volume changed by an external application \
\nPress OK to continue detection or CANCEL to stop detection",
                                       "Volume Change",
                                       MB_ICONQUESTION|MB_OKCANCEL ) == IDOK )
        {
            Timer1->Enabled = true;
        }
     }
}

MAINFORM.h

#ifndef MAINFORMH
#define MAINFORMH
//---------------------------------------------------------------------------
#include <vcl\Classes.hpp>
#include <vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
#include <vcl\MPlayer.hpp>
#include <vcl\Dialogs.hpp>
#include <vcl\ComCtrls.hpp>
#include <vcl\ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
  TButton *Button1;
  TMediaPlayer *MediaPlayer1;
  TOpenDialog *OpenDialog1;
  TProgressBar *ProgressBar1;
  TTimer *Timer1;
  void __fastcall Button1Click(TObject *Sender);
  void __fastcall MediaPlayer1Click(TObject *Sender, TMPBtnType Button,
  bool &DoDefault);
  void __fastcall Timer1Timer(TObject *Sender);
	void __fastcall FormClose(TObject *Sender, TCloseAction &Action);

private:	// User declarations
    DWORD OldVolume, NewVolume;

public:		// User declarations
  __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.