C++ Builder

La Isla Bonita



List of TImages
List of TImages


Description

This article shows how to create an array of TImages. Two TButtons are created. One to load the images (BMP graphics files) and another one to step through the pictures (Next Picture Button). All The components on the form are created dynamically.

ListOfPictures.cpp

#include <vcl.h>
#pragma hdrstop

#include "ListOfTImages.h"
//------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

const int DTop = 50;
const int DLeft = 0;
const int DIM_BUTTONS =  2;      //Number of buttons
const int DIM_IMAGES  = 10;      //Number of pictures
TImage*  Image1[DIM_IMAGES];
TButton* Button1[DIM_BUTTONS];

//----------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
      //Main Form's parameters initialization
    Width    = 800;
    Height   = 600;
    Color    = clBlack;
    Position = poScreenCenter;

      //Creation of two TButtons	
    for ( int i=0; i< DIM_BUTTONS; i++ )
    {
        Button1[i] = new TButton(this);
        Button1[i]->Parent = this;
        Button1[i]->Top    =  10;
        Button1[i]->Left   =  50 + i*205;
        Button1[i]->Width  =  200;
        Button1[i]->Height =  30;
        Button1[i]->Visible = true;

    }
    Button1[0]->Caption = "Load Pictures";
    Button1[1]->Caption = "Next Picture";
    Button1[0]->OnClick = LoadPicturesClick;  //Load Picture
    Button1[1]->OnClick = NextPictureClick;   //Next Picture

      //Creation of the List of TImages
    for ( int i=0; i< DIM_IMAGES; i++ )
    {
        Image1[i] = new TImage(this);
        Image1[i]->Parent = this;
        Image1[i]->Top    =   DTop;
        Image1[i]->Left   =   DLeft;
        Image1[i]->Width  =  Width - DLeft;
        Image1[i]->Height =  Height - DTop;
        Image1[i]->Visible = false;
    }
}
//----------------------------------------------------------------------

void __fastcall TForm1::LoadPicturesClick(TObject *Sender)
{
//Load Pictures Event Handler

   Image1[0]->Picture->LoadFromFile( "C:\\Graphics\\bmp\\pic1.bmp" );
   Image1[1]->Picture->LoadFromFile( "C:\\Graphics\\bmp\\pic2.bmp" );
   Image1[2]->Picture->LoadFromFile( "C:\\Graphics\\bmp\\pic3.bmp" );
   Image1[3]->Picture->LoadFromFile( "C:\\Graphics\\bmp\\pic4.bmp" );
   Image1[4]->Picture->LoadFromFile( "C:\\Graphics\\bmp\\pic5.bmp" );
   Image1[5]->Picture->LoadFromFile( "C:\\Graphics\\bmp\\pic6.bmp" );
   Image1[6]->Picture->LoadFromFile( "C:\\Graphics\\bmp\\pic7.bmp" );
   Image1[7]->Picture->LoadFromFile( "C:\\Graphics\\bmp\\pic8.bmp" );
   Image1[8]->Picture->LoadFromFile( "C:\\Graphics\\bmp\\pic9.bmp" );
   Image1[9]->Picture->LoadFromFile( "C:\\Graphics\\bmp\\pic10.bmp" );

   Image1[0]->Visible = true;
}
//----------------------------------------------------------------------

void __fastcall TForm1::NextPictureClick(TObject *Sender)
{
//Show Next Picture Event Handler

   for ( int i=0; i< DIM_IMAGES; i++ )
   {
       if ( !Image1[i] )
       {
           ShowMessage( "Image1[" + String(i) + "] not loaded." );
           return;
       }
   }

   static int i = 0;     //Counter
   i++;
   if (i >= DIM_IMAGES )
   {
       i = 0;            //Reset the counter.
   }

   //Make previous image invisible.
   if (i == 0 )
   {
       Image1[DIM_IMAGES-1]->Visible = false;
   }
   else
   {
       Image1[i-1]->Visible = false;
   }
   
   //Make the current image visible.
   Image1[i]->Visible = true;
}

ListOfTImages.h

#ifndef ListOfTImagesH
#define ListOfTImagesH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        void __fastcall LoadPicturesClick(TObject *Sender);
        void __fastcall NextPictureClick(TObject *Sender);

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

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