How to draw a line using mouse drag?

后端 未结 2 1955
失恋的感觉
失恋的感觉 2021-01-07 14:43

I need to draw a line in delphi using the cursor, I already have created the line code, but I can\'t get what to do next? How can do that, I push the mouse, when the line ne

2条回答
  •  生来不讨喜
    2021-01-07 15:23

    Another technique that can be used, without the need to create a bitmap, is using the Pen.Mode property. Something like this:

      TForm2 = class(TForm)
        procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
        procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
      private
       PO,LP: TPoint;
       draw: boolean;
      public
        { Public declarations }
      end;
    
    procedure TForm2.FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
     PO.X:= X;
     PO.Y:= Y;
     LP.X:= X;
     LP.Y:= Y;
     draw:= true;
     Canvas.Pen.Mode:= pmNotXor;
    end;
    
    procedure TForm2.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
     if draw then
      begin
       if (LP.X <> PO.X) or (LP.Y <> PO.Y) then
        begin
         Canvas.MoveTo(PO.X,PO.Y);
         Canvas.LineTo(LP.X,LP.Y);
        end;
       LP.X:= X;
       LP.Y:= Y;
       Canvas.MoveTo(PO.X,PO.Y);
       Canvas.LineTo(LP.X,LP.Y);
      end;
    end;
    
    procedure TForm2.FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
     if draw then draw:= false;
    end;
    

提交回复
热议问题