mainform.cpp

来自「这是用C++BUILDER编写的播放声音文件的程序」· C++ 代码 · 共 62 行

CPP
62
字号
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "MAINFORM.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  // pop up the common file dialog
  if(OpenDialog1->Execute())
  {
    MediaPlayer1->FileName=OpenDialog1->FileName;
    // Open raises an exception if something acts up. use a try 
    // catch block to detect an error.
    try
    {
      MediaPlayer1->Open();
    }
    catch (...)
    {
     // should Application->Terminate if an exception occurs
      Application->MessageBox("Error opening file",mtWarning,MB_ICONWARNING|MB_OK);
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MediaPlayer1Click(TObject *Sender, TMPBtnType Button,
  bool &DoDefault)
{
  // find out which button was pressed, if Play, then start
  // the timer, if Stop the disable it. The timer will just
  // run if pause was pushed
  if(Button == btPlay)
    Timer1->Enabled = true;
  else if (Button == btStop)
    Timer1->Enabled = false;

  // set DoDefault to true so the MediaPlayer will do
  // what it normally does for the button press
  DoDefault = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  // update position using properties of TMediaPlayer need to cast because
  // TProgressBar is 16 bits while TMediaPlayer is 32. Division ensures that
  // number will be between 0 and 100
  ProgressBar1->Position = (TProgressRange)(MediaPlayer1->Position * 100 /
                                              MediaPlayer1->Length);
  // if media has finished then shut off the timer
  if(ProgressBar1->Position == 100)
    Timer1->Enabled = false;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?