Delphi Tutorial


 

 

Draw cross hair

 

Download this example

               Use FormMouseMove procedure to draw the cross hair. Actually if you carefully you see there is another procedure call DrawCross define inside the main procedure. I use this method because within this program DrawCross is call only from FormMouseMove procedure. So you can write the sub procedure within main procedure.

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);

      { I wrote this procedure with in this block for
      easy access, this one draws the cross hair}
      Procedure DrawCross(AX,AY:Integer);
      Begin
            With Form1.Canvas Do
            Begin
                  { By putting pen mode to Xor will help to
                  erase the previous cross hair }

                  Form1.Canvas.Pen.Mode:=pmNotXor;
                  MoveTo(Ax,0);
                  LineTo(Ax,Form1.Height);
                  MoveTo(0,AY);
                  LineTo(Form1.Width,AY);
            End;
      End;

begin
      If Not FirstMove Then
            DrawCross(OldX,OldY)
      Else
            FirstMove:=False;
      DrawCross(X,Y);
      Form1.Caption:='X : '+IntToStr(X)+' Y : '+IntToStr(Y);
      OldX:=X;
      OldY:=Y;
end;

 

      In here FirstMove is a global variable define to identify the first time draw. It will help to erase the previous draw as mouse change its position. At the beginning set this to TRUE.

procedure TForm1.FormCreate(Sender: TObject);
begin
      FirstMove:=True;
end;

DOWNLOAD this example

Click here to download this complete example for Delphi 6. (File size 4KB)

 

 

Go top

 


Previous Page First Page Next Page
 
Copyright © 2002 digitalcoding.com. All rights reserved.