Using Transparent Controls (Delphi)
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 and code its click event as follows: |
procedure TForm1.Button1Click(Sender: TObject);
begin
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_WIPE4_R_B_L_B;
Lead1.EffectDelay := 20;
Lead2.EffectDelay := 10;
Lead3.EffectDelay := 10;
Lead1.EffectGrain := 1;
Lead2.EffectGrain := 5;
Lead3.EffectGrain := 1;
{Position Lead2 and Lead3 inside Lead1}
Lead2.Width := Round(Lead1.Width / 2);
Lead2.Height := Round(Lead1.Height / 2);
Lead2.Left := Round((Lead1.Width - Lead2.Width) / 2);
Lead2.Top := Round((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.Width, Lead3.Height, 24);
Lead3.CreateBitmap(Lead3.Width, Lead3.Height, 24);
{Fill them with black, which we will make transparent}
Lead2.Fill(RGB(0, 0, 0));
Lead3.Fill(RGB(0, 0, 0));
Lead2.UseTransparentColor := True;
Lead3.UseTransparentColor := True;
Lead2.TransparentColor := RGB(0, 0, 0);
Lead3.TransparentColor := RGB(0, 0, 0);
{Draw a shape into Lead2}
{Shape background}
Lead2.ShapeBackgroundStyle := sbsTranslucent;
{Shape location}
Lead2.ShapeTop := 0;
Lead2.ShapeLeft := 0;
Lead2.ShapeWidth := Lead2.BitmapWidth;
Lead2.ShapeHeight := Lead2.BitmapHeight;
{Shape border}
Lead2.ShapeBorderColor := RGB(255, 0, 0);
Lead2.ShapeBorderStyle := sbrSolid;
Lead2.ShapeBorderThickness := 5;
Lead2.PatternStyle := epsTransparent;
{Draw the shape}
Lead2.DrawShape(EFX_SHAPE_STAR4, 0);
{Draw text into Lead3}
{Specify the text, font, and styles}
Lead3.TextFont.Name := 'Arial';
Lead3.TextFont.Size := 40;
Lead3.TextAngle := 450;
Lead3.TextFont.Color := RGB(0, 0, 255); {Blue}
Lead3.TextStyle := etsNormal;
Lead3.TextAlign := etaHcenterVcenter;
{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 := psSolid;
Lead3.DrawPenColor := RGB(255, 255, 0);
Lead3.DrawMode := pmCopy;
{Draw the rectangle, then draw the text.}
Lead3.DrawRectangle(0, 0, Lead3.BitmapWidth, Lead3.BitmapHeight);
Lead3.DrawTextStr('Text', 0);
{Load an image into Lead1}
Lead1.Load('c:\lead\images\image1.cmp', 0, 0, 1);
Lead1.PaintSizeMode := smZoom;
Lead1.PaintZoomFactor := 150;
Lead1.ForceRepaint();
{Turn off the effects.}
Lead1.PaintEffect := EFX_EFFECT_NONE;
Lead2.PaintEffect := EFX_EFFECT_NONE;
Lead3.PaintEffect := EFX_EFFECT_NONE;
end;
4. |
Run your program to test it. |