Título: Tocando Waves à partir dos recursos.
Linguagem: C++Builder
S.O.: Windows
Autor(es): Wenderson Teixeira
Muitas vezes queremos adicionar efeitos sonoros aos nossos programas, para isso,
existem funções na API do Windows que tocam arquivos no formato WAVE, mas nem sempre
achamos conveniente enviar um arquivo a mais só para termos um barulhinho, e acabamos
não colocando, agora este problema pode ser resolvido, colocando-se o arquivo WAVE,
junto com os recursos do seu programa, utilizando-se o tipo WAVE e tocando-o utilizando
a função PlaySound, com o flag SND_RESOURCE:
BOOL PlaySound(
LPCTSTR lpszName, // String de recurso ou arquivo
HANDLE hModule, // Handle do módulo do recurso
DWORD fdwSound // Flags opcionais
);
Abaixo segue um exemplo de utilização:
//---------------------------------------------------------------------------
#ifndef MainFrmH
#define MainFrmH
//---------------------------------------------------------------------------
#include <vcl\Classes.hpp>
#include <vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
#include <vcl\ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TMainForm : public TForm
{
__published: // IDE-managed Components
TButton *ExitBtn;
TButton *Play1Btn;
TButton *Play2Btn;
TPanel *Panel1;
TLabel *Label1;
void __fastcall ExitBtnClick(TObject *Sender);
void __fastcall Play1BtnClick(TObject *Sender);
void __fastcall Play2BtnClick(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TMainForm(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern TMainForm *MainForm;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include <mmsystem.h>
#include "MainFrm.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ExitBtnClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Play1BtnClick(TObject *Sender)
{
if(!PlaySound(MAKEINTRESOURCE(100), HInstance, SND_ASYNC | SND_RESOURCE | SND_NOWAIT | SND_NODEFAULT))
Application->MessageBox("Erro ao tentar tocar som 1.", 0, MB_ICONEXCLAMATION);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Play2BtnClick(TObject *Sender)
{
if(!PlaySound("WAVERC2", HInstance, SND_ASYNC | SND_RESOURCE | SND_NOWAIT | SND_NODEFAULT))
Application->MessageBox("Erro ao tentar tocar som 2.", 0, MB_ICONEXCLAMATION);
}
//---------------------------------------------------------------------------
object MainForm: TMainForm
Left = 193
Top = 109
Width = 409
Height = 197
Caption = 'WaveRC'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
PixelsPerInch = 96
TextHeight = 13
object ExitBtn: TButton
Left = 8
Top = 136
Width = 75
Height = 25
Caption = 'Sai&r'
TabOrder = 0
OnClick = ExitBtnClick
end
object Play1Btn: TButton
Left = 8
Top = 8
Width = 75
Height = 25
Caption = 'Tocar Som &1'
TabOrder = 1
OnClick = Play1BtnClick
end
object Play2Btn: TButton
Left = 8
Top = 40
Width = 75
Height = 25
Caption = 'Tocar Som &2'
TabOrder = 2
OnClick = Play2BtnClick
end
object Panel1: TPanel
Left = 88
Top = 8
Width = 305
Height = 153
BevelOuter = bvLowered
TabOrder = 3
object Label1: TLabel
Left = 16
Top = 16
Width = 281
Height = 121
Alignment = taCenter
AutoSize = False
Caption = 'Programa exemplo de como tocar sons à partir de recursos.'#10#10'feito por'#10' Wenderson Teixeira'
Font.Charset = ANSI_CHARSET
Font.Color = clBlack
Font.Height = -19
Font.Name = 'MS Sans Serif'
Font.Style = []
ParentFont = False
WordWrap = True
end
end
end
#define WAVERC1 100 WAVERC1 WAVE "c:/windows/media/ringin.wav" WAVERC2 WAVE "c:/windows/media/ringout.wav"