Delphi 方法或过程中用的参数是动态数组的用法

减小字体 增大字体 作者:佚名  来源:转载  发布时间:2011-08-04 21:51:07

本文来自:http://www.cnblogs.com/zhangzhifeng/archive/2011/07/28/2119886.html

在编写过程中如果入到传入的参数是动态数组的话可以这么做

type

  TPoints = array of TPoint;

Points: TPoints;

SetLength(Points, 3);
  Points[0].X := 150;
  Points[0].Y := 150;

  Points[1].X := 240;
  Points[1].Y := 50;

  Points[2].X := 320;
  Points[2].Y := 200;

源码如下:

unit UForm_Image8;

interface

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

type
  TPoints = array of TPoint;
  TForm_Image8 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    ScrollBox1: TScrollBox;
    PaintBox1: TPaintBox;
    procedure FormCreate(Sender: TObject);
    procedure PaintBox1Paint(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    Points: TPoints;
    R: TRect;
    LeftPoint, RightBottomPoint: TPoint;
    OffsetX, OffsetY: Integer;
    { Private declarations }
    procedure GetCanvasRect(ACanvas: TCanvas; var ARect: TRect);
    function getLeftPoint(APoints: TPoints): TPoint;
    function getRightBottomPoint(APoints: TPoints): TPoint;
    procedure setX(APoints: TPoints; AIncreaseNum: Integer);
    procedure setY(APoints: TPoints; AIncreaseNum: Integer);
    procedure ClearPintBox;
  public
    { Public declarations }
  end;

var
  Form_Image8: TForm_Image8;

implementation

{$R *.dfm}

procedure TForm_Image8.FormCreate(Sender: TObject);
begin
  OffsetX := 0 ;
  OffsetY := 0 ;
  Randomize;
  PaintBox1.Canvas.Brush.Color := Random($FFFFFF);

  SetLength(Points, 3);
  Points[0].X := 150;
  Points[0].Y := 150;

  Points[1].X := 240;
  Points[1].Y := 50;

  Points[2].X := 320;
  Points[2].Y := 200;

  LeftPoint        := getLeftPoint(Points);
  RightBottomPoint := getRightBottomPoint(Points);
  R := Rect(LeftPoint, RightBottomPoint);
  PaintBox1.Canvas.Polygon(Points);
end;

//取左边最小的坐标
function TForm_Image8.getLeftPoint(APoints: TPoints): TPoint;
var
  I, MinX, MinY: Integer;

begin
  MinX := APoints[Low(APoints)].X;
  for I := Low(APoints) to High(APoints) do
  begin
    MinX := min(MinX, APoints[I].X);
  end;

  MinY := APoints[Low(APoints)].Y;
  for I := Low(APoints) to High(APoints) do
  begin
    MinY := min(MinY, APoints[I].Y);
  end;
  Result := Point(MinX, MinY);
end;

//取右边最大的坐标
function TForm_Image8.getRightBottomPoint(APoints: TPoints): TPoint;
var
  I, MaxX, MaxY: Integer;

begin
  MaxX := APoints[Low(APoints)].X;
  for I := Low(APoints) to High(APoints) do
  begin
    MaxX := max(MaxX, APoints[I].X);
  end;

  MaxY := APoints[Low(APoints)].Y;
  for I := Low(APoints) to High(APoints) do
  begin
    MaxY := max(MaxY, APoints[I].Y);
  end;
  Result := Point(MaxX, MaxY);
end;

procedure TForm_Image8.PaintBox1Paint(Sender: TObject);
begin
  PaintBox1.Canvas.Polygon(Points);
end;

procedure TForm_Image8.setX(APoints: TPoints; AIncreaseNum: Integer);
var
  I: Integer;

begin
  for I := Low(APoints) to High(APoints) do
  begin
    APoints[I].X := APoints[I].X + AIncreaseNum;
  end;
end;

procedure TForm_Image8.setY(APoints: TPoints; AIncreaseNum: Integer);
var
  I: Integer;
begin
  for I := Low(APoints) to High(APoints)   do
  begin
    APoints[I].Y := APoints[I].Y + AIncreaseNum;
  end;
end;

procedure TForm_Image8.ClearPintBox;
begin
  PaintBox1.Canvas.Brush.Color := PaintBox1.Parent.Brush.Color  ;//clBtnFace;
  PaintBox1.Canvas.FillRect(PaintBox1.ClientRect);
end;

procedure TForm_Image8.Button1Click(Sender: TObject);
begin
  OffsetY := OffsetY + 2;
  setY(Points, OffsetY);
  PaintBox1.Canvas.Brush.Style := bsClear;
  ClearPintBox;
  PaintBox1.Canvas.Polygon(Points);
end;

procedure TForm_Image8.GetCanvasRect(ACanvas: TCanvas; var ARect: TRect);
begin
  with ACanvas do begin
    ARect.TopLeft     := Point(0, 0);
    ARect.BottomRight := Point(Width, Height);
  end;
end;

end.
——————————————————————————————————————

object Form_Image8: TForm_Image8
  Left = 192
  Top = 114
  Width = 979
  Height = 563
  Caption = '图形处理_矢量图移动'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 808
    Top = 112
    Width = 75
    Height = 25
    Caption = '上移'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 808
    Top = 160
    Width = 75
    Height = 25
    Caption = '下移'
    TabOrder = 1
  end
  object ScrollBox1: TScrollBox
    Left = 0
    Top = 16
    Width = 633
    Height = 337
    HorzScrollBar.Style = ssFlat
    HorzScrollBar.Tracking = True
    BevelInner = bvNone
    BevelOuter = bvNone
    BorderStyle = bsNone
    TabOrder = 2
    object PaintBox1: TPaintBox
      Left = 0
      Top = 0
      Width = 633
      Height = 337
      Align = alClient
      OnPaint = PaintBox1Paint
    end
  end
end

Tags:

作者:佚名
  • 好的评价 如果您觉得此文章好,就请您
      0%(0)
  • 差的评价 如果您觉得此文章差,就请您
      0%(0)

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论

广告位置B