Drawing Simple Lines and Shapes (Delphi 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 DrawEffect Object Library (14.5), and Press Ok. |
3. |
Add the following variable to the Main Form’s private declarations section. |
RasterFxd: LEADRasterFXD;
4. |
Add the following code to the end of the form's Create procedure. |
//Create the RasterFxd Object
RasterFxd := CreateComObject ( CLASS_LEADRasterFXD) as LEADRasterFXD;
5. |
Add the following variables to the Main Form’s private declarations section: |
DrawObject: Integer; //the object we are drawing
StartX: Single ;//Starting X position
StartY: Single ;//Starting Y position
EndX: Single ;//Ending X position
EndY: Single ;//Ending Y position
6. |
Handle the LEADRasterView1 control's OnMouseDown2 event, and code LEADRasterView1MouseDown2 as follows. This code selects a different drawing object each time the event occurs. |
procedure TForm1.LEADRasterView1MouseDown2 (Sender: TObject; Button,
Shift: Smallint; x, y: Integer);
begin
//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.
Case DrawObject of
0: DrawObject := 1 ;//Line
1: DrawObject := 2 ;//Rectangle
2: DrawObject := 0 ;//Ellipse
else
DrawObject := 0;
end;
RasterFxd.DstLeft := 0;
RasterFxd.DstTop := 0;
RasterFxd.DstRight := Trunc (LEADRasterView1.Raster.BitmapWidth);
RasterFxd.DstBottom := Trunc (LEADRasterView1.Raster.BitmapHeight);
RasterFxd.SrcLeft := 0;
RasterFxd.SrcTop := 0;
RasterFxd.SrcRight := Trunc (LEADRasterView1.Raster.BitmapWidth);
RasterFxd.SrcBottom := Trunc (LEADRasterView1.Raster.BitmapHeight);
RasterFxd.ScaleMode := 3;
end;
7. |
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. |
procedure TForm1.LEADRasterView1MouseMove2(Sender: TObject; Button,
Shift: Smallint; x, y: Integer);
var
//Declare local variables.
OldEndX, OldEndY: Single;
OldDrawX, OldDrawY, OldWidth, OldHeight: Single;
DrawX, DrawY, NewWidth, NewHeight: Single;
h_DC: HDC;
iRet: Integer;
sRet: smallint;
begin
if Button = 1 then
begin
//Set the drawing styles.
RasterFxd.DrawPenStyle := DRAWPENSTYLE_SOLID;
RasterFxd.DrawMode := DRAWMODE_INVERT;
RasterFxd.DrawFillStyle := DRAWFILLSTYLE_TRANSPARENT;
RasterFxd.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.
h_DC := LEADRasterView1.GetClientDC (iRet);
Case DrawObject of
0: //Ellipse
begin
RasterFxd.DrawEllipse (Nil, h_DC, OldDrawX, OldDrawY, OldWidth, OldHeight);
RasterFxd.DrawEllipse (Nil, h_DC, DrawX, DrawY, NewWidth, NewHeight);
end;
1: //Line
begin
RasterFxd.DrawLine (Nil, h_DC, StartX, StartY, OldEndX, OldEndY);
RasterFxd.DrawLine (Nil, h_DC, StartX, StartY, EndX, EndY);
end;
2: //Rectangle
begin
RasterFxd.DrawRectangle (Nil, h_DC, OldDrawX, OldDrawY, OldWidth, OldHeight);
RasterFxd.DrawRectangle (Nil, h_DC, DrawX, DrawY, NewWidth, NewHeight);
end;
else
end;
LEADRasterView1.ReleaseClientDC (sRet);
end;
end;
8. |
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. |
procedure TForm1.LEADRasterView1MouseUp2 (Sender: TObject; Button,
Shift: Smallint; x, y: Integer);
var
//Declare local variables.
DrawX, DrawY, NewWidth, NewHeight: Single;
sRet: Smallint;
begin
//Set the drawing style.
RasterFxd.DrawPenStyle := DRAWPENSTYLE_SOLID;
RasterFxd.DrawPenWidth := 2;
RasterFxd.DrawPenColor := RGB(255, 0, 0) ;//Red
RasterFxd.DrawMode := DRAWMODE_COPY_PEN;
RasterFxd.DrawFillColor := RGB(0, 255, 0); //Green;
RasterFxd.DrawFillStyle := DRAWFILLSTYLE_HORIZONTAL_LINE;
RasterFxd.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
RasterFxd.DrawEllipse (LEADRasterView1.Raster, 0, DrawX, DrawY, NewWidth, NewHeight);
1: //Line
RasterFxd.DrawLine (LEADRasterView1.Raster, 0, StartX, StartY, EndX, EndY);
2: //Rectangle
RasterFxd.DrawRectangle (LEADRasterView1.Raster, 0, DrawX, DrawY, NewWidth, NewHeight);
end;
LEADRasterView1.ForceRepaint (sRet);
end;
9. |
At the beginning of the Unit1 file, add LTRASTERFXDLib_TLB to the uses section. For example: |
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, LTRASTERFXDLib_TLB;
10. |
Run your program to test it. |