Transparent Flashing Bitmap

Transparent Flashing Bitmap

 

Introduction

This exercise shows how to use a transparent flashing bitmap. First, you must have a picture for the background. For this exercise, I used the following bitmap (a file with .bmp extension):

 

Small Bitmap

Second, you would need the bitmap that will be flashing. Obviously, it should be smaller than the one used as the background. For this exercise, I used the FunctionX logo on the right above. When creating such a picture, decide on the color that will be transparent because the compiler would need it.

Designing the Application

For this exercise, since we want the bitmap to flash, obviously you would place a Timer control on the form and double-click it to access its OnTimer event.

In the class definition, you can declare a TBitmap variable for each bitmap. Because the whole would need to be repainted on each event of the Timer but since we want to control the drawing, you can just declare a TBitmap object on which we will draw. Here is the header file of the form:

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    TTimer *Timer1;
    void __fastcall Timer1Timer(TObject *Sender);
private:
    Graphics::TBitmap *BG;
    Graphics::TBitmap *GameArea;
    Graphics::TBitmap *Flashing;	// User declarations
public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

If you don't plan to save the project, then remember the path where you saved each of the pictures you would need. If the project will be saved, it would be a good idea to idea the pictures in the same folder on which you are saving the other files. In all cases, the location(s) of the file will be used for the TBitmap::LoadFromFile() method that includes each picture.

When defining the transparent picture, make sure you specify its TBitmap::Transparent property as True.

In the OnTimer event, you first draw the background with the necessary picture, the you select a random location to position the flashing bitmap. These two pictures should be placed on the drawing area as defined above. Then include this drawing area on the canvas of the form. Here is how we could do it:

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    GameArea         = new Graphics::TBitmap;
    GameArea->Width  = this->ClientWidth;
    GameArea->Height = this->ClientHeight;

    BG = new Graphics::TBitmap;
    BG->LoadFromFile("bg.bmp");

    // Resize the form to the dimensions of the background
    Form1->ClientWidth  = BG->Width;
    Form1->ClientHeight = BG->Height;

    Flashing = new Graphics::TBitmap;
    Flashing->LoadFromFile("Flasher.bmp");
    Flashing->Transparent = True;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    GameArea->Canvas->Draw(0, 0, BG);
    GameArea->Canvas->Draw( random(this->ClientWidth),
                            random(this->ClientHeight),
                            Flashing );
    this->Canvas->Draw(0, 0, GameArea);
}
//---------------------------------------------------------------------------
 

Copyright © 2003-2007 FunctionX, Inc.