再學GDI+[50]: 路徑

來源:互聯網
上載者:User

本例效果圖:

代碼檔案:unit Unit1;
interface
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls, Grids;
type
 TForm1 = class(TForm)
  StringGrid1: TStringGrid;
  procedure FormCreate(Sender: TObject);
  procedure FormPaint(Sender: TObject);
  procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
   var CanSelect: Boolean);
 end;
var
 Form1: TForm1;
implementation
{$R *.dfm}
uses GDIPOBJ, GDIPAPI;
procedure TForm1.FormCreate(Sender: TObject);
begin
 StringGrid1.Align := alRight;
 StringGrid1.FixedCols := 0;
 StringGrid1.ColCount := 3;
 StringGrid1.ColWidths[0] := 25;
 StringGrid1.ColWidths[1] := 25;
 StringGrid1.ColWidths[2] := 80;
 StringGrid1.DefaultRowHeight := 20;
 StringGrid1.Cells[0,0] := 'X';
 StringGrid1.Cells[1,0] := 'Y';
 StringGrid1.Cells[2,0] := '點類型';
end;
procedure TForm1.FormPaint(Sender: TObject);
var
 g: TGPGraphics;
 p: TGPPen;
 path: TGPGraphicsPath;
 points: array of TGPPoint;
 types: PByte;
 typestr: string;
 i: Integer;
begin
 g := TGPGraphics.Create(Canvas.Handle);
 p := TGPPen.Create(aclRed);
 path := TGPGraphicsPath.Create;
 path.StartFigure;
 path.AddRectangle(MakeRect(30,20,90,40));
 path.AddEllipse(MakeRect(30,80,90,180));
 path.CloseFigure;
 g.DrawPath(p, path);
 SetLength(points, path.GetPointCount);
 GetMem(types, path.GetPointCount);
 path.GetPathPoints(PGPPoint(points), Length(points));
 path.GetPathTypes(types, Length(points));
 StringGrid1.RowCount := Length(points) + 1;
 for i := 0 to Length(points) - 1 do
 begin
  case types^ of
   $00 : typestr := '路徑起始點';
   $01 : typestr := '直線點';
   $03 : typestr := '貝塞爾線點';
   $07 : typestr := '遮蓋點';
   $10 : typestr := '虛線點';
   $20 : typestr := '路徑標記';
   $80 : typestr := '子路徑結束點';
  end;
  StringGrid1.Cells[0, i+1] := IntToStr(points[i].X);
  StringGrid1.Cells[1, i+1] := IntToStr(points[i].Y);
  StringGrid1.Cells[2, i+1] := typestr;
  Inc(types);
 end;
 Dec(types, Length(points));
 FreeMem(types);
 types := nil;
 path.Free;
 p.Free;
 g.Free;
end;
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
 var CanSelect: Boolean);
var
 x,y: Integer;
begin
 x := StrToIntDef(StringGrid1.Cells[0,ARow], 0);
 y := StrToIntDef(StringGrid1.Cells[1,ARow], 0);
 Repaint;
 Canvas.Brush.Color := clBlue;
 Canvas.FillRect(Bounds(x-3,y-3,6,6));
 Text := Format('%d,%d',[x,y]);
end;
end.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.