Вы находитесь на странице: 1из 22

TALLER DE PROGRAMACION EN C++ BUILDER

Ejemplo de un convertidor de divisas sencillo


La interfaz de usuario esta formada por:
De la paleta standard:
Tres etiquetas Label, un cuadro de texto Edit, una Lista desplegable ComboBox y dos Botones Button.

//--------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//--------------------------------------------------------------------------#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
float cambio[20];
//--------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------void __fastcall TForm1::FormCreate(TObject *Sender)
{
ComboBox1->Items->Add("Dolares");
ComboBox1->Items->Add("Marcos Alemanes");
ComboBox1->Items->Add("Francos Suizos");
ComboBox1->Items->Add("Francos Alemanes");
ComboBox1->Items->Add("Libras Esterlinas");
cambio[0]=154.2;
cambio[1]=970.3;
cambio[2]=108.30;
cambio[3]=23.2;
cambio[4]=164.2;
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender)
{
int codigo;
float valor,endivisa;
char cadena[25];
try
{valor=StrToFloat(Edit1->Text);
endivisa=valor/cambio[ComboBox1->ItemIndex];
Label3->Caption =FloatToStr(endivisa)+ " " +ComboBox1->Text;
}
catch(Exception& E)

{
MessageDlg(E.Message, mtError, TMsgDlgButtons() << mbOK, 0);
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button2Click(TObject *Sender)
{
Close();
}
//--------------------------------------------------------------------------void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
if ((Key>='a'&& Key<='z')||(Key>='A'&& Key<='Z'))
{
ShowMessage("Numero Invalido");
Edit1->ReadOnly=true;
}
else
Edit1->ReadOnly=false;
}
//--------------------------------------------------------------------------void __fastcall TForm1::ComboBox1Click(TObject *Sender)
{
Label3->Caption ="Valor del Cambio" ;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------

TALLER DE C++ BUILDER


Ejemplo para darle formato a un texto.
De la paleta estndar Utilizar los siguientes componentes.
3 GroupBox
12 RadioButtons
2 Buttons
1 Label

//--------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//--------------------------------------------------------------------------#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)


: TForm(Owner)
{
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender)
{
// Cambiar el estilo de la fuente
if (RadioButton1->Checked)
Label1->Font->Style = TFontStyles();
if (RadioButton2->Checked)
Label1->Font->Style = TFontStyles()<< fsItalic;
if (RadioButton3->Checked)
Label1->Font->Style = TFontStyles()<< fsBold;
if (RadioButton4->Checked)
Label1->Font->Style = TFontStyles()<< fsBold << fsItalic;
// Cambiar el color de la fuente
if (RadioButton5->Checked)
Label1->Font->Color=clBlack;
if (RadioButton6->Checked)
Label1->Font->Color=clBlue;
if (RadioButton7->Checked)
Label1->Font->Color=clRed;
if (RadioButton8->Checked)
Label1->Font->Color=clGreen;
// Cambiar el tipo de la fuente
if (RadioButton9->Checked)
Label1->Font->Name="MS Sans Serif";
if (RadioButton10->Checked)
Label1->Font->Name="Times New Roman";
if (RadioButton11->Checked)
Label1->Font->Name="Arial";
if (RadioButton12->Checked)
Label1->Font->Name="Courier New";
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button2Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------

TALLER DE PROGRAMACION AVANZADA C++ BUILDER


Ejemplo de un editor de texto sencillo
La interfaz de usuario esta formada por:
De la paleta standard:
Una etiqueta Label, un cuadro de texto edit, un MainMenu, un Memo.
De la paleta Additional:
Dos Shapes, siete BitBtn.
Un control Image que contien un icono con una mano para indicarle al usario el recuadro de texto donde tiene que introducir el
nombre del archivo. Si este no se introduce cuando se quiere Abrir o Guardar un archivo, o no existe el archivo al ejecutarse
cualquiera de los dos metodos, SaveToFile o LoadFromFile, debe gestionarse un manipulador de excepciones que controle el
error o errores que se puedan producir.
De la paleta Dialogs:
Un OpenDialog, un SaveDialog y un PrintDialog.

//--------------------------------------------------------------------------#include <vcl\vcl.h>
#pragma hdrstop
#include "Unit1.h"
//--------------------------------------------------------------------------#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------void __fastcall TForm1::BitBtn7Click(TObject *Sender)
{
Close();
}
//--------------------------------------------------------------------------void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
Memo1->Clear();
Memo1->SetFocus();
}
//--------------------------------------------------------------------------void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
Memo1->CopyToClipboard();
Memo1->SetFocus();
}
//--------------------------------------------------------------------------void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Memo1->CutToClipboard();
Memo1->SetFocus();
}

//--------------------------------------------------------------------------void __fastcall TForm1::BitBtn2Click(TObject *Sender)


{
Memo1->PasteFromClipboard();
Memo1->SetFocus();
}
//--------------------------------------------------------------------------void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
try
{
Memo1->Lines->LoadFromFile(Edit1->Text);
}
catch (Exception &exception)
{
ShowMessage("No se puede cargar el archivo");
}
Memo1->SetFocus();
}
//--------------------------------------------------------------------------void __fastcall TForm1::BitBtn6Click(TObject *Sender)
{
try
{
Memo1->Lines->SaveToFile(Edit1->Text);
}
catch (Exception &exception)
{
ShowMessage("No se puede guardar el archivo");
}
Memo1->SetFocus();
}
//--------------------------------------------------------------------------void __fastcall TForm1::Abrir1Click(TObject *Sender)
{
OpenDialog1->Filter = "Text files (*.txt)|*.TXT";
OpenDialog1->Execute();
Edit1->Text=OpenDialog1->FileName;
Memo1->Lines->LoadFromFile(Edit1->Text);
}
//--------------------------------------------------------------------------void __fastcall TForm1::Guardar1Click(TObject *Sender)
{
SaveDialog1->Filter = "Text files (*.txt)|*.TXT";
SaveDialog1->Execute();
Edit1->Text=SaveDialog1->FileName;
Memo1->Lines->SaveToFile(Edit1->Text);
}
//--------------------------------------------------------------------------void __fastcall TForm1::Imprimir1Click(TObject *Sender)
{
PrintDialog1->Execute();
}
//---------------------------------------------------------------------------

TALLER DE C++ BUILDER - USO DEL COMPONENTE TIMER


COMPONENTES USADOS:
DE LA PALETA ESTNDAR:
4 Labels.
Modificar las siguientes propiedades en tiempo de diseo
Label1->Caption=ALTURA
Label2->Caption=0
Label3->Caption=NUMEO DE REBOTES
Label4->Caption=0
DE LA PALETA ADDITIONAL:
2 Shapes.
1 StatusBar.
Modificar las siguientes propiedades en tiempo de diseo
Shape1->Height=25
Shape1->Left=32
Shape1->Top=64
Shape1->Shape=stRectangle
Shape1->Brush->Color=clOlive
Shape1->Brush->Style=bsBDiagonal
Shape1->Height=217
Shape1->Left=32
Shape1->Top=88
DE LA PALETA SYSTEM:
3 Timers.
Modificar las siguientes propiedades en tiempo de diseo
Timer1->Enabled=false
Timer2->Enabled=false
Timer1->Interval=1
Timer2->Interval=1
Timer3->Interval=1
With the Canvas object, you can set the properties of a pen for drawing lines, a brush for filling shapes, a font for writing text, and
an array of pixels to represent the image.
//--------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop
#include "math.h"
#include "Unit1.h"
//--------------------------------------------------------------------------#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
float altura,altini;
int contreb=0;
//--------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------void __fastcall TForm1::FormPaint(TObject *Sender)
{
Form1->Canvas->MoveTo(97,303);// El objeto CanvasProvides access to the drawing area of the form.
Form1->Canvas->LineTo(550,303);
}
//--------------------------------------------------------------------------void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if (Shape1->Left==90)
{Timer1->Enabled =false;
Timer2->Enabled =true;
}

Shape1->Left=Shape1->Left+2;
}
//--------------------------------------------------------------------------void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
if (Shape1->Top>=280)
{
contreb++;
Label4->Caption=contreb;
altura=altura*0.70;
Label2->Caption=altura;
Shape1->Top=64*altini/altura;
Shape1->Left= Shape1->Left+1;
if(Shape1->Top>=280)
Shape1->Top=280;
if (altura<=0.5)
{Shape1->Top=280;
Timer2->Enabled =false;
}
}
if (Shape1->Top<280)
{Shape1->Top=Shape1->Top+3;
Shape1->Left=Shape1->Left+1;
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::FormCreate(TObject *Sender)
{
AnsiString cadena = "";
if (InputQuery("Caida libre", "Digite el valor de la altura", cadena))
{altura=StrToInt(cadena);
altini=altura;
Label2->Caption=altura;
Timer1->Enabled=true;}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Timer3Timer(TObject *Sender)
{
StatusBar1->Panels->Items[3]->Text = Now().TimeString();
StatusBar1->Panels->Items[1]->Text = Now().DateString();
}
//---------------------------------------------------------------------------

TALLER DE C++ BUILDER USO DEL COMPONENTE STATUSBAR


COMPONENTES UTILIZADOS:
DE LA PALETA STANDARD:
3 GroupBox
9 RadioButtons
1 Button
DE LA PALETA WIN32
1 StatusBar
DE LA PALETA SYSTEM:
1 Timer

//--------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
int PosXIni, PosYIni; // Declare at the top of the forms
Word Agno, NMes, Dia, Hora, Min, Seg, MSeg;
bool bandera1=true,bandera2=false,bandera3=false;
//--------------------------------------------------------------------------#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if (Shift.Contains(ssLeft))
{StatusBar1->Panels->Items[0]->Text = "X Inicial: " + IntToStr(PosXIni);
StatusBar1->Panels->Items[1]->Text = "Y Inicial: " + IntToStr(PosYIni);
StatusBar1->Panels->Items[2]->Text = "X Final: " + IntToStr(X);
StatusBar1->Panels->Items[3]->Text = "Y Final: " + IntToStr(Y);
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::FormCreate(TObject *Sender)
{
AnsiString mes,dias;
TDateTime actual = Date();
TDateTime DiaSem = Date();
StatusBar1->Panels->Add();
StatusBar1->Panels->Items[0]->Width=80;
StatusBar1->Panels->Items[0]->Text="";
StatusBar1->Panels->Add();
StatusBar1->Panels->Items[1]->Width=80;
StatusBar1->Panels->Items[1]->Bevel=pbRaised;
StatusBar1->Panels->Items[1]->Text="";
StatusBar1->Panels->Add();
StatusBar1->Panels->Items[2]->Width=80;
StatusBar1->Panels->Items[2]->Text="";
StatusBar1->Panels->Add();
StatusBar1->Panels->Items[3]->Width=80;
StatusBar1->Panels->Items[3]->Bevel=pbRaised;
StatusBar1->Panels->Items[3]->Text="";
DecodeDate(actual, Agno, NMes, Dia);
switch(NMes)
{

case 1: mes="Enero";
break;
case 2: mes="Febrero";
break;
case 3: mes="Marzo";
break;
case 4: mes="Abril";
break;
case 5: mes="Mayo";
break;
case 6: mes="Junio";
break;
case 7: mes="Julio";
break;
case 8: mes="Agosto";
break;
case 9: mes="Septiembre";
break;
case 10: mes="Octubre";
break;
case 11: mes="Noviembre";
break;
case 12: mes="Diciembre";
break;
}
switch(DiaSem.DayOfWeek())
{
case 1: mes="Domingo";
break;
case 2: dias="Lunes";
break;
case 3: dias="Martes";
break;
case 4: dias="Miercoles";
break;
case 5: dias="Jueves";
break;
case 6: dias="Viernes";
break;
case 7: dias="Sabado";
break;
}
StatusBar1->Panels->Add();
StatusBar1->Panels->Items[4]->Width=280;
StatusBar1->Panels->Items[4]->Text="Hoy es " + dias+ " " + IntToStr(Dia) + " del Mes " + mes + " del Ao "+ IntToStr(Agno);
StatusBar1->Panels->Add();
StatusBar1->Panels->Items[5]->Width=150;
StatusBar1->Panels->Items[5]->Bevel=pbRaised;
}
//--------------------------------------------------------------------------void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
PosXIni = X;
PosYIni = Y;
}
//--------------------------------------------------------------------------void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
if (RadioButton1->Checked)
Form1->Canvas->Brush->Color = clRed;
if (RadioButton2->Checked)
Form1->Canvas->Brush->Color = clYellow;
if (RadioButton3->Checked)
Form1->Canvas->Brush->Color = clBlue;

if (RadioButton4->Checked)
Form1->Canvas->Brush->Style = bsDiagCross;
if (RadioButton5->Checked)
Form1->Canvas->Brush->Style = bsFDiagonal;
if (RadioButton6->Checked)
Form1->Canvas->Brush->Style = bsCross;
if (bandera1)
{
Form1->Canvas->Rectangle(PosXIni, PosYIni, X, Y);
StatusBar1->Panels->Items[0]->Text = "";
StatusBar1->Panels->Items[1]->Text = "";
StatusBar1->Panels->Items[2]->Text = "";
StatusBar1->Panels->Items[3]->Text = "";
}
if (bandera2)
{
Form1->Canvas->Ellipse(PosXIni, PosYIni, X, Y);
StatusBar1->Panels->Items[0]->Text = "";
StatusBar1->Panels->Items[1]->Text = "";
StatusBar1->Panels->Items[2]->Text = "";
StatusBar1->Panels->Items[3]->Text = "";
}
if (bandera3)
{
Form1->Canvas->MoveTo(PosXIni, PosYIni);
Form1->Canvas->LineTo(X, Y);
StatusBar1->Panels->Items[0]->Text = "";
StatusBar1->Panels->Items[1]->Text = "";
StatusBar1->Panels->Items[2]->Text = "";
StatusBar1->Panels->Items[3]->Text = "";
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form1->Repaint();
}
//--------------------------------------------------------------------------void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
TDateTime actual = Now();
DecodeTime(actual, Hora, Min, Seg, MSeg);
StatusBar1->Panels->Items[5]->Text="La Hora es " + IntToStr(Hora) + " : "+
IntToStr(Min)+ " : "+IntToStr(Seg);
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button5Click(TObject *Sender)
{
bandera1=true;
bandera2=false;
bandera3=false;
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button3Click(TObject *Sender)
{
bandera1=false;
bandera2=false;
bandera3=true;
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button4Click(TObject *Sender)
{
bandera1=false;
bandera2=true;
bandera3=false;
}

//--------------------------------------------------------------------------void __fastcall TForm1::RadioButton7Click(TObject *Sender)


{
bandera1=true;
bandera2=false;
bandera3=false;
}
//--------------------------------------------------------------------------void __fastcall TForm1::RadioButton8Click(TObject *Sender)
{
bandera1=false;
bandera2=true;
bandera3=false;
}
//--------------------------------------------------------------------------void __fastcall TForm1::RadioButton9Click(TObject *Sender)
{
bandera1=false;
bandera2=false;
bandera3=true;
}
//--------------------------------------------------------------------------TALLER DE C++ BUILDER COMPONENTE ANIMATE
COMPONENTES USADOS:
DE LA PALETA STANDARD:
3 Buttons
DE LA PALETA WIN32:
1 Animate
Propiedades principales del componente Animate.
FileName =especifica la ruta de acceso del archivo de video .avi
StartFrame=1 // especifica la primera vista que se visualiza cuando se activa el control.
StopFrame=71 // especifica la ultima vista del video. (cuando es 0 se ejecuta hasta el final)
Repetitions=0 // especifica el numero de veces que se va a repetir el video (un valor 0 indica indefinido)
//--------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//--------------------------------------------------------------------------#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender)
{
Animate1->FileName ="C:/Archivos de programa/Borland/CBuilder5/Examples/MFC/General/Cmnctrls/dillo.avi"; // si el archivo
esta en otra ruta, cmbiela por esa ruta.
Animate1->Play(0,71,0);
}//--------------------------------------------------------------------------void __fastcall TForm1::Button2Click(TObject *Sender)
{
Animate1->Stop();
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button3Click(TObject *Sender)
{
Close();

}
//--------------------------------------------------------------------------TALLER DE C++ BUILDER

En la forma 1
5 StaticText
1 Timer
1 BitButton
Modificar las siguientes propiedades en tiempo de diseo

object Form1: TForm1


Color = clWhite
object StaticText1: TStaticText
Left = 84
Top = 152
Width = 18
Height = 33
BorderStyle = sbsSunken
Caption = 'J'
Color = clWhite
object StaticText2: TStaticText
Left = 224
Top = 52
Width = 21
Height = 33
BorderStyle = sbsSunken
Caption = 'A'
Color = clWhite
object StaticText3: TStaticText
Left = 264
Top = 152
Width = 12
Height = 33
BorderStyle = sbsSunken
Caption = 'I'
Color = clWhite
object StaticText4: TStaticText
Left = 396
Top = 152
Width = 23
Height = 33
BorderStyle = sbsSunken
Caption = 'R'
Color = clWhite
object StaticText5: TStaticText
Left = 336
Top = 252
Width = 25
Height = 33
BorderStyle = sbsSunken
Caption = 'O'
Color = clWhite
object BitBtn1: TBitBtn
Caption = 'Siguiente'
Visible = False

object Timer1: TTimer


Interval = 150
/--------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"//incluir para enlazare con la forma 2
#include <stdlib.h>//incluir
#include <stdio.h>//incluir
#include <time.h>//incluir
//--------------------------------------------------------------------------#pragma package(smart_init)
#pragma resource "*.dfm"
int i = 0;
TForm1 *Form1;
//--------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
randomize();
if (i==100)
{
Timer1->Enabled =false;
BitBtn1->Visible =true;
}
else
{
StaticText1->Left=StaticText1->Left+1;
StaticText1->Font->Color=TColor (random(2147483647));
StaticText2->Top=StaticText2->Top+1;
StaticText2->Font->Color=TColor (random(2147483647));
StaticText3->Font->Color=TColor (random(2147483647));
StaticText4->Left=StaticText4->Left-1;
StaticText4->Font->Color=TColor (random(2147483647));
StaticText5->Top=StaticText5->Top-1;
StaticText5->Font->Color=TColor (random(2147483647));
i++;
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Form2->Show(); //muestra la segunda forma
}
//--------------------------------------------------------------------------void __fastcall TForm1::FormCreate(TObject *Sender)
{
}
//---------------------------------------------------------------------------

En la forma 2

1 RichEdit
1 BitButton
Modificar las siguientes propiedades en tiempo de diseo

object RichEdit1: TRichEdit


Left = 24
Top = 40
Width = 473
Height = 249
Lines.Strings = ('')
ReadOnly = True
ScrollBars = ssVertical
TabOrder = 0
//--------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop
#include "Unit1.h" //incluir esta librera para enlazarse con la forma 1
#include "Unit2.h"
#include <string.h> //incluir
//--------------------------------------------------------------------------#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//--------------------------------------------------------------------------__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------void __fastcall TForm2::FormClose(TObject *Sender, TCloseAction &Action)
{
Form1->Close(); // cierra la forma 1
}
//--------------------------------------------------------------------------void __fastcall TForm2::FormCreate(TObject *Sender)
{
/*aqu se carga el archivo en el RichEdit. El archivo debe tener extension rtf y la ubicacin en el disco
depende de su computador.*/
char const *Path = "C:\\Mis documentos\\Algoritmos de planificacion.rtf";
RichEdit1->Lines->LoadFromFile(Path);
}
//--------------------------------------------------------------------------void __fastcall TForm2::BitBtn1Click(TObject *Sender)
{
if (RichEdit1->SelLength)
{
int Size = RichEdit1->SelLength; //captura la longitud del texto seleccionado del RichEdit1
Size++;
//Adiciona un espacio para el carcter nulo
char *Buffer = new char[Size]; //Crea una variable dinmica Buffer
RichEdit1->GetSelTextBuf(Buffer,Size);//captura el texto seleccionado y lo copia en Buffer
//aqu pregunta por las direcciones que hay en su documento
if (strstr(Buffer,"http://www.cecar.edu.co")!=NULL)
{
WinExec("C:\\Archivos de programa\\Internet Explorer\\iexplore.exe http://www.cecar.edu.co",SW_SHOW);//
abre el explorador de internet

}
delete Buffer; //borra lo que contiene la variable
}
else
ShowMessage("No ha seleccionado link");
}
//---------------------------------------------------------------------------

Curso de C++ Builder


Sonidos

En este ejemplo se crear un formulario que contendr un componente MediaPlayer que servir para reproducir ficheros .WAV.
Utilizaremos la gestin de excepciones para controlar la correcta ejecucin del programa.
Crear y configurar el proyecto.
En primer lugar, crear el directorio Sonidos. y, si procede, una nueva aplicacin ( File | New Application).
Guardar proyecto: Salvar Unit1.cpp como Ppal.cpp y Project1.bpr como Sonidos.bpr
Configurar el formulario.
Configurar el formulario para que tenga el aspecto mostrado en la figura siguiente:

Cambiar el ttulo (Caption por Sonidos) y aadir los siguientes componentes:


Cuatro botones normales (Button) llamados (Name): LogoffBtn, MSSoundBtn, ErrorBtn y OtrosBtn con el texto (Caption)
indicado en la figura anterior.
Un botn BitBtn para terminar.
Un control MediaPlayer (pestaa System) no visible (Visible=false).
Un cuadro de dilogo OpenDialog (pestaa Dialogs).
Escribir los gestores de eventos asociados a la pulsacin de los botones.
La gestin de las interrupciones capturadas se realiza con una funcin comn cuyo prototipo es:
void Error (AnsiString & name, Exception & e); que se escribir por encima de todas las funciones de Ppal.cpp. As, los gestores
de los eventos OnClick asociados a los botones sern los siguientes:
//-------------------------------------------------------------void __fastcall TForm1::LogoffBtnClick(TObject *Sender)
{
MediaPlayer1->FileName = "C:\\WINDOWS\\MEDIA\\Logoff.wav";
try {
MediaPlayer1->Open();
MediaPlayer1->Wait = true;
MediaPlayer1->Play();
MediaPlayer1->Close();
}
catch (Exception & e) {
Error (MediaPlayer1->FileName, e);
}
}
//-------------------------------------------------------------void __fastcall TForm1::MSSoundBtnClick(TObject *Sender)
{
MediaPlayer1->FileName = "C:\\WINDOWS\\MEDIA\\The Microsoft Sound.wav";

try {
MediaPlayer1->Open();
MediaPlayer1->Wait = true;
MediaPlayer1->Play();
MediaPlayer1->Close();
}
catch (Exception & e) {
Error (MediaPlayer1->FileName, e);
}
}
//-------------------------------------------------------------void __fastcall TForm1::ErrorBtnClick(TObject *Sender)
{
MediaPlayer1->FileName = "C:\\WINDOWS\\MEDIA\\NoEsta.wav";
try {
MediaPlayer1->Open();
MediaPlayer1->Wait = true;
MediaPlayer1->Play();
MediaPlayer1->Close();
}
catch (Exception & e) {
Error (MediaPlayer1->FileName, e);
}
}
//-------------------------------------------------------------void __fastcall TForm1::OtrosBtnClick(TObject *Sender)
{
OpenDialog1->Title = "Abrir un fichero de sonido";
OpenDialog1->Filter = "Ficheros de sonido (*.wav) | *.WAV";
OpenDialog1->InitialDir = "C:\\WINDOWS";
if (OpenDialog1->Execute())
{
MediaPlayer1->FileName = OpenDialog1->FileName;
try {
MediaPlayer1->Open();
MediaPlayer1->Wait = true;
MediaPlayer1->Play();
MediaPlayer1->Close();
}
catch (Exception & e) {
Error (MediaPlayer1->FileName, e);
}
} // if (OpenDialog1->Execute())
}
//-------------------------------------------------------------void Error (AnsiString & name, Exception & e)
{
ShowMessage ("Fallo en la reproduccin del fichero de sonido: " + name +
"\nExcepcin: " + AnsiString(e.ClassName()) +
"\nDetalles: " + AnsiString(e.Message));
}
//-------------------------------------------------------------Cuando se ejecuta este programa, si se pulsa en el botn Error (se supone que el fichero de sonido que se quiere reproducir no
existe) aparece la siguiente ventana:

En cambio, si se quiere reproducir un fichero cuyo formato no sea .wav aparece la siguiente ventana:

TALLER DE C++ BUILDER COMPONENTE ANIMATE


COMPONENTES USADOS:
DE LA PALETA STANDARD:
3 Buttons
DE LA PALETA WIN32:
1 Animate
Propiedades principales del componente Animate.
FileName =especifica la ruta de acceso del archivo de video .avi
StartFrame=1 // especifica la primera vista que se visualiza cuando se activa el control.
StopFrame=71 // especifica la ultima vista del video. (cuando es 0 se ejecuta hasta el final)
Repetitions=0 // especifica el numero de veces que se va a repetir el video (un valor 0 indica indefinido)
//--------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//--------------------------------------------------------------------------#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender)
{
Animate1->FileName ="C:/Archivos de programa/Borland/CBuilder5/Examples/MFC/General/Cmnctrls/dillo.avi"; // si el archivo
esta en otra ruta, cmbiela por esa ruta.
Animate1->Play(0,71,0);
}//--------------------------------------------------------------------------void __fastcall TForm1::Button2Click(TObject *Sender)
{
Animate1->Stop();
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button3Click(TObject *Sender)
{
Close();
}

Taller de Rompecabezas
1 Panel, 15 Buttons
object Panel1: TPanel
Left = 72
Top = 24
Width = 185
Height = 185
BorderStyle = bsSingle
object Button2: TButton
Left = 48
Top = 8
Width = 41
Height = 41
Caption = '2'
object Button3: TButton
Left = 88
Top = 8
Width = 41
Height = 41
Caption = '3'
object Button4: TButton
Left = 128
Top = 8
Width = 41
Height = 41
Caption = '4'
object Button5: TButton
Left = 8
Top = 48
Width = 41
Height = 41
Caption = '5'
object Button6: TButton
Left = 48
Top = 48
Width = 41
Height = 41
Caption = '6'
object Button7: TButton
Left = 88
Top = 48
Width = 41
Height = 41
Caption = '7'
object Button8: TButton
Left = 128
Top = 48
Width = 41
Height = 41
Caption = '8'
object Button9: TButton
Left = 8
Top = 88
Width = 41
Height = 41
Caption = '9'
object Button10: TButton

Left = 48
Top = 88
Width = 41
Height = 41
Caption = '10'
object Button11: TButton
Left = 88
Top = 88
Width = 41
Height = 41
Caption = '11'
object Button12: TButton
Left = 128
Top = 88
Width = 41
Height = 41
Caption = '12'
object Button13: TButton
Left = 8
Top = 128
Width = 41
Height = 41
Caption = '13'
object Button14: TButton
Left = 48
Top = 128
Width = 41
Height = 41
Caption = '14'
object Button15: TButton
Left = 88
Top = 128
Width = 41
Height = 41
Caption = '15'
object Button1: TButton
Left = 8
Top = 8
Width = 41
Height = 41
Caption = '1'
//--------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//--------------------------------------------------------------------------#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int i,j,k;
int m[4][4];
//--------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)


{
k=0;
for (i=0;i<=3;i++)
for (j=0;j<=3;j++)
{
k++;
m[i][j]=k;
}
m[3][3]=0;
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button15Click(TObject *Sender)
{
k=StrToInt(Button15->Caption);
for (i=0;i<=3;i++)
for (j=0;j<=3;j++)
{
if (m[i][j]==k)//encuentra el dato
{
if (m[i-1][j]==0&&i>0)
{
m[i-1][j]=k;
m[i][j]=0;
Button15->Top=Button15->Top-Button15->Height;
i=j=4;
}
else
if (m[i+1][j]==0&&i<3)
{
m[i+1][j]=k;
m[i][j]=0;
Button15->Top=Button15->Top+Button15->Height;
i=j=4;
}
else
if (m[i][j-1]==0&&j>0)
{
m[i][j-1]=k;
m[i][j]=0;
Button15->Left=Button15->Left-Button15->Width;
i=j=4;
}
else
if (m[i][j+1]==0&&j<3)
{
m[i][j+1]=k;
m[i][j]=0;
Button15->Left=Button15->Left+Button15->Width;
i=j=4;
}
}
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button12Click(TObject *Sender)
{
k=StrToInt(Button12->Caption);
for (i=0;i<=3;i++)
for (j=0;j<=3;j++)
{
if (m[i][j]==k)//encuentra el dato
{
if (m[i-1][j]==0&&i>0)

{
m[i-1][j]=k;
m[i][j]=0;
Button12->Top=Button12->Top-Button12->Height;
i=j=4;
}
else
if (m[i+1][j]==0&&i<3)
{
m[i+1][j]=k;
m[i][j]=0;
Button12->Top=Button12->Top+Button12->Height;
i=j=4;
}
else
if (m[i][j-1]==0&&j>0)
{
m[i][j-1]=k;
m[i][j]=0;
Button12->Left=Button12->Left-Button12->Width;
i=j=4;
}
else
if (m[i][j+1]==0&&j<3)
{
m[i][j+1]=k;
m[i][j]=0;
Button12->Left=Button12->Left+Button12->Width;
i=j=4;
}
}
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button11Click(TObject *Sender)
{
k=StrToInt(Button11->Caption);
for (i=0;i<=3;i++)
for (j=0;j<=3;j++)
{
if (m[i][j]==k)//encuentra el dato
{
if (m[i-1][j]==0&&i>0)
{
m[i-1][j]=k;
m[i][j]=0;
Button11->Top=Button11->Top-Button11->Height;
i=j=4;
}
else
if (m[i+1][j]==0&&i<3)
{
m[i+1][j]=k;
m[i][j]=0;
Button11->Top=Button11->Top+Button11->Height;
i=j=4;
}
else
if (m[i][j-1]==0&&j>0)
{
m[i][j-1]=k;
m[i][j]=0;
Button11->Left=Button11->Left-Button11->Width;
i=j=4;
}

else
if (m[i][j+1]==0&&j<3)
{
m[i][j+1]=k;
m[i][j]=0;
Button11->Left=Button11->Left+Button11->Width;
i=j=4;
}
}
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button14Click(TObject *Sender)
{
k=StrToInt(Button14->Caption);
for (i=0;i<=3;i++)
for (j=0;j<=3;j++)
{
if (m[i][j]==k)//encuentra el dato
{
if (m[i-1][j]==0&&i>0)
{
m[i-1][j]=k;
m[i][j]=0;
Button14->Top=Button14->Top-Button14->Height;
i=j=4;
}
else
if (m[i+1][j]==0&&i<3)
{
m[i+1][j]=k;
m[i][j]=0;
Button14->Top=Button14->Top+Button14->Height;
i=j=4;
}
else
if (m[i][j-1]==0&&j>0)
{
m[i][j-1]=k;
m[i][j]=0;
Button14->Left=Button14->Left-Button14->Width;
i=j=4;
}
else
if (m[i][j+1]==0&&j<3)
{
m[i][j+1]=k;
m[i][j]=0;
Button14->Left=Button14->Left+Button14->Width;
i=j=4;
}
}
}
}
//---------------------------------------------------------------------------

Вам также может понравиться