Using Transparent Controls (C++ 4.0 and later)

Take the following steps to add transparent controls, with special effects. The transparent controls are added as nested child controls.

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

2. Edit the TUTORDLG.H file, and add the following as protected members to the CTutorDlg class:

CLead   m_Lead2;
CLead   m_Lead3;

3. Also in the TUTORDLG.H file, include the FONT.H file by adding the following statement at the beginning of the file:

#include "font.h"

4. Update the CTutorDlg::OnInitDialog function to create the m_Lead2 and m_Lead3 controls at run time. To do so, add the following code just before the last return statement:

const WCHAR _szLicString[] =
L"LEADTOOLS OCX Copyright (c) 1997 LEAD Technologies, Inc.";
BSTR lpLic = SysAllocString(_szLicString);
m_Lead2.Create("", 0, CRect(0,0,1,1), &m_Lead1, 0,NULL,FALSE,lpLic);
m_Lead2.ShowWindow(SW_SHOW);
m_Lead3.Create("", 0, CRect(0,0,1,1), &m_Lead2, 0,NULL,FALSE,lpLic);
m_Lead3.ShowWindow(SW_SHOW);
SysFreeString(lpLic);

5. Add a command button, and code its click event as follows:

void CTutorDlg::OnButton1() 
{
   // Avoid extra repaints.
   m_Lead1.SetAutoRepaint(FALSE);
   m_Lead2.SetAutoRepaint(FALSE);
   m_Lead3.SetAutoRepaint(FALSE);

   // Make m_Lead2 and m_Lead3 transparent.
   m_Lead2.SetTransparent(TRUE);
   m_Lead3.SetTransparent(TRUE);

   // Give each control a different effect.
   m_Lead1.SetPaintEffect(EFX_EFFECT_WIPE_RECTANGLE_OUT);
   m_Lead2.SetPaintEffect(EFX_EFFECT_WIPE_RECTANGLE_IN);
   m_Lead3.SetPaintEffect(EFX_EFFECT_WIPE4_R_B_L_B);

   m_Lead1.SetEffectDelay(20);
   m_Lead2.SetEffectDelay(20);
   m_Lead3.SetEffectDelay(20);
   m_Lead1.SetEffectGrain(1);
   m_Lead2.SetEffectGrain(5);
   m_Lead3.SetEffectGrain(1);
   m_Lead1.SetScaleMode(3);
   m_Lead2.SetScaleMode(3);
   m_Lead3.SetScaleMode(3);

   // Position m_Lead2 and m_Lead3 inside m_Lead1.
   float LabelWidth = m_Lead1.GetScaleWidth() / 2;
   float LabelHeight = m_Lead1.GetScaleHeight() / 2;
   float LabelLeft = LabelWidth / 2;
   float LabelTop = LabelHeight / 2;
   m_Lead2.MoveWindow((int)LabelLeft,(int)LabelTop,(int)LabelWidth,(int)LabelHeight);
   m_Lead3.MoveWindow(0,0,(int)LabelWidth,(int)LabelHeight);

   // Update the measurements that we use, based on the client area.
   LabelWidth = m_Lead2.GetScaleWidth();
   LabelHeight = m_Lead2.GetScaleHeight();

// We will be drawing into the bitmaps.
   m_Lead2.SetDrawPersistence(TRUE);
   m_Lead3.SetDrawPersistence(TRUE);

   // First, we need some bitmaps.
   m_Lead2.CreateBitmap(LabelWidth, LabelHeight, 24);
   m_Lead3.CreateBitmap(LabelWidth, LabelHeight, 24);

   // Fill them with black, and make black transparent.
   m_Lead2.Fill(RGB(0, 0, 0));
   m_Lead3.Fill(RGB(0, 0, 0));
   m_Lead2.SetBitmapEnableTransparency(TRUE);
   m_Lead3.SetBitmapEnableTransparency(TRUE);
   m_Lead2.SetBitmapTransparentColor(RGB(0, 0, 0));
   m_Lead3.SetBitmapTransparentColor(RGB(0, 0, 0));

   // Draw a shape into m_Lead2.
   // Shape background
   m_Lead2.SetShapeBackgroundStyle(EFX_BACKSTYLE_TRANSLUCENT);

   // Shape location
   m_Lead2.SetShapeTop(0.0f);
   m_Lead2.SetShapeLeft(0.0f);
   m_Lead2.SetShapeWidth(LabelWidth);
   m_Lead2.SetShapeHeight(LabelHeight);

   // Shape border
   m_Lead2.SetShapeBorderColor(RGB(255, 0, 0));
   m_Lead2.SetShapeBorderStyle(EFX_BORDERSTYLE_SOLID);
   m_Lead2.SetShapeBorderThickness(5.0f);

   // Set the fill pattern.
   m_Lead2.SetPatternStyle(EFX_PATTERN_TRANSPARENT);

   // Draw the shape.
   m_Lead2.DrawShape(EFX_SHAPE_STAR4, 0);

   // Draw text into m_Lead3.
   // Make the font 48 point Arial. The variable, myFont, is a dispatch interface.
   COleFont myFont = m_Lead3.GetFont();
   myFont.SetName("Arial");
   CY size = myFont.GetSize();
   size.int64 = 48 * 10000;
   myFont.SetSize(size);

   // Finish describing the text.
   m_Lead3.SetTextAngle(450);
   m_Lead3.SetDrawFontColor(RGB(0, 0, 255)); //Blue
   m_Lead3.SetTextStyle(EFX_TEXTSTYLE_NORMAL);
   m_Lead3.SetTextAlign(EFX_TEXTALIGN_HCENTER_VCENTER);

   // Set the location for the text.
   m_Lead3.SetTextTop(0.0f);
   m_Lead3.SetTextLeft(0.0f);
   m_Lead3.SetTextWidth(LabelWidth);
   m_Lead3.SetTextHeight(LabelHeight);

   // Set the properties for drawing a rectangle behind the text.
   m_Lead3.SetDrawPenStyle(DRAWPENSTYLE_SOLID);
   m_Lead3.SetDrawPenColor(RGB(255, 255, 0));
   m_Lead3.SetDrawMode(DRAWMODE_COPY_PEN);

   // Draw the rectangle, then draw the text.
   m_Lead3.DrawRectangle(0.0f, 0.0f, LabelWidth, LabelHeight);
   m_Lead3.DrawText("Text", 0);

   // Load an image into m_Lead1, and paint the image.
   m_Lead1.Load("e:\\LTWIN14x\\images\\image1.cmp", 0, 0, 1);
   m_Lead1.ForceRepaint();

   // Turn off the paint effects.
   m_Lead1.SetPaintEffect(EFX_EFFECT_NONE);
   m_Lead2.SetPaintEffect(EFX_EFFECT_NONE);
   m_Lead3.SetPaintEffect(EFX_EFFECT_NONE);
}

6. Rebuild and run the application.