鼠标画折线,先点左键不放,松开后就画出折线,编译没问题,但折线画不出来unit TreeViewTest;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
   Button1: TButton;
    procedure FormMouseDown(Sender:TObject;Button:TMouseButton;Shift:TShiftState;X,Y:Integer);
    procedure FormMouseUp(Sender:TObject;Button:TMouseButton;Shift:TShiftState;X,Y:Integer);
    procedure FormCreate(Sender:TObject);
    procedure Button1Click(Sender:TObject);  private
    { Private declarations }
  public
    { Public declarations }
    ax,ay:Integer;
  end;var
  Form1: TForm1;
  start:Boolean;
implementation
  procedure TForm1.FormCreate(Sender:TObject);
  begin
    start:=False;
  end;  procedure TForm1.Button1Click(Sender:TObject);
  begin
    start:=true;
  end;
  procedure TForm1.FormMouseDown(Sender:TObject;Button:TMouseButton;Shift:TShiftState;X,Y:Integer);
  begin
   ax:=x;
   ay:=y;
  end;  procedure TForm1.FormMouseUp(Sender:TObject;Button:TMouseButton;Shift:TShiftState;X,Y:Integer);
  begin  with Form1 do
  begin
     if  start then
   Canvas.Pen.Width:=1;
   Canvas.Pen.Color:=clBlack;
   Canvas.Pen.Style:=psSolid;
   {以下代码画折线}
   Canvas.MoveTo(ax,ay);
   Canvas.LineTo(ax+40,ay);
   Canvas.LineTo(ax+40,y);
   Canvas.LineTo(x,y);
{以下代码画箭头}
   Canvas.MoveTo(x-5,y-5);
   Canvas.LineTo(x,y);
   Canvas.LineTo(x-5,y+5);
   start:=false;
  end;
  end;
  {$R *.dfm}end.

解决方案 »

  1.   

    在 "if start then" 之后应加begin,否则只执行其后的一条语句.改为如下:  if  start then
     begin
       Canvas.Pen.Width:=1;
       Canvas.Pen.Color:=clBlack;
       Canvas.Pen.Style:=psSolid;
       {以下代码画折线}
       Canvas.MoveTo(ax,ay);
       Canvas.LineTo(ax+40,ay);
       Canvas.LineTo(ax+40,y);
       Canvas.LineTo(x,y);
    {以下代码画箭头}
       Canvas.MoveTo(x-5,y-5);
       Canvas.LineTo(x,y);
       Canvas.LineTo(x-5,y+5);
       start:=false;
     end;