RAD Studio |  |

Drawing Straight Lines In a VCL Forms Application
See Also
Collapse All

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:

Copy Code

    with Canvas do
    begin
    MoveTo(0,0);
    LineTo(ClientWidth, ClientHeight);
    MoveTo(0, ClientHeight);
    LineTo(ClientWidth, 0);
    end;

Copy Code

    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.

See Also

VCL Overview

Building VCL Forms Applications With Graphics

Drawing Rectangles and Ellipses in a VCL Forms Application

Drawing a Polygon in a VCL Forms Application

Displaying a Bitmap Image in a VCL Forms Application

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