Drawing Simple Lines and Shapes (C++ Builder 4)

Take the following steps to add code that lets you draw a line, rectangle and ellipse on the bitmap.

1.

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

2.

On the Project pull-down menu, use the Import Type library… and select the LEAD 3D Raster DrawEffect Object Library (14.5), and Press Ok.

3.

Add the following variable to Unit1.h private declarations section.

 LEADRasterFXD * pRasterFxd;

4.

Add the following code to the end of the form's Create procedure.

//Create the RasterFxd Object
CoCreateInstance(CLSID_LEADRasterFXD, NULL, CLSCTX_ALL, IID_ILEADRasterFXD, (void**)&pRasterFxd); 

5.

Code the Form’s Close event as the following:

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
 if (pRasterFxd)
 pRasterFxd->Release();
}

6.

Add the following variables to Unit1.h private declarations section:

  int DrawObject; //the object we are drawing
float StartX;//Starting X position
float StartY;//Starting Y position
float EndX;//Ending X position
float EndY;//Ending Y position

7.

Handle the LEADRasterView1 control's OnMouseDown2 event, and code LEADRasterView1MouseDown2 as follows.This code selects a different drawing object each time the event occurs.

void __fastcall TForm1::LEADRasterView1MouseDown2 (TObject *Sender,
short Button, short Shift, long x, long y)
{
 //Use the same scale mode as the mouse.
LEADRasterView1->ScaleMode = 1;
//Save the starting position.
 StartX = x;
 StartY = y;
 EndX = x;
 EndY = y;
 //Cycle through the types of drawing objects.
 switch(DrawObject)
{

  case 0:
 DrawObject = 1 ;//Line
break;

  case 1:
 DrawObject = 2 ;//Rectangle
 break;

  case 2:
 DrawObject = 0 ;//Ellipse
break;
  default:
  DrawObject = 0;
 }
 pRasterFxd->DstLeft = 0;
 pRasterFxd->DstTop = 0;
 pRasterFxd->DstRight = LEADRasterView1->Raster->BitmapWidth;
 pRasterFxd->DstBottom = LEADRasterView1->Raster->BitmapHeight;
 pRasterFxd->SrcLeft = 0;
 pRasterFxd->SrcTop = 0;
 pRasterFxd->SrcRight = LEADRasterView1->Raster->BitmapWidth;
 pRasterFxd->SrcBottom = LEADRasterView1->Raster->BitmapHeight;
 pRasterFxd->ScaleMode = 3;
}

8.

Handle the LEADRasterView1 control's OnMouseMove2 event, and code LEADRasterView1MouseMove2 as follows. This code uses DRAWMODE_INVERT for the DrawMode, which means that pixel colors are inverted. Thus, the drawing methods can erase the previous object and draw a new one.

void __fastcall TForm1::LEADRasterView1MouseMove2 (TObject *Sender,
short Button, short Shift, long x, long y)
{
 //Declare local variables
 float OldEndX, OldEndY;
 float OldDrawX, OldDrawY, OldWidth, OldHeight;
 float DrawX, DrawY, NewWidth, NewHeight;
HDC h_DC;

 if (Button == 1)
{
  //Set the drawing styles
  pRasterFxd->DrawPenStyle = DRAWPENSTYLE_SOLID;
  pRasterFxd->DrawMode = DRAWMODE_INVERT;
  pRasterFxd->DrawFillStyle = DRAWFILLSTYLE_TRANSPARENT;
  pRasterFxd->DrawPersistence = False; // On the window, not the bitmap
  //Save the previous ending mouse position
  OldEndX = EndX;
  OldEndY = EndY;
  //Get the current mouse position->
  EndX = x;
  EndY = y;
  //Calculate the origin of the current object
  if (EndX > StartX)
 DrawX = StartX;
  else
 DrawX = EndX;

  if (EndY > StartY)
 DrawY = StartY;
  else
 DrawY = EndY;

  //Calculate the origin of the previous object
  if (OldEndX > StartX)
 OldDrawX = StartX;
  else
 OldDrawX = OldEndX;

  if (OldEndY > StartY)
 OldDrawY = StartY;
  else
 OldDrawY = OldEndY;

  //Calculate the height and width of the current object
  NewHeight = abs(StartY - EndY);
  NewWidth = abs(StartX - EndX);
  //Calculate the height and width of the previous object
  OldHeight = abs(StartY - OldEndY);
  OldWidth = abs(StartX - OldEndX);
  //Erase the old object and draw the new one
  h_DC = (HDC)LEADRasterView1->GetClientDC ();
  switch (DrawObject)
{
 case 0: //Ellipse
{
   pRasterFxd->DrawEllipse (NULL, (long)h_DC, OldDrawX, OldDrawY, OldWidth, OldHeight);
   pRasterFxd->DrawEllipse (NULL, (long)h_DC, DrawX, DrawY, NewWidth, NewHeight);
}
break;
   case 1: //Line
{
   pRasterFxd->DrawLine (NULL, (long)h_DC, StartX, StartY, OldEndX, OldEndY);
   pRasterFxd->DrawLine (NULL, (long)h_DC, StartX, StartY, EndX, EndY);
}
break;
   case 2: //Rectangle
{
   pRasterFxd->DrawRectangle (NULL, (long)h_DC, OldDrawX, OldDrawY, OldWidth, OldHeight);
   pRasterFxd->DrawRectangle (NULL, (long)h_DC, DrawX, DrawY, NewWidth, NewHeight);
}
break;

   default:
 break;
  }
  LEADRasterView1->ReleaseClientDC ();
}
}

9.

Handle the LEADRasterView1 control's OnMouseUp2 event, and code LEADRasterView1MouseUp2 as follows. This code sets the drawing style and draws the object on the bitmap.

void __fastcall TForm1::LEADRasterView1MouseUp2 (TObject *Sender,
short Button, short Shift, long x, long y)
{
 //Declare local variables.
 float DrawX, DrawY, NewWidth, NewHeight;

 //Set the drawing style.
 pRasterFxd->DrawPenStyle = DRAWPENSTYLE_SOLID;
 pRasterFxd->DrawPenWidth = 2;
 pRasterFxd->DrawPenColor = RGB(255, 0, 0) ;//Red
 pRasterFxd->DrawMode = DRAWMODE_COPY_PEN;
 pRasterFxd->DrawFillColor = RGB(0, 255, 0); //Green;
 pRasterFxd->DrawFillStyle = DRAWFILLSTYLE_HORIZONTAL_LINE;
 pRasterFxd->DrawPersistence = True ;//On the bitmap
 //Get the current mouse position
 EndX = x;
 EndY = y;
 //Determine the origin of the object
 if (EndX > StartX)
  DrawX = StartX;
 else
  DrawX = EndX;

 if (EndY > StartY)
  DrawY = StartY;
 else
  DrawY = EndY;

 //Determine the height and width of the object
 NewHeight = abs(StartY - EndY);
 NewWidth = abs(StartX - EndX);
 //Draw the object
 switch(DrawObject)
{
  case 0: //Ellipse
   pRasterFxd->DrawEllipse (LEADRasterView1->Raster, 0, DrawX, DrawY, NewWidth, NewHeight);
break;
 case 1: //Line
   pRasterFxd->DrawLine (LEADRasterView1->Raster, 0, StartX, StartY, EndX, EndY);
break;
  case 2: //Rectangle
   pRasterFxd->DrawRectangle (LEADRasterView1->Raster, 0, DrawX, DrawY, NewWidth, NewHeight);
break;
 }
 LEADRasterView1->ForceRepaint ();
}

10.

At the beginning of the Unit1.h file, include the file

#include "LTRASTERFXDLib_TLB.h"

11.

Run your program to test it.