Drawing Simple Lines and Shapes (Delphi 6.0)
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). Press OK. |
3. |
Select LEAD Raster FXD, from the ActiveX tab and add it to your form. |
4. |
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
5. |
Handle the LEADRasterView1 control's OnMouseDown2 event, and code LEADRasterView1MouseDown2 as follows: |
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;
LEADRasterFxd1.DstLeft := 0;
LEADRasterFxd1.DstTop := 0;
LEADRasterFxd1.DstRight := Trunc (LEADRasterView1.Raster.BitmapWidth);
LEADRasterFxd1.DstBottom := Trunc (LEADRasterView1.Raster.BitmapHeight);
LEADRasterFxd1.SrcLeft := 0;
LEADRasterFxd1.SrcTop := 0;
LEADRasterFxd1.SrcRight := Trunc (LEADRasterView1.Raster.BitmapWidth);
LEADRasterFxd1.SrcBottom := Trunc (LEADRasterView1.Raster.BitmapHeight);
LEADRasterFxd1.ScaleMode := 3;
end;
6. |
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;
begin
if Button = 1 then
begin
//Set the drawing styles.
LEADRasterFxd1.DrawPenStyle := DRAWPENSTYLE_SOLID;
LEADRasterFxd1.DrawMode := DRAWMODE_INVERT;
LEADRasterFxd1.DrawFillStyle := DRAWFILLSTYLE_TRANSPARENT;
LEADRasterFxd1.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 ();
Case DrawObject of
0: //Ellipse
begin
LEADRasterFxd1.DrawEllipse(Nil, h_DC, OldDrawX, OldDrawY, OldWidth, OldHeight);
LEADRasterFxd1.DrawEllipse (Nil, h_DC, DrawX, DrawY, NewWidth, NewHeight);
end;
1: //Line
begin
LEADRasterFxd1.DrawLine(Nil, h_DC, StartX, StartY, OldEndX, OldEndY);
LEADRasterFxd1.DrawLine (Nil, h_DC, StartX, StartY, EndX, EndY);
end;
2: //Rectangle
begin
LEADRasterFxd1.DrawRectangle(Nil, h_DC, OldDrawX, OldDrawY, OldWidth, OldHeight);
LEADRasterFxd1.DrawRectangle (Nil, h_DC, DrawX, DrawY, NewWidth, NewHeight);
end;
else
end;
LEADRasterView1.ReleaseClientDC ();
end;
end;
7. |
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;
begin
//Set the drawing style.
LEADRasterFxd1.DrawPenStyle := DRAWPENSTYLE_SOLID;
LEADRasterFxd1.DrawPenWidth := 2;
LEADRasterFxd1.DrawPenColor := RGB(255, 0, 0) ;//Red
LEADRasterFxd1.DrawMode := DRAWMODE_COPY_PEN;
LEADRasterFxd1.DrawFillColor := RGB(0, 255, 0); //Green;
LEADRasterFxd1.DrawFillStyle := DRAWFILLSTYLE_HORIZONTAL_LINE;
LEADRasterFxd1.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
LEADRasterFxd1.DrawEllipse (LEADRasterView1.Raster, 0, DrawX, DrawY, NewWidth, NewHeight);
1: //Line
LEADRasterFxd1.DrawLine (LEADRasterView1.Raster, 0, StartX, StartY, EndX, EndY);
2: //Rectangle
LEADRasterFxd1.DrawRectangle (LEADRasterView1.Raster, 0, DrawX, DrawY, NewWidth, NewHeight);
end;
LEADRasterView1.ForceRepaint ();
end;
8. |
Run your program to test it. |