unit Unit1;

interface

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

type
  TForm1 = 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);
    procedure FormPaint(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses
  GDIPOBJ,GDIPAPI;
var
  pts:array of TGPPoint;
  ptn:Integer = -1;
  flag:Boolean;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  flag:=True;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  i:Integer;
  R:TRect;
begin
  if flag then
  begin
    if ptn = -1 then Exit;
    pts[ptn]:=MakePoint(x,y);
    Repaint;
  end
  else
  begin
    ptn:=-1;
    Cursor:=crDefault;
    for I := Low(pts) to High(pts) do
    begin
      R:=Rect(pts[i].X-2,pts[i].Y-2,pts[i].X+2,pts[i].Y+2);
      if PtInRect(R,Point(x,y)) then
      begin
        ptn:=i;
        Cursor:=crHandPoint;
      end;
    end;
  end;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  flag:=False;
  if ssCtrl in Shift then
  begin
    SetLength(pts,Length(pts)+1);
    pts[High(pts)]:=MakePoint(x,y);
    Canvas.Pen.Color:=clBlue;
    Canvas.Rectangle(x-2,y-2,x+2,y+2);
    Repaint;  //万老师博客上是if Length(pts) mod 3 = 1 then Repaint; ,这个其实不需要这样
  end;
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  g:TGPGraphics;
  p:TGPPen;
  i:Integer;
begin
  Form1.DoubleBuffered:=True;
  g:=TGPGraphics.Create(Canvas.Handle);
  g.Clear(aclWhite);
  p:=TGPPen.Create(aclRed);

  g.DrawBeziers(p,PGPPoint(pts),Length(pts));

  p.SetColor(aclBlue);
  for I := Low(pts) to High(pts) do
  begin
    g.DrawRectangle(p,MakeRect(Rect(pts[i].X-2,pts[i].Y-2,pts[i].X+2,pts[i].Y+2)));
  end;
  p.Free;
  g.Free;
end;

end.

posted on 2010-12-31 10:58  巅枫  阅读(446)  评论(0编辑  收藏  举报