Drawing Straight Lines In a VCL Forms Application

From RAD Studio
Jump to: navigation, search

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.

  1. Create a VCL form.
  2. Code the form's OnPaint event handler to draw the straight lines.
  3. Build and run the application.

To create a VCL form and place an image on it

  1. Choose File > New > Other > Delphi Projects or C++Builder Projects and double-click the VCL Forms Application icon.The VCL Forms Designer is displayed.
  2. In the form view, click the form, if necessary, to display Form1 in the Object Inspector.

To write the OnPaint event handler

  1. 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.
  2. 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

  1. Choose Run > Run .
  2. 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.

See Also