zoukankan      html  css  js  c++  java
  • 再学 GDI+[58]: 路径 保存与读取路径数据

    本例演示了把路径中的数据保存到一个文本文件, 然后再读出的过程.

    本例效果图:



    代码文件:
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure FormPaint(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    uses GDIPOBJ, GDIPAPI;
    
    const
      FilePath = 'c:\temp\path.txt';
    var
      path: TGPGraphicsPath;
      p: TGPPen;
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      pts: array[0..6] of TGPPoint;
      rect: TGPRect;
    begin
      pts[0].X := 10;  pts[0].Y := 50;
      pts[1].X := 40;  pts[1].Y := 90;
      pts[2].X := 80;  pts[2].Y := 10;
      pts[3].X := 110;  pts[3].Y := 50;
      pts[4].X := 140;  pts[4].Y := 10;
      pts[5].X := 180; pts[5].Y := 90;
      pts[6].X := 210; pts[6].Y := 50;
    
      path := TGPGraphicsPath.Create;
      path.AddBeziers(PGPPoint(@pts), Length(pts));
    
      path.GetBounds(rect);
      path.AddEllipse(rect);
    
      p := TGPPen.Create(aclBlue, 2);
    
      Button1.Caption := '保存路径数据';
      Button2.Caption := '读取路径数据';
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      path.Free;
      p.Free;
    end;
    
    procedure TForm1.FormPaint(Sender: TObject);
    var
      g: TGPGraphics;
    begin
      g := TGPGraphics.Create(Canvas.Handle);
      g.DrawPath(p, path);
      g.Free;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      points: array of TGPPoint;
      types: array of Byte;
      List: TStringList;
      i: Integer;
    begin
      SetLength(points, path.GetPointCount);
      SetLength(types, path.GetPointCount);
      path.GetPathPoints(PGPPoint(points), Length(points));
      path.GetPathTypes(PByte(types), Length(types));
    
      List := TStringList.Create;
      for i := 0 to Length(points) - 1 do
        List.Add(Format('%d,%d,%d', [points[i].X, points[i].Y, types[i]]));
    
      List.SaveToFile(FilePath);
      List.Free;
    
      Text := FilePath;
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    var
      points: array of TGPPoint;
      types: array of Byte;
      List1,List2: TStringList;
      i: Integer;
    begin
      List1 := TStringList.Create;
      List2 := TStringList.Create;
    
      if not FileExists(FilePath) then Exit;
      List1.LoadFromFile(FilePath);
    
      SetLength(points, List1.Count);
      SetLength(types, List1.Count);
    
      for i := 0 to List1.Count - 1 do
      begin
        if List1[i] = '' then Break;
        List2.CommaText := List1[i];
        points[i].X := StrToIntDef(List2[0], 0);
        points[i].Y := StrToIntDef(List2[1], 0);
        types[i] := StrToIntDef(List2[2], 0);
      end;
    
      path.Reset;
      path.Free;
      path := TGPGraphicsPath.Create(PGPPoint(points), PByte(types), List1.Count);
    
      p.SetColor(aclRed);
      Repaint;
    
      List1.Free;
      List2.Free;
    end;
    
    end.
    
    窗体文件:
    object Form1: TForm1
      Left = 0
      Top = 0
      Caption = 'Form1'
      ClientHeight = 147
      ClientWidth = 224
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'Tahoma'
      Font.Style = []
      OldCreateOrder = False
      Position = poDesktopCenter
      OnCreate = FormCreate
      OnDestroy = FormDestroy
      OnPaint = FormPaint
      PixelsPerInch = 96
      TextHeight = 13
      object Button1: TButton
        Left = 13
        Top = 114
        Width = 94
        Height = 25
        Caption = 'Button1'
        TabOrder = 0
        OnClick = Button1Click
      end
      object Button2: TButton
        Left = 117
        Top = 114
        Width = 94
        Height = 25
        Caption = 'Button2'
        TabOrder = 1
        OnClick = Button2Click
      end
    end
    
  • 相关阅读:
    springmvc
    POJ 3683 Priest John's Busiest Day
    POJ 3678 Katu Puzzle
    HDU 1815 Building roads
    CDOJ UESTC 1220 The Battle of Guandu
    HDU 3715 Go Deeper
    HDU 3622 Bomb Game
    POJ 3207 Ikki's Story IV
    POJ 3648 Wedding
    HDU 1814 Peaceful Commission
  • 原文地址:https://www.cnblogs.com/del/p/1227421.html
Copyright © 2011-2022 走看看