• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Window画图程序中画曲线的简单实现_Delphi

Delphi 搞代码 3年前 (2018-08-09) 131次浏览 已收录 0个评论

下面是很入以前的一个程序,模仿Windows的画图程序中画曲线.其实原理简单得要命 ,就是一个PolyBezier函数.
那一位有兴趣的可以封装成一个类,方便以后用.
unit Unit1;

interface

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

http://www.gaodaima.com/?p=66081Window画图程序中画曲线的简单实现_Delphi

type
  TStep=(sStartEnd,sCtrl1,sCtrl2);
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    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);
    procedure FormPaint(Sender: TObject);
  private
    { Private declarations }
    isDown:Boolean;
    isOK:Boolean;  //标识一次Bezier线的绘制是否完成
    Step:TStep; //指示到那个阶段了
    StartP:TPoint;  //以下是四个点坐标
    EndP:TPoint;
    CtrP1:TPoint;
    CtrP2:TPoint;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  self.DoubleBuffered:=true;
  isDown:=False;
  Step:=sStartEnd;
  isOK:=False;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  isDown:=true;
  if Button=mbLeft then
  begin
    if (Step=sStartEnd) then
    begin
      StartP.X:=X;
      StartP.Y:=Y;
      EndP:=Point(X,Y);
      isOK:=False;
    end
    else if (Step=sCtrl1)  then
    begin
      CtrP1:=Point(X,Y);
      CtrP2:=Point(X,Y);
    end
    else if (Step=sCtrl2)  then
    begin
      CtrP2:=Point(X,Y);
    end;
  end;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if isDown and (Shift=[ssLeft]) then
  begin
    if (Step=sStartEnd) then
    begin
      EndP.X:=X;
      EndP.Y:=Y;
    end
    else if Step=sCtrl1 then
    begin
      CtrP1:=Point(X,Y);
      CtrP2:=Point(X,Y);
    end
    else if (Step=sCtrl2)  then
    begin
      CtrP2:=Point(X,Y);
    end;
    self.Invalidate;
  end;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if isDown and (Button=mbLeft) then
  begin
    isDown:=False;
    if Step=sStartEnd then Step:=sCtrl1
    else if Step=sCtrl1 then Step:=sCtrl2
    else if Step=sCtrl2 then begin Step:=sStartEnd; isOK:=True; end;
  end;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  if not isOK then
  begin
    if Step=sStartEnd then
    begin
      self.Canvas.MoveTo(StartP.X,StartP.Y);
      self.Canvas.LineTo(EndP.X,EndP.Y);
    end
    else begin
     Self.Canvas.PolyBezier([StartP,CtrP1,CtrP2,EndP]);
    end;
  end else begin
    Self.Canvas.PolyBezier([StartP,CtrP1,CtrP2,EndP]);
  end;
end;

end.

欢迎大家阅读《Window画图程序中画曲线的简单实现_Delphi,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址