0

我需要使用光标在delphi中画一条线,我已经创建了线代码,但是我不知道下一步该怎么做?怎么能做到这一点,我按下鼠标,当线条需要开始并拖动我的鼠标或简单地释放鼠标按钮并绘制线条时。

procedure TForm1.Button1Click(Sender: TObject);
var 
  x0, y0, x1, y1: Integer;
begin
  x0 := StrToInt(Edit1.Text);
  y0 := StrToInt(Edit2.Text);
  x1 := StrToInt(Edit3.Text);
  y1 := StrToInt(Edit4.Text);
  Brezenhems(x0 , Y0 , X1 , Y1);
end;

我希望有人帮助我谢谢

4

2 回答 2

11

像这样的东西:

unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm4 = class(TForm)
    procedure FormResize(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    FStartPoint, FEndPoint: TPoint;
    FDrawingLine: boolean;
    bm: TBitmap;
    procedure AddLineToCanvas;
    procedure SwapBuffers;
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.FormCreate(Sender: TObject);
begin
  bm := TBitmap.Create;
  FDrawingLine := false;
end;

procedure TForm4.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FStartPoint := Point(X, Y);
  FDrawingLine := true;
end;

procedure TForm4.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if FDrawingLine then
  begin
    SwapBuffers;
    Canvas.MoveTo(FStartPoint.X, FStartPoint.Y);
    Canvas.LineTo(X, Y);
  end;
end;

procedure TForm4.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FDrawingLine := false;
  FEndPoint := Point(X, Y);
  AddLineToCanvas;
  SwapBuffers;
end;

procedure TForm4.AddLineToCanvas;
begin
  bm.Canvas.MoveTo(FStartPoint.X, FStartPoint.Y);
  bm.Canvas.LineTo(FEndPoint.X, FEndPoint.Y);
end;

procedure TForm4.FormPaint(Sender: TObject);
begin
  SwapBuffers;
end;

procedure TForm4.SwapBuffers;
begin
  BitBlt(Canvas.Handle, 0, 0, ClientWidth, ClientHeight,
    bm.Canvas.Handle, 0, 0, SRCCOPY);
end;

procedure TForm4.FormResize(Sender: TObject);
begin
  bm.SetSize(ClientWidth, ClientHeight);
end;

end.

编译示例EXE

请注意,此方法简单且稳健,但在性能方面并非最佳。如果您尝试在 Windows 3.1 时代的计算机上运行它,这可能会成为一个问题。

于 2012-11-21T16:45:07.393 回答
0

另一种无需创建位图即可使用的技术是使用 Pen.Mode 属性。像这样的东西:

  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;
于 2018-01-01T10:37:26.213 回答