Drawing Simple Lines and Shapes (C++ Builder 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 install 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: |
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
5. |
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;
}
LEADRasterFXD1->set_DstLeft ( 0 );
LEADRasterFXD1->set_DstTop ( 0 );
LEADRasterFXD1->set_DstRight ( LEADRasterView1->Raster->BitmapWidth );
LEADRasterFXD1->set_DstBottom ( LEADRasterView1->Raster->BitmapHeight );
LEADRasterFXD1->set_SrcLeft ( 0 );
LEADRasterFXD1->set_SrcTop ( 0 );
LEADRasterFXD1->set_SrcRight ( LEADRasterView1->Raster->BitmapWidth );
LEADRasterFXD1->set_SrcBottom ( LEADRasterView1->Raster->BitmapHeight );
LEADRasterFXD1->set_ScaleMode ( 3 );
}
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. |
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
LEADRasterFXD1->set_DrawPenStyle ( DRAWPENSTYLE_SOLID );
LEADRasterFXD1->set_DrawMode ( DRAWMODE_INVERT );
LEADRasterFXD1->set_DrawFillStyle ( DRAWFILLSTYLE_TRANSPARENT );
LEADRasterFXD1->set_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
{
LEADRasterFXD1->DrawEllipse (NULL, (long)h_DC, OldDrawX, OldDrawY, OldWidth, OldHeight);
LEADRasterFXD1->DrawEllipse (NULL, (long)h_DC, DrawX, DrawY, NewWidth, NewHeight);
}
break;
case 1: //Line
{
LEADRasterFXD1->DrawLine (NULL, (long)h_DC, StartX, StartY, OldEndX, OldEndY);
LEADRasterFXD1->DrawLine (NULL, (long)h_DC, StartX, StartY, EndX, EndY);
}
break;
case 2: //Rectangle
{
LEADRasterFXD1->DrawRectangle (NULL, (long)h_DC, OldDrawX, OldDrawY, OldWidth, OldHeight);
LEADRasterFXD1->DrawRectangle (NULL, (long)h_DC, DrawX, DrawY, NewWidth, NewHeight);
}
break;
default:
break;
}
LEADRasterView1->ReleaseClientDC ();
}
}
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. |
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.
LEADRasterFXD1->set_DrawPenStyle ( DRAWPENSTYLE_SOLID );
LEADRasterFXD1->set_DrawPenWidth ( 2 );
LEADRasterFXD1->set_DrawPenColor ( RGB(255, 0, 0) );//Red
LEADRasterFXD1->set_DrawMode ( DRAWMODE_COPY_PEN );
LEADRasterFXD1->set_DrawFillColor ( RGB(0, 255, 0) ); //Green;
LEADRasterFXD1->set_DrawFillStyle ( DRAWFILLSTYLE_HORIZONTAL_LINE );
LEADRasterFXD1->set_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
LEADRasterFXD1->DrawEllipse (LEADRasterView1->Raster, 0, DrawX, DrawY, NewWidth, NewHeight);
break;
case 1: //Line
LEADRasterFXD1->DrawLine (LEADRasterView1->Raster, 0, StartX, StartY, EndX, EndY);
break;
case 2: //Rectangle
LEADRasterFXD1->DrawRectangle (LEADRasterView1->Raster, 0, DrawX, DrawY, NewWidth, NewHeight);
break;
}
LEADRasterView1->ForceRepaint ();
}
8. |
Run your program to test it. |