Drawing Straight Lines In a VCL Forms Application
Go Up to How To Build a VCL Forms Application with Graphics
This procedure draws two diagonal straight lines on an image in a VCL form.
- Create a VCL form.
- Code the form's OnPaint event handler to draw the straight lines.
- Build and run the application.
To create a VCL form and place an image on it
- Choose File > New > Other > Delphi Projects or C++Builder Projects and double-click the VCL Forms Application icon.The VCL Forms Designer is displayed.
- In the form view, click the form, if necessary, to display Form1 in the Object Inspector.
To write the OnPaint event handler
- In the Object Inspector, double-click the Form1 OnPaint event on the Events tab.The Code Editor displays with the cursor in the TForm1.FormPaint event handler block.
- Enter the following event handling code:
with Canvas do begin MoveTo(0,0); LineTo(ClientWidth, ClientHeight); MoveTo(0, ClientHeight); LineTo(ClientWidth, 0); end;
Canvas->MoveTo( 0, 0 ); Canvas->LineTo( ClientWidth, ClientHeight ); Canvas->MoveTo( 0, ClientHeight ); Canvas->LineTo( ClientWidth, 0 );
To run the program
- Choose Run > Run .
- The applications executes, displaying a form with two diagonal crossing lines.
Tip: To change the color of the pen to green, insert this statement following the first MoveTo() statement in the event handler code: Pen.Color := clGreen; (Delphi) Canvas->Pen->Color = clGreen; (C++). Experiment using other canvas and pen object properties. See Using the Properties of the Canvas Object in the Windows Developer's Guide.