Slide Show |
This exercise shows how to use a series of bitmaps to create a slide show. The pictures simply replace each other on the canvas as a timer fires fires its event. The pictures are placed in an array and not selected at random. First, you must have the necessary pictures. For this exercise, I used the following bitmaps (files with .bmp extension): |
In the class definition of the form, declare an array of TBitmap objects and specify the number of pictures you will use. To initialize the picture when the application starts, you would use the OnCreate event of the form. To change the pictures as time goes by, you would need a Timer control and its OnTimer event. For this exercise, I left the timer interval at 1000. Because this is a slide show, we will implement our pictures on a TImage control. You can place it anywhere on the form. Since the pictures will cover the whole form, you can set the Align property of the TImage object to alClient. Here is the header file of the form: //--------------------------------------------------------------------------- #ifndef MainH #define MainH //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <ExtCtrls.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TImage *Image1; TTimer *Timer1; void __fastcall FormCreate(TObject *Sender); void __fastcall Timer1Timer(TObject *Sender); private: Graphics::TBitmap *Slide[6]; // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif In the constructor of the form, initialize each picture. Then, in the OnCreate event, specify that the form's width will follow the width of the first picture. In the OnTimer event, start with the first picture and, every time the control fires this event, select the next picture. Here is how we could do it: //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Main.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Slide[0] = new Graphics::TBitmap; Slide[0]->LoadFromFile("Bitmap0.bmp"); Slide[1] = new Graphics::TBitmap; Slide[1]->LoadFromFile("Bitmap1.bmp"); Slide[2] = new Graphics::TBitmap; Slide[2]->LoadFromFile("Bitmap2.bmp"); Slide[3] = new Graphics::TBitmap; Slide[3]->LoadFromFile("Bitmap3.bmp"); Slide[4] = new Graphics::TBitmap; Slide[4]->LoadFromFile("Bitmap4.bmp"); Slide[5] = new Graphics::TBitmap; Slide[5]->LoadFromFile("Bitmap5.bmp"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { Form1->Width = Slide[0]->Width; Form1->Height = Slide[0]->Height; } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) { static int SlideNumber = 0; if( SlideNumber <= 5 ) { Image1->Canvas->Draw(0, 0, Slide[SlideNumber]); SlideNumber++; } else SlideNumber = 0; } //---------------------------------------------------------------------------
|
|
||
Copyright © 2003-2007 FunctionX, Inc. | ||
|