RAD Studio
ContentsIndex
PreviousUpNext
Drawing Straight Lines In a VCL Forms Application

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 FileNewOtherDelphi 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 RunRun.
  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 Delphi 7 Developer's Guide.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!