Drawing Simple Lines and Shapes (Delphi)

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 file:

DrawObject : Integer; {The object we are drawing}
StartX : Integer; {Starting X position}
StartY : Integer; {Starting Y position}
EndX : Integer; {Ending X position}
EndY : Integer; {Ending Y position}
Drawing : boolean;

3.

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

DrawObject := 0;
Drawing := False;

4.

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

procedure TForm1.Lead1MouseDown (Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  //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.}
  case DrawObject of
    0:
       DrawObject := 1;   {Line}
    1:
       DrawObject := 2;   {Rectangle}
    2:
       DrawObject := 0;   {Ellipse}
  end;
end;

5.

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.

procedure TForm1.Lead1MouseMove (Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  OldEndX, OldEndY : Integer;
  OldDrawX, OldDrawY, OldWidth, OldHeight : Integer;
  DrawX, DrawY, NewWidth, NewHeight : Integer;

begin
  if (Drawing = True) then
  begin
    {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) then
       DrawX := StartX
    else
       DrawX := EndX;
    if (EndY > StartY) then
       DrawY := StartY
    else
       DrawY := EndY;

    {Calculate the origin of the previous object.}
    if (OldEndX > StartX) then
       OldDrawX := StartX
    else
       OldDrawX := OldEndX;
    if (OldEndY > StartY) then
       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.}
    case DrawObject of
      0:   {Ellipse}
      begin
       Lead1.DrawEllipse(OldDrawX, OldDrawY, OldWidth, OldHeight);
       Lead1.DrawEllipse(DrawX, DrawY, NewWidth, NewHeight);
      end;
      1:   {Line}
      begin
       Lead1.DrawLine(StartX, StartY, OldEndX, OldEndY);
       Lead1.DrawLine(StartX, StartY, EndX, EndY);
      end;
      2:   {Rectangle}
      begin
       Lead1.DrawRectangle(OldDrawX, OldDrawY, OldWidth, OldHeight);
       Lead1.DrawRectangle(DrawX, DrawY, NewWidth, NewHeight);
      end;
    end;
  end;
end;

6.

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

procedure TForm1.Lead1MouseUp (Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  DrawX, DrawY, NewWidth, NewHeight : Integer;
begin
  //Restore scrolling
  Lead1.EnableKeyboard:= True;
  Lead1.EnableScroll:= True;
  Drawing := False;
  {Set the drawing style.}
  Lead1.DrawPenStyle := psSolid;
  Lead1.DrawPenWidth := 2;
  Lead1.DrawPenColor := RGB(255, 0, 0);    {Red}
  Lead1.DrawMode := pmCopy;
  Lead1.DrawFillColor := 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) then
     DrawX := StartX
  else
     DrawX := EndX;
  if (EndY > StartY) then
     DrawY := StartY
  else
     DrawY := EndY;

  {Determine the height and width of the object.}
  NewHeight := Abs(StartY - EndY);
  NewWidth := Abs(StartX - EndX);

  {Draw the object}
  case DrawObject of
    0: {Ellipse}
       Lead1.DrawEllipse(DrawX, DrawY, NewWidth, NewHeight);
    1: {Line}
       Lead1.DrawLine(StartX, StartY, EndX, EndY);
    2: {Rectangle}
       Lead1.DrawRectangle(DrawX, DrawY, NewWidth, NewHeight);
  end;
end;

7.

Run your program to test it.