Using Transparent Controls (C++ Builder 3.0)

Take the following steps to add transparent controls, with special effects. The transparent controls are added as child controls so that they will scroll with the parent image.

1. Start with the project that you created in Loading and Displaying an Image.

2. Add two LEAD controls, which you will set to be transparent at run time. (At design time, you can make the controls easier to see by setting the BackColor property of each control to a different color.) Add the controls as follows:

a. Add a control named LEAD2 as a child control to LEAD1. In other words, draw it inside the LEAD1 control.

b. Add a control named LEAD3 as a child control to LEAD2. In other words, draw it inside the LEAD2 control.

3. Add a command button to your form and name it as follows:

Name

Caption

Trans

Transparent

4. Code the Trans button's click procedure as follows:

void __fastcall TForm1::TransClick(TObject *Sender)
{
   LEAD1->EnableMethodErrors = False;
   LEAD1->AutoRepaint = False;
   LEAD2->AutoRepaint = False;
   LEAD3->AutoRepaint = False;
   //Make Lead2 and Lead3 Transparent
   LEAD2->Transparent = True;
   LEAD3->Transparent = True;
   //Give each control a different effect
   LEAD1->PaintEffect = EFX_EFFECT_WIPE_RECTANGLE_OUT;
   LEAD2->PaintEffect = EFX_EFFECT_WIPE_RECTANGLE_IN;
   LEAD3->PaintEffect = EFX_EFFECT_CHECKERBOARD_T_TO_B_THEN_T_TO_B;
   LEAD1->EffectDelay = 20;
   LEAD2->EffectDelay = 20;
   LEAD3->EffectDelay = 20;
   LEAD1->EffectGrain = 1;
   LEAD2->EffectGrain = 5;
   LEAD3->EffectGrain = 1;
   LEAD1->ScaleMode = 3;
   LEAD2->ScaleMode = 3;
   LEAD3->ScaleMode = 3;

   //Position Lead2 and Lead3 inside Lead1
   LEAD2->Width = LEAD1->Width / 2;
   LEAD2->Height = LEAD1->Height / 2;
   LEAD2->Left = (LEAD1->Width - LEAD2->Width) / 2;
   LEAD2->Top = (LEAD1->Height - LEAD2->Height) / 2;
   LEAD3->Top = 0;
   LEAD3->Left = 0;
   LEAD3->Width = LEAD2->Width;
   LEAD3->Height = LEAD2->Height;
   //We will be drawing into the bitmaps
   LEAD2->DrawPersistence = True;
   LEAD3->DrawPersistence = True;

   //First, we need some bitmaps
   LEAD2->CreateBitmap(LEAD2->ScaleWidth, LEAD3->ScaleHeight, 24);
   LEAD3->CreateBitmap(LEAD3->ScaleWidth, LEAD3->ScaleHeight, 24);
   //Fill them with black, which we will make transparent
   LEAD2->Fill(clBlack);
   LEAD3->Fill(clBlack);
   LEAD2->BitmapEnableTransparency = True;
   LEAD3->BitmapEnableTransparencyr = True;
   LEAD2->BitmapTransparentColor = clBlack;
   LEAD3->BitmapTransparentColor = clBlack;

   //Draw a shape into Lead2
   //Shape background
   LEAD2->ShapeBackgroundStyle = EFX_BACKSTYLE_TRANSLUCENT;
   //Shape location
   LEAD2->ShapeTop = 0;
   LEAD2->ShapeLeft = 0;
   LEAD2->ShapeWidth = LEAD2->BitmapWidth;
   LEAD2->ShapeHeight = LEAD2->BitmapHeight;
   //Shape border
   LEAD2->ShapeBorderColor = clFuchsia;
   LEAD2->ShapeBorderStyle = EFX_BORDERSTYLE_SOLID;
   LEAD2->ShapeBorderThickness = 5;
   LEAD2->PatternStyle = EFX_PATTERN_TRANSPARENT;

   //Draw the shape
   LEAD2->DrawShape(EFX_SHAPE_STAR4, 0);
   //Draw text into Lead3
   //Specify the text, font, and styles
   LEAD3->Font->Name = StringToOleStr("Arial");
   LEAD3->Font->Size = 40;
   LEAD3->TextAngle = 450;
   LEAD3->DrawFontColor = clBlue;
   LEAD3->TextStyle = EFX_TEXTSTYLE_NORMAL;
   LEAD3->TextAlign = EFX_TEXTALIGN_HCENTER_VCENTER;
   //Set the location for the text
   LEAD3->TextTop = 0;
   LEAD3->TextLeft = 0;
   LEAD3->TextWidth = LEAD3->BitmapWidth;
   LEAD3->TextHeight = LEAD3->BitmapHeight;

   //Set the properties for drawing a rectangle behind the text.
   LEAD3->DrawPenStyle = DRAWPENSTYLE_SOLID;
   LEAD3->DrawPenColor = clAqua;
   LEAD3->DrawMode = DRAWMODE_COPY_PEN;
   //Draw the rectangle, then draw the text.
   LEAD3->DrawRectangle(0, 0, LEAD3->BitmapWidth, LEAD3->BitmapHeight);
   LEAD3->DrawText(StringToOleStr("Text"), 0);
  //Load an image into Lead1}
   LEAD1->Load(StringToOleStr("e:\\LTWIN12x\\images\\image1.cmp"), 0, 0, 1);
   LEAD1->PaintSizeMode = PAINTSIZEMODE_ZOOM;
   LEAD1->PaintZoomFactor = 150;
   LEAD1->ForceRepaint();

   //Turn off the effects.
   LEAD1->PaintEffect = EFX_EFFECT_NONE;
   LEAD2->PaintEffect = EFX_EFFECT_NONE;
   LEAD3->PaintEffect = EFX_EFFECT_NONE;
}

4. Run your program to test it.