Drawing Simple Lines and Shapes (C++ Builder)

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.

Add the following declarations to the private section of the Unit1.h file:

int DrawObject; /*The object we are drawing*/
int StartX; /*Starting X position*/
int StartY; /*Starting Y position*/
int EndX; /*Ending X position*/
int EndY; /*Ending Y position*/
bool Drawing;

3.

Add the following line to the Unit1.h file:

#include <stdlib.h>

4.

Add the following initialization code to the main form's FormShow procedure:

DrawObject = 0;
Drawing = False;

5.

Code the LEAD control's MouseDown event as follows. This code selects a different drawing object each time the event occurs.

void __fastcall TForm1::Lead1MouseDown (TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
   //Temp. Disable Scrolling
   Lead1->EnableKeyboard = False;
   Lead1->EnableScroll = False;
   Drawing = True;
   /*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;
  }
}

6.

Code the LEAD control's MouseMove event as follows. This code uses pmNot 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::Lead1MouseMove (TObject *Sender, TShiftState Shift, int X, int Y)
{
   int OldEndX, OldEndY;
   int OldDrawX, OldDrawY, OldWidth, OldHeight;
   int DrawX, DrawY, NewWidth, NewHeight;

   if(Drawing == True)
   {
      /*Set the drawing styles.*/
      Lead1->DrawPenStyle = psSolid;
      Lead1->DrawMode = pmNot;
      Lead1->DrawFillStyle = bsClear;
      Lead1->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.*/
      switch(DrawObject)
      {
        case 0:   /*Ellipse*/
           Lead1->DrawEllipse(OldDrawX, OldDrawY, OldWidth, OldHeight);
           Lead1->DrawEllipse(DrawX, DrawY, NewWidth, NewHeight);
           break;
        case 1:   /*Line*/
           Lead1->DrawLine(StartX, StartY, OldEndX, OldEndY);
           Lead1->DrawLine(StartX, StartY, EndX, EndY);
           break;
        case 2:   /*Rectangle*/
           Lead1->DrawRectangle(OldDrawX, OldDrawY, OldWidth, OldHeight);
           Lead1->DrawRectangle(DrawX, DrawY, NewWidth, NewHeight);
           break;
      }
   }
}

7.

Code the LEAD control's MouseUp event as follows. This code sets the drawing style and draws the object on the bitmap.

void __fastcall TForm1::Lead1MouseUp (TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
  int DrawX, DrawY, NewWidth, NewHeight;

  //Restore scrolling
  Lead1->EnableKeyboard = True;
  Lead1->EnableScroll = True;
  Drawing = False;
  /*Set the drawing style.*/
  Lead1->DrawPenStyle = psSolid;
  Lead1->DrawPenWidth = 2;
  Lead1->DrawPenColor = (TColor)RGB(255, 0, 0);    /*Red*/
  Lead1->DrawMode = pmCopy;
  Lead1->DrawFillColor = (TColor)RGB(0, 255, 0);   /*Green*/
  Lead1->DrawFillStyle = bsHorizontal;
  Lead1->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*/
       Lead1->DrawEllipse(DrawX, DrawY, NewWidth, NewHeight);
       break;
    case 1: /*Line*/
       Lead1->DrawLine(StartX, StartY, EndX, EndY);
       break;
    case 2: /*Rectangle*/
       Lead1->DrawRectangle(DrawX, DrawY, NewWidth, NewHeight);
       break;
  }
}

8.

Run your program to test it.